Jump to content

outputting photo's in a table


coolphpdude

Recommended Posts

Hi guys,

 

I touched on this a while back but never got it sorted. Im just wondering how to do this. Imagine these 2 scenario's.

 

1) You have 13 pics to display in a table... you output these 1 after another in a while loop vertically. The code would simply output the pictures 1 after another vertically until 13 pics were output (1 new row after another)

 

...This scenario is fine, i understand it and have used it. Nice and easy.

 

But how about this one...

 

2) You need to output the pictures horizontally with limited horizontal space... Then what? Say i have got a table 800px wide and i can only fit 3 pictures on every row, i would need a table consisting of 3 columns and 5 rows. But i am still not sure how to go about doing this or how to insert each picture one after the other. Also what happens if you dont kno how many results would be returned... I could do a query that returns 53 pictures or 999 pics... What i am trying to get at is how do i output all of my pictures without using the verticle, one after another, method.

 

Im quite stuck with this one!!

 

 

Link to comment
Share on other sites

What I would do is use an image resize script, which is available on this forum in the repository. Then keep the rows a static size and put the photo thumbs in it. Here is a script which will do it. Just have to substitute your image name in between the <td> tags.

 

<html>
<table width="800" border="0" cellspacing="0" cellpadding="10" align="center">

<?php
require('config.php');
include('includes/mysql.php');

//set number of columns and initial column number
$numcols = 4; // how many columns to display
$numcolsprinted = 0; // no of columns so far

// get the results to be displayed
$query = "SELECT imagename FROM mytable ";
$mysql_result = mysql_query($query) or die (mysql_error());

// get each row
while($myrow = mysql_fetch_row($mysql_result))
{

//get data 
$pic = $myrow[0];


if ($numcolsprinted == $numcols) {
print "</tr>\n<tr>\n";
$numcolsprinted = 0;
}

// output row from database
echo "<td width=\"25%\"><img src=\"images/$pic_name\" /></td>\n";

// bump up row counter
$numcolsprinted++;

} // end while loop

$colstobalance = $numcols - $numcolsprinted;
for ($i=1; $i<=$colstobalance; $i++) {

}
print "<TD></TD>\n";
?>
</table>
</html>

 

Ray

 

Link to comment
Share on other sites

as long as the images are fixed in size it wouldnt be hard to create a table to do 3 per row

 

it would be as simple as

 

		
// print HTML table
	echo '<tr>';
	echo '<td>whatever text if any</td>';
	echo '</tr>';

	// iterate over record set
	// print each field
	while($row = mysql_fetch_row($result))
	{
	 echo '<tr>';
	 echo '<td></td>';
	 echo '<td></td>';
	 echo '<td></td>';
	 echo '</tr>';
	}
}
else
{
	// print error message
	echo '<tr>';
	echo '<td>No pics found</td>';
	echo '</tr>';
}

// once processing is complete
// free result set
mysql_free_result($result);
// close connection to MySQL server
mysql_close($connection);	

 

then just use your code. Im new at php but would image that would work. I get mysql data and insert it into columns then start a new row. Might have to use some count variable. Dont know what your code is.

 

If you dont have a standard size picture use the code from the previous post to resize then insert into a 3 column loop like i showed

Link to comment
Share on other sites

Math is your friend.

I suck at math, but this works:

 

You'll have an IF statement in the loop, something like this:

 

if ($picCount % 3 === 0) 
{
   // echo '</tr><tr>';
   // start a new row!
}

 

Assuming $picCount is the number of the current picture, and you'd place that IF statement near the end of the loop.

 

Something like that would check to see if the REMAINDER of  the Count, Divided by the Number of Pictures So Far is ZERO.  If the number is Zero, obviously you have a number evenly divisible by 3 (in this case)... see how that works?

Link to comment
Share on other sites

I've got set pic sizes and 3 pics would fit comfortably on every row...

 

So basically do a pic count and row count?? Ignore the syntax but something as follows for quickness??

 

<table>
<tr>
// start the while loop within the row
while ($imagesexist) 
{
<td>$image<td>
// if the the $imageexist counter exceeds 3 close off that row and begin new row
if ($imageexist is a multiple of 3)
{
</tr>
<tr>
}

// end while loop
}

//close table
</table>

 

would that work???

Link to comment
Share on other sites

Take a look at this example. It takes care of partially filled rows at the end:

<?php
$photos = range(0,rand(10,27));
$cols = rand(3,10);

echo 'Photos: ' . count($photos) . '<br>Columns: ' . $cols . '<br>';

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title></title>
</head>

<body>
<table border="1">
<?php
	$tmp = array();
	$tmp2 = array();
	$i = 0;
	foreach($photos as $ind => $v) {
		if ($i % $cols == 0 && !empty($tmp)) {
			$tmp2[] = '<tr><td>' . implode('</td><td>',$tmp) . '</td></tr>';
			$tmp = array();
			$tmp[] = 'image ' .$v;
		}
		else
			$tmp[] = 'image ' . $v;
		$i++;
	}
	if (!empty($tmp))
		$tmp2[] = '<tr><td>' . implode('</td><td>',$tmp) . '</td></tr>';
	echo implode("\n",$tmp2);
?>
</table>


</body>
</html>

 

Ken

Link to comment
Share on other sites

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.