Jump to content

Using a for each loop to create html tables with data from a database :s


shure2

Recommended Posts

Hi,

 

This is the 1st time I have used a for each loop and I'm having a few issues.

 

I have a leadproductdetail table that with the following structure:

LEADPRODUCTDETAIL(leadproddetailid(pk), leadid, leadprodnumber, leadproddetailfield, leadproddetailvalue)

 

It seems messy but all it really shows is the number of the product (say 1) and the product fields such as depth, height and their values.

 

The leadid is in the table as a foreign key because there can be many leadproductdetails to one lead, basically a lead can have a number of products, and each of those products has details such as height, depth and a value.

 

I am trying to use a for each loop to display the number of the product and the associated details with that product.

 

So far I have the below code, but the while loop within the for loop only runs once and I need it to echo out the leadproddetailfield and leadproddetailvalue for each specific product number.

 

$query =  "select * from leadproductdetail where userid = '".$_SESSION['userid']."'";
                $result = @mysql_query($query, $connection) or die ("Unable to perform query.<br />$query<br/>".mysql_error());

                $query2 = "select * from leadproductdetail";
                $result2 = @mysql_query($query2, $connection) or die ("Unable to perform query.<br />$query2<br/>".mysql_error());
                $row1 = @mysql_num_rows($result2);
                ?>

                <?php
                for ($i=1; $i<=$row1; $i++)
                { 
                    echo $i //$i can represent the product number
                    ?>

                  <table border="1">
                    <tr>
                        <th >Detail</th>
                        <th >Value</th>
                    </tr>

                <?php
                while($row= mysql_fetch_array($result))  //I need to have a condition like 'where leadprodid = $i so that in the 1 loop only the details and values are shown for the relevant product)
                { ?>
                    <tr>
                        <td><?php echo $row['leadproddetailfield']?></td>
                        <td><?php echo $row['leadproddetailvalue']?></td>
                    </tr>
               <?php
                } ?>
                </table>
                <?php }?>

 

 

The output should look like this:

 

Product number: 1

 

<table>

Detail        Value

*****        ********

******      ******

****          *****

</table>

 

Product number: 2

 

<table>

Detail        Value

*****        ********

******      ******

****          *****

</table>

 

etc

Link to comment
Share on other sites

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.