Colleen78 Posted November 3, 2008 Share Posted November 3, 2008 I have this PHP code, it creates 1 row of the 3 latest new items in our shopping cart: <tr align="center" valign="top"> <?php //Specify the query $query = 'SELECT * FROM `tbl_item` where category_id not in (104,105,106,107,108,109,110,111,112,113,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,132,133,138,139,140) GROUP BY `item_id` ORDER BY `item_id` DESC LIMIT 3'; //Execut the query $result = mysql_query($query) or die ("Error. Unable to perform query."); //Loop through all the results, displaying each one while ($row = mysql_fetch_array($result)) { extract($row); $item_name = str_replace(" ", "_",$item_name); echo "<td width=\"33%\" bgcolor=\"#c2edaa\" style=\"border: 1px solid rgb(45, 135, 7);\">\n"; echo "<a href=\"$URL/item/$item_name/$item_id\"><img src=\"$SSL_URL/item_images/$thumbnail_image\" width=\"110\" height=\"100\" alt=\"$short_description\" border=\"0\" /></a><br />\n"; echo "<b>$row[item_name]</b><br />\n"; echo "$row[short_description]"; echo "</td>\n"; } ?></tr> But now I'd like to display 9 items, so 3 rows of 3 items per row... I don't know how to include the <tr> tag into the php to make it loop per each 3 items, does anyone have an idea how to do this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/131146-solved-how-to-create-rows-within-my-loop/ Share on other sites More sharing options...
.josh Posted November 3, 2008 Share Posted November 3, 2008 apply concept to your code: <?php $cols = 0; echo "<table><tr>"; while ($cols < 20) { echo ($cols % 3 == 0)? "</tr><tr>" : ""; echo "<td>$cols</td>"; $cols++; } echo "</tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/131146-solved-how-to-create-rows-within-my-loop/#findComment-680940 Share on other sites More sharing options...
Colleen78 Posted November 3, 2008 Author Share Posted November 3, 2008 Thank you, CV. I don't need to include the table start and end, because I have it as you'll see below, and also, this is how I included your code, I tried other ways, but keep getting an error. The error is: Parse error: syntax error, unexpected $end in /home/domain/public_html/template/hctheme/index.php on line 76 My code: <table class="newitems" cellspacing="1" cellpadding="0" border="0" bgcolor="#ffffff" align="center" width="100%" style="border: 1px solid rgb(45, 135, 7);"> <tr><td colspan="3" align="center"><strong>New Products</strong></td></tr> <?php //Specify the query $query = 'SELECT * FROM `tbl_item` where category_id not in (104,105,106,107,108,109,110,111,112,113,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,132,133,138,139,140) GROUP BY `item_id` ORDER BY `item_id` DESC LIMIT 3'; //Execut the query $result = mysql_query($query) or die ("Error. Unable to perform query."); //Loop through all the results, displaying each one while ($row = mysql_fetch_array($result)) $cols = 0; while ($cols < 20) { echo ($cols % 3 == 0)? "</tr><tr>" : ""; { extract($row); $item_name = str_replace(" ", "_",$item_name); echo "<td width=\"33%\" bgcolor=\"#c2edaa\" style=\"border: 1px solid rgb(45, 135, 7);\">\n"; echo "<a href=\"$URL/item/$item_name/$item_id\"><img src=\"$SSL_URL/item_images/$thumbnail_image\" width=\"110\" height=\"100\" alt=\"$short_description\" border=\"0\" /></a><br />\n"; echo "<b>$row[item_name]</b><br />\n"; echo "$row[short_description]"; echo "</td>\n"; $cols++; } ?></table> I'm not quite sure how to adapt your code into mine. ??? Quote Link to comment https://forums.phpfreaks.com/topic/131146-solved-how-to-create-rows-within-my-loop/#findComment-680952 Share on other sites More sharing options...
.josh Posted November 3, 2008 Share Posted November 3, 2008 Ah see well the while loop in my code was just an example loop. You would use your $row = ... instead of that. Also you need to move $cols = 0; out of the loop: <table class="newitems" cellspacing="1" cellpadding="0" border="0" bgcolor="#ffffff" align="center" width="100%" style="border: 1px solid rgb(45, 135, 7);"> <tr><td colspan="3" align="center"><strong>New Products</strong></td></tr> <?php //Specify the query $query = 'SELECT * FROM `tbl_item` where category_id not in (104,105,106,107,108,109,110,111,112,113,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,132,133,138,139,140) GROUP BY `item_id` ORDER BY `item_id` DESC LIMIT 3'; //Execut the query $result = mysql_query($query) or die ("Error. Unable to perform query."); //Loop through all the results, displaying each one $cols = 0; while ($row = mysql_fetch_array($result)) { echo ($cols % 3 == 0)? "</tr><tr>" : ""; extract($row); $item_name = str_replace(" ", "_",$item_name); echo "<td width=\"33%\" bgcolor=\"#c2edaa\" style=\"border: 1px solid rgb(45, 135, 7);\">\n"; echo "<a href=\"$URL/item/$item_name/$item_id\"><img src=\"$SSL_URL/item_images/$thumbnail_image\" width=\"110\" height=\"100\" alt=\"$short_description\" border=\"0\" /></a><br />\n"; echo "<b>$row[item_name]</b><br />\n"; echo "$row[short_description]"; echo "</td>\n"; $cols++; } ?></table> Quote Link to comment https://forums.phpfreaks.com/topic/131146-solved-how-to-create-rows-within-my-loop/#findComment-680974 Share on other sites More sharing options...
Colleen78 Posted November 3, 2008 Author Share Posted November 3, 2008 Wow, thank you, that's perfect, and I learned what I am doing wrong. Quote Link to comment https://forums.phpfreaks.com/topic/131146-solved-how-to-create-rows-within-my-loop/#findComment-680977 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.