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 */
?> 

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.