Jump to content

Recommended Posts

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>';
}
?>

post-172611-0-95200600-1408546114_thumb.jpg

 

Thanks

Edited by JimmyLeaman
Link to comment
https://forums.phpfreaks.com/topic/290555-php-displaying-mysqli-array/
Share on other sites

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.

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>

Assuming that there are always 6 images and you want the images to always be positioned and sized the same, you could:

  1. Read the values into an array
  2. Output the images into a table
  3. 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>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.