Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8235871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:57:46+00:00 2026-06-07T18:57:46+00:00

The following script works correctly however I don’t like it because it isn’t elegant

  • 0

The following script works correctly however I don’t like it because it isn’t elegant and is just down right messy.

Without getting too advanced (meaning I don’t want OOP style) I just want to minimize this code in a procedural fashion. Mainly I want to combine the SQL queries and clean up the mess.

//get page url and query db to find the correct page
$this_page = $_GET['page'];
$this_page = escape_data($_GET['page']);

//Make sure page exists - if it doesn't redirect the browser
$SQL_page_exist = "SELECT * FROM pages_learn_more WHERE page_title = '$this_page'";
$SPE_result = mysql_query($SQL_page_exist);

while ($details = mysql_fetch_array($SPE_result))
{
$page_id          =     $details['id'];
$page_title       =     $details['page_title'];
$main_title       =     $details['main_title'];
$main_content     =     $details['main_content'];
$sub_title        =     $details['sub_title'];
$sub_content      =     $details['sub_content'];    
}

if(mysql_num_rows($SPE_result) == 0) 
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=page_not_found.php">';

} else {

$SQL = 
    "SELECT   ptt.tool_tip_link
       FROM   pages_tool_tip ptt
       JOIN   pages_to_pages ptp
         ON   ptp.tool_tip_id = ptt.tool_tip_id
      WHERE   ptp.learn_more_id = '$page_id'";

  $result = mysql_query($SQL); // or die(mysql_error()); 

  //set array for reference_keys variable which may contain 0 to 20 keys
  $reference_keys = array();
  while ($db_field = mysql_fetch_array($result))
{   
 $reference_keys[] =     $db_field['tool_tip_link'];    
}   

Ok so this above code works as it should, but it is very ugly and I have been trying for the past 2 hours to refine it into something clean, simple and beautiful, but alas my skills are not yet there and all i get is mysql errors.

My most recent attempt is:

$SQL = 
"
     SELECT     *
       FROM     pages_learn_more plm

       JOIN     pages_tool_tip ptt, pages_to_pages ptp 
         ON     ptp.tool_tip_id = ptt.tool_tip_id
      WHERE     plm.page_title = '$this_page' AND ptp.learn_more_id = plm.id 
";

$result = mysql_query($SQL); // or die(mysql_error()); 

$reference_keys = array();
while ($details = mysql_fetch_array($result))
{   
        $reference_keys[] =     $details['tool_tip_link'];  
        $page_id          =     $details['id'];
        $page_title       =     $details['page_title'];
        $main_title       =     $details['main_title'];
        $main_content     =     $details['main_content'];
        $sub_title        =     $details['sub_title'];
        $sub_content      =     $details['sub_content'];    
}

**This is like my 30th failed attempt to clean up this code. Can someone with knowledge please help me out and if possible please explain your input…

Thank you **

As a side note:
I even tried many queries in PHPmyAdmin, which taught me that the order of your tables in your SELECT of FROM statement will impact your result so for example SELECT pages_learn_more plm, pages_tool_tip ptt != SELECT pages_tool_tip ptt, pages_learn_more plm And I don’t understand how the order of these statements impacts the results. (When used in the full query 1 Will result in an error and the other Shows the tables)

PIC OF MY DATABASE SCHEME IF IT HELPS

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:57:48+00:00Added an answer on June 7, 2026 at 6:57 pm

    You have a strange mix of implicit (comma-separated tables) and explicit JOINs, which affects the order the tables appear in. Implicit joins are discouraged, so use only explicit ones.

    Since you are not using all columns, do not SELECT *, especially since some tables have the same column names (tool_tip_id), which causes ambiguity. Instead, be a little more explicit about what you’re selecting. You can even replace plm.* below with the exact list of columns you need from that table.

    $SQL = 
    "
         SELECT
           plm.*,
           ptt.tool_tip_link
         FROM
             pages_learn_more plm
             LEFT JOIN pages_to_pages ptp ON plm.id = ptp.learn_more_id
             LEFT JOIN pages_tool_tip ptt ON ptp.tool_tip_id = ptt.tool_tip_id
          WHERE
             plm.page_title = '$this_page'
    ";
    

    In your while loop, the only columns you access are those of pages_learn_more and one from pages_tool_tip, so in the SELECT list, I used plm.* for all columns on that table, and ptt.tool_tip_link only from the other table.

    Since your primary table appears to be pages_learn_more which may or may not have associated records in pages_to_pages, I substituted a LEFT JOIN, so even if it had no associated tool tip, the query could still return a record (with a NULL tooltip).

    Technically, the second LEFT JOIN could just be an INNER JOIN since there must be a record in pages_tool_tip if one exists pages_to_pages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following script works for me, but I wonder if it could be done
The following script works fine: $(#regform).validate().showErrors({username:message}); After I changed the script to the below
The following script works to open Firefox's location/awesome bar from anywhere using control-l ,
I have the following script that works well in Firefox and Chrome (not sure
I have the following code that works fine in IE: <HTML> <BODY> <script language=JavaScript>
I have the following chunk of code. It works perfectly. <div id=restaurant_locations></div> <script type=text/javascript>
The following example works fine in Firefox and Opera, but not in ie8. However,
Dear all, i've got following script here (don't blame me i don't know javascript
Do you think jquery could help me get the following script work faster? Thanks!
How would I change the following markov script to treat capitalized and lowercase words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.