shure2 Posted April 5, 2010 Share Posted April 5, 2010 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 https://forums.phpfreaks.com/topic/197632-using-a-for-each-loop-to-create-html-tables-with-data-from-a-database-s/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.