shamwowy Posted August 19, 2011 Share Posted August 19, 2011 Hi all, I'm trying to print a list of records out of my database in a table. Each row contains 5 cells and is then supposed to loop into the next row of 5 cells until there are no more rows. The table is building almost correctly, with the slight problem of every 6th record is not showing up. Here's the code: <tr> $query1 =" SELECT * FROM jitem JOIN jloc ON jloc.jloc_id IN(jitem.jitem_location) WHERE jitem.jitem_display = 1 ORDER BY jitem_listorder"; if( $r = mysql_query($query1) ) { $ii = 1; while( $row = mysql_fetch_assoc($r) ) { if ($ii <= 5) { print "<td width=20% valign=top align=center>"; print "<a href='slides/{$row['jitem_slide']}' title='{$row['jitem_title']}' border=0 onclick=\"window.open(this.href, '','height=545,width=500');return false;\">"; print "<img border=0 src='thumbs/{$row['jitem_thumb']}' alt='{$row['jitem_title']}'/><br/>"; print "<font size=2 color=white><strong>{$row['jitem_title']}</strong></a><br/>{$row['jitem_desc']}<br/>"; print "\${$row['jitem_price']}</font>"; print "<br/><font size=2><a href=cart.php?itemid=".$row['jitem_id']."&action=add>Add To Cart</a></font>"; print "</td>"; $ii ++; } else { print "</tr><tr><td colspan=5> </td></tr><tr>"; $ii = 1; } } } else { print "Database returned no records. query1 is ".$query1.""; } mysql_close(); Something is wrong with my counter but I'm not sure what. My output looks like this: Item1 Item2 Item3 Item4 Item5 Item7 Item8 Item9 Item10 Item11 Item13 Item14 Any thoughts on my counter? Is <= 5 my problem? Or am I resetting $ii in the wrong spot? I've tried a few variations but it just makes things really bad. Any help greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/245179-looping-problem-dropping-a-record-every-6th-row/ Share on other sites More sharing options...
MasterACE14 Posted August 19, 2011 Share Posted August 19, 2011 Each row contains 5 cells and is then supposed to loop into the next row of 5 cells until there are no more rows. The code is doing exactly what you want it to do? Link to comment https://forums.phpfreaks.com/topic/245179-looping-problem-dropping-a-record-every-6th-row/#findComment-1259323 Share on other sites More sharing options...
shamwowy Posted August 19, 2011 Author Share Posted August 19, 2011 The code is doing what I want it to do with the slight exception of dropping every 6th database record. So it prints a row of 5 records, then record #6 disappears, then it prints another row of 7-11, drops #12, etc. Link to comment https://forums.phpfreaks.com/topic/245179-looping-problem-dropping-a-record-every-6th-row/#findComment-1259349 Share on other sites More sharing options...
harristweed Posted August 19, 2011 Share Posted August 19, 2011 <?php $query1 =" SELECT * FROM jitem JOIN jloc ON jloc.jloc_id IN(jitem.jitem_location) WHERE jitem.jitem_display = 1 ORDER BY jitem_listorder"; if( $r = mysql_query($query1) ) { $ii = 1; while( $row = mysql_fetch_assoc($r) ) { if ($ii <= 5) { print "<td width=20% valign=top align=center>"; print "<a href='slides/{$row['jitem_slide']}' title='{$row['jitem_title']}' border=0 onclick=\"window.open(this.href, '','height=545,width=500');return false;\">"; print "<img border=0 src='thumbs/{$row['jitem_thumb']}' alt='{$row['jitem_title']}'/><br/>"; print "<font size=2 color=white><strong>{$row['jitem_title']}</strong></a><br/>{$row['jitem_desc']}<br/>"; print "\${$row['jitem_price']}</font>"; print "<br/><font size=2><a href=cart.php?itemid=".$row['jitem_id']."&action=add>Add To Cart</a></font>"; print "</td>"; $ii ++; if($ii==5){ print "</tr><tr><td colspan=5> </td></tr><tr>"; $ii = 1; } } } } else { print "Database returned no records. query1 is ".$query1.""; } mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/245179-looping-problem-dropping-a-record-every-6th-row/#findComment-1259352 Share on other sites More sharing options...
sasa Posted August 19, 2011 Share Posted August 19, 2011 <tr> $query1 =" SELECT * FROM jitem JOIN jloc ON jloc.jloc_id IN(jitem.jitem_location) WHERE jitem.jitem_display = 1 ORDER BY jitem_listorder"; if( $r = mysql_query($query1) ) { $ii = 0; //before is 0 echoed while( $row = mysql_fetch_assoc($r) ) { if ($ii == 0) echo '<tr>'; //start table row print "<td width=20% valign=top align=center>"; print "<a href='slides/{$row['jitem_slide']}' title='{$row['jitem_title']}' border=0 onclick=\"window.open(this.href, '','height=545,width=500');return false;\">"; print "<img border=0 src='thumbs/{$row['jitem_thumb']}' alt='{$row['jitem_title']}'/><br/>"; print "<font size=2 color=white><strong>{$row['jitem_title']}</strong></a><br/>{$row['jitem_desc']}<br/>"; print "\${$row['jitem_price']}</font>"; print "<br/><font size=2><a href=cart.php?itemid=".$row['jitem_id']."&action=add>Add To Cart</a></font>"; print "</td>"; $ii ++; if($ii == 5) //end row { print "</tr><tr><td colspan=5> </td></tr>"; $ii = 0; } } else { print "Database returned no records. query1 is ".$query1.""; } mysql_close(); Link to comment https://forums.phpfreaks.com/topic/245179-looping-problem-dropping-a-record-every-6th-row/#findComment-1259419 Share on other sites More sharing options...
shamwowy Posted August 19, 2011 Author Share Posted August 19, 2011 Thanks guys/gals! I will try this out and close out this thread when I get home and it works . Link to comment https://forums.phpfreaks.com/topic/245179-looping-problem-dropping-a-record-every-6th-row/#findComment-1259620 Share on other sites More sharing options...
Psycho Posted August 20, 2011 Share Posted August 20, 2011 Not to knock the others' solutions, I'd prefer using a modulus operator in this situation. Also, I see there was no handling to close the last row if the results weren't an exact multiple of 5. I don't like having mis-formatted HTML. Here's my solution. you can easily change how many columns are used by changing the value of $cols_per_row. Also, the last row will always be properly closed even if there are only 1-4 record on the last row $query1 ="SELECT * FROM jitem JOIN jloc ON jloc.jloc_id IN(jitem.jitem_location) WHERE jitem.jitem_display = 1 ORDER BY jitem_listorder"; if( $r = mysql_query($query1) ) { $cols_per_row = 5; $column = 0; while( $row = mysql_fetch_assoc($r) ) { $column++; if ($column % $cols_per_row == 1) { echo "<tr>\n"; //start table row } print "<td width=20% valign=top align=center>"; print "<a href='slides/{$row['jitem_slide']}' title='{$row['jitem_title']}' border=0 onclick=\"window.open(this.href, '','height=545,width=500');return false;\">"; print "<img border=0 src='thumbs/{$row['jitem_thumb']}' alt='{$row['jitem_title']}'/><br/>"; print "<font size=2 color=white><strong>{$row['jitem_title']}</strong></a><br/>{$row['jitem_desc']}<br/>"; print "\${$row['jitem_price']}</font>"; print "<br/><font size=2><a href=cart.php?itemid={$row['jitem_id']}&action=add>Add To Cart</a></font>"; print "</td>\n"; if ($column % $cols_per_row == 0) { echo "</tr>\n"; //end table row } } //Close last row if not already closed if ($column % $cols_per_row != 0) { echo "</tr>\n"; //end last table row } } else { print "Database returned no records. query1 is {$query1}"; } mysql_close(); Link to comment https://forums.phpfreaks.com/topic/245179-looping-problem-dropping-a-record-every-6th-row/#findComment-1259625 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.