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 8265949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:58:47+00:00 2026-06-08T04:58:47+00:00

forgive me I just learned php this week, so I’m not sure I’m doing

  • 0

forgive me I just learned php this week, so I’m not sure I’m doing this all right.

It starts out accessing the DB, and the categories table for headers; it then takes that info and creates the header, pricing, and catalog links.

Then within that while loop after it completes the first part it’s supposed to run a second while loop to access the products table to list all the products with the category_id that matches the cat_id from the categories table.

When it prints out it should be

Header
Pricing PDF
Item Dimensions Image Image
Item Dimensions Image Image
Item Dimensions Image Image
etc

Header
Pricing PDF
Item Dimensions Image Image
etc….

And so far the first while loop works but the second isn’t. Is there a correct way to pass the variable? Can I just not access a second table while in a while loop for the first table? I dunno…I’ve tried a few things, and nothing is working well

<?php
//connect to server
$con = mysql_connect('localhost','username','password');
//test connection
if (!$con)
{
    die ('Could not connect: ' . mysql_error());
}

//access primary DB 
mysql_select_db("main_db", $con);

//place table into variable
$categories = mysql_query("SELECT * FROM categories");


//begin table build
while($row = mysql_fetch_array($categories))
{
    //set shading variable
    $table_row = 0;

    //set current set   
    $cur_set = $row['cat_id'];
    //create document link and header
    echo "<a name='" . $row['cat_name'] . "'><h3>" . $row['cat_title'] . "</h3></a>";
    //create table and table formatting cell
    echo "<table id='productTable'><tr id='tableHead'>";
    //table width formattting here
    echo "<td style='width:165px;'></td>";
    echo "<td style='width:235px;'></td>";
    echo "<td style='width:155px;'>";
    //link and icons to category catalog
    echo "<a href='catalog/" . $row['cat_pdf'] . ".pdf'><img src='data/pdflogo.png' alt='pdf button' /></a>";
    //link and icons to category pricing sheet
    echo "<a href='catalog/" . $row['cat_pricing'] . ".pdf'><img src='data/pricinglogo.png' alt='pricing button' /></a>";
    //finish formatting
    echo "</td></tr>";

    //place table into variable
    $products = mysql_query("SELECT * FROM products WHERE category_id='" . $row['cat_id'] . "'");

    //begin table build
    while($table = mysql_fetch_array($products));
    {
        //create up row
        echo "<tr id='tr" . $table_row . "'>";
        //create first cell
        echo "<td>" . $table['prod_name'] . "</td>";
        //create second cell
        echo "<td>" . $table['prod_dim'] . "</td>";
        //create third cell
        echo "<td>";
        //create third cell, first image
        echo "<a href='catalog/" . $table['prod_img1'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
        //create third cell, second image
        echo "<a href='catalog/" . $row2['prod_img2'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
        //finish formatting
        echo "</td></tr>";
        //cycle row
        if ($table_row == 0)
            {
                    $table_row = 1;
            }
        else
            {
                $table_row = 0;
            }

    //end table
    echo "</table>";
    }
}

//close connection
mysql_close($con);
?>

Thanks in advance

  • 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-08T04:58:49+00:00Added an answer on June 8, 2026 at 4:58 am

    It would be more streamlined to perform an INNER JOIN on both tables

    SELECT
        A.cat_id,A.cat_name,A.cat_title,A.cat_pdf,A.cat_pricing,
        B.prod_name,B.prod_img1,B.prod_img2
    FROM categories A INNER JOIN products B ON A.cat_id = B.category_id;
    

    You can iterate on A.cat_id

    This is my proposed suggestion (braces might be off, but here is what the iteration on cat_id should look like). Please change the style for starting and stopping tags.

    <?php
    //connect to server
    $con = mysql_connect('localhost','username','password');
    //test connection
    if (!$con)
    {
        die ('Could not connect: ' . mysql_error());
    }
    
    //access primary DB 
    mysql_select_db("main_db", $con);
    
    //place table into variable
    $categories = mysql_query("SELECT A.cat_id,A.cat_name,A.cat_title,A.cat_pdf,A.cat_pricing,B.prod_name,B.prod_img1,B.prod_img2 FROM categories A INNER JOIN products B ON A.cat_id = B.category_id");
    
    $current_catid = -1;
    
    //begin table build
    while($row = mysql_fetch_array($categories))
    {
            if ( $current_catid != $row['cat_id'] )
            {
                if ( $current_catid > -1 ) { echo "</table>"; }
                $current_catid != $row['cat_id']
    
        //set shading variable
        $table_row = 0;
    
        //set current set   
        $cur_set = $row['cat_id'];
        //create document link and header
        echo "<a name='" . $row['cat_name'] . "'><h3>" . $row['cat_title'] . "</h3></a>";
        //create table and table formatting cell
        echo "<table id='productTable'><tr id='tableHead'>";
        //table width formattting here
        echo "<td style='width:165px;'></td>";
        echo "<td style='width:235px;'></td>";
        echo "<td style='width:155px;'>";
        //link and icons to category catalog
        echo "<a href='catalog/" . $row['cat_pdf'] . ".pdf'><img src='data/pdflogo.png' alt='pdf button' /></a>";
        //link and icons to category pricing sheet
        echo "<a href='catalog/" . $row['cat_pricing'] . ".pdf'><img src='data/pricinglogo.png' alt='pricing button' /></a>";
        //finish formatting
        echo "</td></tr>";
            }
    
    //create up row
        echo "<tr id='tr" . $table_row . "'>";
        //create first cell
        echo "<td>" . $table['prod_name'] . "</td>";
        //create second cell
        echo "<td>" . $table['prod_dim'] . "</td>";
        //create third cell
        echo "<td>";
        //create third cell, first image
        echo "<a href='catalog/" . $table['prod_img1'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
        //create third cell, second image
        echo "<a href='catalog/" . $row2['prod_img2'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
    
        //finish formatting
        echo "</td></tr>";
            //cycle row
        if ($table_row == 0)
        {
            $table_row = 1;
        }
        else
        {
            $table_row = 0;
        }
    
        //end table (Fix this, might produce extra table tag)
        echo "</table>";
    }
    
    //close connection
    mysql_close($con);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just picking up development in CakePHP right now so forgive me if this
I just learned about character sets today, so forgive the newb factor if this
I'm just starting out, so please forgive if this is a silly question. I'm
Just getting into the NoSQL stuff so forgive me if this is a simple
Forgive if this question is silly. I just started reading the SugarCRM developer documentation
I am just starting out with Haskell and Yesod so please forgive if I
Forgive my second-rate google-fu, but I'm finding this topic tricky to search for. All
I'm just starting to explore C++, so forgive the newbiness of this question. I
I'm just getting into C#, so forgive me if this is a basic question.
I'm just starting with OO php so please forgive my ignorance. Assuming I have

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.