Jump to content

[SOLVED] MySql result loop problems only first record shown


Recommended Posts

Can anyone tell what i'm doing wrong?

Is there another way to do this?

 

Here's my problem, i have a few categories on a page,

the first SQL query, that works fine however i'd like to add products belonging to the category,

right under the header, but i'm only getting one record returned, seems like the second loop doesn't work at all.

PS. i'm using an accordion script for this, the "dhtmlgoodies_question" is the header, (the category)

and the "dhtmlgoodies_answer" is the products information

 

Here's the code:

<?php
$query  = "SELECT * FROM collection WHERE mcat_id='$cat_id'";
$result = mysql_query($query);
while($row = mysql_fetch_array( $result )) {

$c_name = $row['name'];
$c_desc =$row['desc'];
$c_id =$row['collection_id'];

$query_prod  = "SELECT * FROM products WHERE c_id='$c_id' ORDER BY name DESC";
$result_prod = mysql_query($query_prod);
while($row = mysql_fetch_array( $result_prod )) {

$p_name = $row['name'];
$p_desc =$row['desc'];
$p_pic =$row['pic'];
}

echo '
<div class="dhtmlgoodies_question"><strong>'.$c_name.'</strong><br />
'.$c_desc.'<br />
<div class="dt_button">Show Products</div>
</div>
<div class="dhtmlgoodies_answer"><div>
Product Name: <b>'.$p_name.'</b><br>
Product Desc: '.$p_desc.'<br>

<br />
  </div>
</div>' ;
}
?>

 

 

You overwrite $row in your second while() loop.

 

The second while() loop doesn't print anything, it just loops over the result rows and assigns variables until there are no more results. If you want to print all results you'll need to do so inside the loop.

You will want to place the echo statement within the second while loop, but remove the first line of html from the echo statement and echo it out outside of the secound while loop.

<?php

$query  = "SELECT * FROM collection WHERE mcat_id='$cat_id'";
$result = mysql_query($query);

while($row = mysql_fetch_array( $result ))
{
    $c_name = $row['name'];
    $c_desc = $row['desc'];
    $c_id   = $row['collection_id'];

    // Only want to the header to show once
    echo '
    <div class="dhtmlgoodies_question"><strong>' . $c_name . '</strong><br />
    ' . $c_desc . ' <br />';

    $query_prod  = "SELECT * FROM products WHERE c_id='$c_id' ORDER BY name DESC";
    $result_prod = mysql_query($query_prod);

    while($row = mysql_fetch_array( $result_prod ))
    {
        $p_name = $row['name'];
        $p_desc = $row['desc'];
        $p_pic  = $row['pic'];

        // show all items that belong to the header
        echo '
        <div class="dt_button">Show Products</div>
        </div>
        <div class="dhtmlgoodies_answer"><div>
        Product Name: <b>'.$p_name.'</b><br>
        Product Desc: '.$p_desc.'<br>

         <br />
          </div>
        </div>';
    }
}

?>

@wildteen88

THANKS A LOT, here's what i did and it works like a charm..

i guess i was just looking at it too long and didn't see the obvious problem,

which was my <div> placements....

 

@charlieholder

I've tried with JOIN but in this instance with the AJAX accordion it wouldn't work properly,

because the category name would repat itself for every product....

 

FIXED CODE....

<?php
// CATEGORY
$query  = "SELECT * FROM collection WHERE mcat_id='$cat_id'";
$result = mysql_query($query);
while($row = mysql_fetch_array( $result )) { 

$c_name = $row['name'];
$c_desc =$row['desc'];
$c_id =$row['collection_id'];
// Header info....
echo '
<div class="dhtmlgoodies_question"><strong>'.$c_name.'</strong><br />
'.$c_desc.'<br />
<div class="dt_button">Show Products</div>
</div>';

// PRODUCTS
?>
<div class="dhtmlgoodies_answer"><div>
<?php
$query_prod  = "SELECT * FROM products WHERE c_id='$c_id' ORDER BY name DESC";
$result_prod = mysql_query($query_prod);
while($row = mysql_fetch_array( $result_prod )) {

$p_name = $row['name'];
$p_desc =$row['desc'];
$p_pic =$row['pic'];
// Product info....
echo '
Product Name: <b>'.$p_name.'</b><br>
Product Desc: '.$p_desc.'<br>
<br />';
}
?>
  </div>
</div>
<?php
}
?>

 

THANKS ALL

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.