Jump to content

How can i display images in a series of div or in table cell?


colap

Recommended Posts

A rough idea of how to do it - this will display ALL of the images in a table. Ultimately you will most likely want to paginate the results.

 

To test it, simply modify where necessary.

 

<?php
/* connect to db */
include('db.php');

/* create your query */
$query = "SELECT * FROM what_ever_table_name";

/* execute the query */
$result = mysql_query($query);

/* set the max columns for the table */
$max_columns = 4; /* set max columns for table */

/* count total images to be displayed */
$total_cells = mysql_num_rows($result);

/* initially set counter to use in starting and ending rows */
$i = 0; 
?>
<table border ="2">
<?PHP
$total_rows = ceil($total_cells / $max_columns); /* calculate total rows needed */
$junk1 = $total_rows * $max_columns; /* calculate number of empty cells in last row */
$junk2 = $junk1 - $total_cells;
if($junk2==0) { 
	$last_row = "</tr>"; 
}else{ 
	$j = 0; 
	while($j<$junk2){ 
		$last_row = $last_row . "<td></td>"; 
		$j ++; 
	} 
	$last_row = $last_row . "</tr>"; 
}
while($row = mysql_fetch_array($result))) { /* begin looping thru the results */
	if($i == 0){ 
		echo "<tr>"; 
		$i ++; 
	} 
	?>
	/* EDIT THIS LINE TO REFLECT THE ACTUAL NAME OF YOUR DB TABLE FIELD */
	<td><IMG src="<?PHP echo $row['image_field_name_here'; ?>"></td>
	<?PHP 
	$i ++;
	if($i > $max_columns) { /* check if need to close row */
		echo "</tr>"; 
		$i=0; 
	} 
}
echo $last_row . "</table>"; /* clean up last row */
?> 

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.