Jump to content

Arranging images into rows


nublet

Recommended Posts

Hi im trying to arrange images into neat rows but im not sure how to go about it. This is the code im using to display pictures at the moment but it just gives me a long column of pictures.

 

<?php
   include("config.inc.php");

$result = mysql_query("SELECT * FROM gallery_photos");
While($row = mysql_fetch_array($result)){
echo "<a href='photos/" . $row['photo_filename'] . "'><img src='photos/tb_" . $row['photo_filename'] . "' border='1' /></a><br />"; 
} 

?> 

 

 

Link to comment
https://forums.phpfreaks.com/topic/142695-arranging-images-into-rows/
Share on other sites

<?php
   include("config.inc.php");

$result = mysql_query("SELECT * FROM gallery_photos");
$i = 0;
While($row = mysql_fetch_array($result)){
if($i == 3) { echo "<br />"; $i = 0; } //change 3 to however many you want per row
echo "<a href='photos/" . $row['photo_filename'] . "'><img src='photos/tb_" . $row['photo_filename'] . "' border='1' /></a>"; 
$i++;
} 

?>

<?php 
include("config.inc.php"); 
$pics_per_row = 5; 
$result = mysql_query("select * from gallery_photos"); 
$inc = 0; 
echo '<table>';
while($row = mysql_fetch_array($result))
{
if($inc%$pics_per_row == 0)
{
echo '<tr>'; 
}
echo '<td><a href="photos/' . $row["photo_filename"] . '"><img src="photos/tb_' . $row["photo_filename"] . '" border="1" /></a><br /></td>';
if($inc%$pics_per_row == 0)
{
echo '<tr>'; 
}
$inc++; 
}
echo '</table>';

Thanks for the quick replies. How would i got about spacing the images evenly and centering them in the page? Is it fine to do that with php or is it better doing things like that with css?

 

xangelo I tried your code but it made the pictures go in a strange order like rows of 1 then 4 then 1 then 3 I coudnt work out why that was happening though.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.