JimmyLeaman Posted August 20, 2014 Share Posted August 20, 2014 (edited) Hey All. I'm currently using the following code to source some information from a Mysql Database. I've limited the results to 6 Values. I'm using the WHILE function to display a table with the results populated in. As you'll see from the attached picture, the WHILE function is causing 5 tables to be created, each with 5 of the same image displayed. How do I go about displaying only 1 table with the 5 different pictures displayed in? <?php $result = mysqli_query($con, "SELECT * FROM photo ORDER BY id DESC LIMIT 6"); while($row = mysqli_fetch_array($result)){ echo '<table> <tr><td class="left"> <a href="photo.php?id='.$row['id'].'"><img src="images/'.$row['id'].'.jpg" height="232" alt=""/></a> </td> <td class="center"> <a href="photo.php?id='.$row['id'].'"><img src="images/'.$row['id'].'.jpg" height="112" alt=""/></a> <a href="photo.php?id='.$row['id'].'"><img src="images/'.$row['id'].'.jpg" height="112" alt=""/></a> </td> <td class="right"> <a href="photo.php?id='.$row['id'].'"><img src="images/'.$row['id'].'.jpg" height="72" alt=""/></a> <a href="photo.php?id='.$row['id'].'"><img src="images/'.$row['id'].'.jpg" height="72" alt=""/></a> <a href="photo.php?id='.$row['id'].'"><img src="images/'.$row['id'].'.jpg" height="72" alt=""/></a> </td></tr></table>'; } ?> Thanks Edited August 20, 2014 by JimmyLeaman Quote Link to comment https://forums.phpfreaks.com/topic/290555-php-displaying-mysqli-array/ Share on other sites More sharing options...
ginerjm Posted August 20, 2014 Share Posted August 20, 2014 You have a loop in which you create a table and show the exact same information 5 times. What do you think your output will be??? While it may be a small misnomer, the mysqli_fetch_array fetches an "array of the fields of one record" of the results. Not an array of all the records. Therefore your loop is processing just one result record at a time. Consequently you have to go thru the loop 6 times(?) to get all the records. Thus it would make sense to only create the table one time - before the loop begins, with the end tag after the loop ends. Of course a quick read of this function in the php manual might have made this immediately clear. Quote Link to comment https://forums.phpfreaks.com/topic/290555-php-displaying-mysqli-array/#findComment-1488408 Share on other sites More sharing options...
JimmyLeaman Posted August 20, 2014 Author Share Posted August 20, 2014 Ok, thanks for your response. Unfortunately I don't have the same degree of knowledge at the moment, could you maybe point me in the right direction? I know I could use something like this which would stop 5 tables being shown, however each image needs to have it's own styling, image size.. etc. <table><tr> <?php while($row=mysqli_fetch_array($result)){ echo ' <td><img src="images/'.$row['id'].'.jpg"></td> '; } ?> </tr><table> Quote Link to comment https://forums.phpfreaks.com/topic/290555-php-displaying-mysqli-array/#findComment-1488410 Share on other sites More sharing options...
ginerjm Posted August 20, 2014 Share Posted August 20, 2014 That code is correct. Where are you getting the attributes you speak of? Are they part of the query results? If so , just use them to form your img tag. If not, how do you expect to assign them to each image? Quote Link to comment https://forums.phpfreaks.com/topic/290555-php-displaying-mysqli-array/#findComment-1488413 Share on other sites More sharing options...
cyberRobot Posted August 20, 2014 Share Posted August 20, 2014 Assuming that there are always 6 images and you want the images to always be positioned and sized the same, you could: Read the values into an array Output the images into a table Use CSS to position and size the images Here is a quick and crude example: <?php $images = array(); $result = mysqli_query($con, "SELECT * FROM photo ORDER BY id DESC LIMIT 6"); while($row = mysqli_fetch_assoc($result)) { $images[] = '<a href="photo.php?id='.$row['id'].'"><img src="images/'.$row['id'].'.jpg" alt=""/></a>'; } ?> <style type="text/css"> table .left { width:200px; } table .left img { width:200px; height:232px; } table .center { width:100px; } table .center img { width:100px; height:112px; } table .right { width:50px; } table .right img { width:50px; height:72px; } </style> <table> <tr> <td class="left"><?php print $images[0] ?></td> <td class="center"><?php print $images[1] . $images[2]; ?></td> <td class="right"><?php print $images[3] . $images[4] . $images[5]; ?></td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/290555-php-displaying-mysqli-array/#findComment-1488415 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.