microbert Posted April 3, 2012 Share Posted April 3, 2012 I am managing a shop website which is using php and mysql for data. The website has a section that will display the related products with the one that you are watching. Now my problem is that if there are more than 5 items it will continue to display all the items in one row with the consequent that the page will not display well. I need to find a way to begin a new row after the 5th item so it will display 5 items in each row. this is my current code that is responsible for showing the related items. There is also a picture attached with the problem. while ($row = mysql_fetch_array($retd)) { $code = $row["code"]; $name = $row["name"]; echo("<td width=150 align=center>"); echo ("<a href=../products/info.php?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>"); echo ("<br><a href=../products/info.php?scode=$code><span class=fs13>$name</span></a>"); echo("</td>"); } Does anyone know how can I do this? Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/ Share on other sites More sharing options...
scootstah Posted April 3, 2012 Share Posted April 3, 2012 Basically, you'll need to use a little logic to end rows and create new ones. One way to do that is to increment a counter and then just test the counter's value. My code is a little different than yours, just for simplicity but it's the same concept. // create an array to loop through $array = range(1,20); // set number of columns per row $cols = 5; // start our counter $i = 0; echo '<table>'; foreach($array as $arr) { // check the modulus of our counter // if it is 0 we have to start a new row if ($i % $cols == 0) { if ($i > 0) { // end the current row if it isnt the first row echo '</tr>'; } echo '<tr>'; } echo '<td>' . $arr . '</td>'; // increment the counter // this is important, dont forget this $i++; } echo '</tr>'; echo '</table>'; Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1333930 Share on other sites More sharing options...
microbert Posted April 3, 2012 Author Share Posted April 3, 2012 I have tried your code and it worked fine with the numbers, but I am finding a problem to get the data from the mysql database and then display it that way. Can you tell me how can I fill the array from the database field? Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1333960 Share on other sites More sharing options...
litebearer Posted April 3, 2012 Share Posted April 3, 2012 http://www.phpfreaks.com/forums/index.php?topic=95426.0 Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1333966 Share on other sites More sharing options...
scootstah Posted April 3, 2012 Share Posted April 3, 2012 Do the same thing only in the while loop. // define the number of columns per row $cols = 5; // start the counter $i = 0; while ($row = mysql_fetch_array($retd)) { $code = $row["code"]; $name = $row["name"]; if ($i % $cols == 0) { if ($i > 0) { echo '</tr>'; } echo '<tr>'; } echo("<td width=150 align=center>"); echo ("<a href=../products/info.php?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>"); echo ("<br><a href=../products/info.php?scode=$code><span class=fs13>$name</span></a>"); echo("</td>"); // increment the counter $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1333976 Share on other sites More sharing options...
microbert Posted April 4, 2012 Author Share Posted April 4, 2012 Hi, I have tried applying your code into my website and now I have another problem now it is only displaying 2 items instead of 8. here is a link to the problem. http://bitsandbytesmalta.host56.com/products/info.php?scode=UDPT1010 Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1334306 Share on other sites More sharing options...
scootstah Posted April 4, 2012 Share Posted April 4, 2012 I need your code, not a link. Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1334312 Share on other sites More sharing options...
microbert Posted April 4, 2012 Author Share Posted April 4, 2012 I have managed to solve the problem that was showing only two items, now I'd like to make it to show 5 items on 1 line and then continue on another line. this the code: // define the number of columns per row $cols = 5; // start the counter $count = 0; while ($row = mysql_fetch_array($retd)) { $code = $row["code"]; $name = $row["name"]; if ($count % $cols == 0) { if ($c > 0) { echo '</tr>'; } echo '<tr>'; } echo("<td width=150 align=center>"); echo ("<a href=../products/info.php?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>"); echo ("<br><a href=../products/info.php?scode=$code><span class=fs13>$name</span></a>"); echo("</td>"); //increment the counter $count++; } Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1334315 Share on other sites More sharing options...
scootstah Posted April 4, 2012 Share Posted April 4, 2012 I think your problem is here: if ($c > 0). It should be if ($count > 0). Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1334321 Share on other sites More sharing options...
microbert Posted April 4, 2012 Author Share Posted April 4, 2012 I have changed that before your reply because I too thought that it was the problem but still the same problem. Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1334331 Share on other sites More sharing options...
microbert Posted April 10, 2012 Author Share Posted April 10, 2012 Hi, does anyone have an idea of what should I try now? this is my final updated code: // define the number of columns per row $cols = 5; // start the counter $counter = 0; while ($row = mysql_fetch_array($retd)) { $code = $row["code"]; $name = $row["name"]; if ($counter % $cols == 0) { if ($counter > 0) { echo '</tr>'; } echo '<tr>'; } echo("<td width=150 align=center>"); echo ("<a href=../products/info.php?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>"); echo ("<br><a href=../products/info.php?scode=$code><span class=fs13>$name</span></a>"); echo("</td>"); //increment the counter $counter++; Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1335982 Share on other sites More sharing options...
ras1986 Posted April 10, 2012 Share Posted April 10, 2012 I don't see a problem.. I've tried your code and it generates 5 columns just like you wanted.. Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1335985 Share on other sites More sharing options...
microbert Posted April 10, 2012 Author Share Posted April 10, 2012 On my end it is generating this: http://bitsandbytesmalta.host56.com/products/info.php?scode=UDPT1010 Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1335988 Share on other sites More sharing options...
ras1986 Posted April 10, 2012 Share Posted April 10, 2012 Is your $counter incremented inside the while loop? Because I can't see a closing for the while loop. Make sure its inside the loop. Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1336006 Share on other sites More sharing options...
microbert Posted April 10, 2012 Author Share Posted April 10, 2012 sorry about that I had the } missing, but in my original code it is there and yes the $counter++ is in the while loop. Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1336009 Share on other sites More sharing options...
ras1986 Posted April 10, 2012 Share Posted April 10, 2012 Would you mind posting the code for the full page? I tried the code you posted before and it works fine for me. Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1336046 Share on other sites More sharing options...
microbert Posted April 11, 2012 Author Share Posted April 11, 2012 Hi, I am still trying to solve this problem, so if there are someone out there that have an idea of how can I solve it, please let me know i really need this. this is my latest version of the code: // define the number of columns per row $cols = 5; // start the counter $counter = 0; while ($row = mysql_fetch_array($retd)) { $code = $row["code"]; $name = $row["name"]; if ($counter % $cols == 0) { if ($counter > 0) { echo '</tr>'; } echo '<tr>'; } echo("<td width=150 align=center>"); echo ("<a href=../products/info.php?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>"); echo ("<br><a href=../products/info.php?scode=$code><span class=fs13>$name</span></a>"); echo("</td>"); //increment the counter $counter++; } This is a link to the problem that I have: http://bitsandbytesmalta.host56.com/products/info.php?scode=UDPT1010 The related items need to be 5 in every line. thanks in advance for your help. Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1336288 Share on other sites More sharing options...
scootstah Posted April 11, 2012 Share Posted April 11, 2012 I find it hard to believe that that is the code on your link; because there's no error with that snippet. It should be creating a new row every 5th iteration but your live example creates one on every iteration, which is not possible given the above snippet. I've tested it again and it works without issue for me. You need to give us the exact code for the link you have provided. Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1336392 Share on other sites More sharing options...
microbert Posted April 11, 2012 Author Share Posted April 11, 2012 I am attaching the php file of that page. 18047_.php Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1336399 Share on other sites More sharing options...
scootstah Posted April 11, 2012 Share Posted April 11, 2012 That code is a nightmare. I suspect your problem is here: $SQL = " SELECT * FROM list "; $SQL = $SQL . " WHERE code = '$pieces[$i]' "; $retd = mysql_db_query($db, $SQL, $cid); if (!$retd) { echo( mysql_error()); } else { // define the number of columns per row $cols = 5; // start the counter $counter = 0; while ($row = mysql_fetch_array($retd)) { $code = $row["code"]; $name = $row["name"]; /*if ($counter % $cols == 0) { if ($counter > 0) { echo '</tr>'; } echo '<tr>'; }*/ if ($counter % $cols == 0) { if ($counter > 0) { echo '</tr>'; } echo '<tr>'; } echo("<td width=150 align=center>"); echo ("<a href=../products/info.php?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>"); echo ("<br><a href=../products/info.php?scode=$code><span class=fs13>$name</span></a>"); echo("</td>"); //increment the counter $counter++; } } I didn't dig too deeply but I would venture a guess that you are only returning one row at a time. You need to clean up this code a lot. Running queries in nested loops like that is a bad, bad thing. You can most likely gather the same data in a single query. Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1336403 Share on other sites More sharing options...
microbert Posted April 12, 2012 Author Share Posted April 12, 2012 I thought so that the problem is somewhere there but could not find the problem. About the code I know that it is a nightmare but it was not written by me and I am not that good in coding php, so it is taking me a lot of time to figure what to change and how to change it. Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1336609 Share on other sites More sharing options...
scootstah Posted April 12, 2012 Share Posted April 12, 2012 How many rows are returned with that query each time? Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1336644 Share on other sites More sharing options...
microbert Posted April 12, 2012 Author Share Posted April 12, 2012 it is diplaying them 1 in each row I wanted to make 5 columns and then a new row. here you can see the result: http://bitsandbytesmalta.host56.com/products/info.php?scode=UDPT1010 Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1336687 Share on other sites More sharing options...
scootstah Posted April 12, 2012 Share Posted April 12, 2012 That's not what I meant. Put this echo 'Rows returned: ' . mysql_num_rows($retd) . '<br />'; after this $SQL = " SELECT * FROM list "; $SQL = $SQL . " WHERE code = '$pieces[$i]' "; $retd = mysql_db_query($db, $SQL, $cid); And then post the output. Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1336690 Share on other sites More sharing options...
microbert Posted April 13, 2012 Author Share Posted April 13, 2012 Sorry about that. This is the result: Rows returned: 1 Rows returned: 1 Rows returned: 1 Rows returned: 1 Rows returned: 1 Rows returned: 1 Rows returned: 1 Rows returned: 1 Rows returned: 0 there is a picture also attached Quote Link to comment https://forums.phpfreaks.com/topic/260259-display-data-from-mysql/#findComment-1336941 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.