Jump to content

Recommended Posts

I have made my own image gallery which uses pagination and it all works fine.

 

I have set the number of columns and the number of rows per page and if the total number of images are more than can be displayed on the page a link to the next batch of images is displayed.

 

The problem is that it gets the right amount of rows and the right amount of columns but the last column cell on the last row is empty even if there are other images on the next page.

 

<?php 
//the number of columns in the table
define ("NUMCOLS",3);

$db = & new MySQL($host,$dbUser,$dbPass,$dbName);

$sql="Select count(*) FROM `gallery`";

// Perform a query getting back a MySQLResult object
$res = $db->query($sql);

$numrows = $res->fetchrow();
  
if(isset($_GET['pagenum'])?$page = $_GET['pagenum']:$page = 1); 
$entries_per_page = 20;   

$total_pages = ceil($numrows[0]/$entries_per_page); 
$offset = (($page * $entries_per_page) - $entries_per_page);
				   						
$sql="SELECT id,alternate,thumb FROM gallery LIMIT $offset,$entries_per_page";

// Perform a query getting back a MySQLResult object
$result = $db->query($sql);

$err = $result->size();

if($err == 0) {
echo ("No matches met your criteria."); 
} else {
$count = 0;
$counter= 1;
echo "<table border='0' id='gallery'>";
while (list($id,$alternate,$thumb) = $result->fetchrow()) {

	if ($count % NUMCOLS == 0) echo "<tr>\n";  # new row
	echo '<td><div class="img-shadow"><a href="/gallery/image-'.$counter.'"><img src="images/gallery/thumbs/'.$thumb.'" alt="'.$alternate.'" style="border:none" /></a></div></td>';
	$count++;
	$counter++;

	if ($count % NUMCOLS == 0) echo "</tr>";  # end row
}

# end row if not already ended

if ($count % NUMCOLS != 0) {
   while ($count++ % NUMCOLS) echo "<td> </td>";
   echo "</tr>";
}
echo "</table>";

//or after the results
pagination_two($dir,$total_pages,$page);
}
echo "</div>";
?>

I've tried changing the NUMCOLS and the entries per page and i get strange results.  Sometimes it works and other times is misses 2 table cells

Link to comment
https://forums.phpfreaks.com/topic/142880-missing-table-cells-in-image-gallery/
Share on other sites

Here is the html ouput

 

It's adding a non breaking space instead of another image because it thinks there's no more images when there is loads left

 

<h1>Image Gallery</h1><hr />
<div>
<table border='0' id='gallery'>
	<tr>
		<td><a href="/gallery/image-1"><img src="images/gallery/thumbs/honey001.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-2"><img src="images/gallery/thumbs/honey002.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-3"><img src="images/gallery/thumbs/honey003.jpg" alt="" /></a></td>
	</tr>
	<tr>
		<td><a href="/gallery/image-4"><img src="images/gallery/thumbs/honey004.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-5"><img src="images/gallery/thumbs/honey005.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-6"><img src="images/gallery/thumbs/honey006.jpg" alt="" /></a></td>
	</tr>
	<tr>
		<td><a href="/gallery/image-7"><img src="images/gallery/thumbs/honey007.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-8"><img src="images/gallery/thumbs/honey008.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-9"><img src="images/gallery/thumbs/honey009.jpg" alt="" /></a></td>
	</tr>
	<tr>
		<td><a href="/gallery/image-10"><img src="images/gallery/thumbs/honey010.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-11"><img src="images/gallery/thumbs/honey011.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-12"><img src="images/gallery/thumbs/honey012.jpg" alt="" /></a></td>
	</tr>
	<tr>
		<td><a href="/gallery/image-13"><img src="images/gallery/thumbs/honey013.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-14"><img src="images/gallery/thumbs/honey014.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-15"><img src="images/gallery/thumbs/honey015.jpg" alt="" /></a></td>
	</tr>
	<tr>
		<td><a href="/gallery/image-16"><img src="images/gallery/thumbs/honey016.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-17"><img src="images/gallery/thumbs/honey017.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-18"><img src="images/gallery/thumbs/honey018.jpg" alt="" /></a></td>
	</tr>
	<tr>
		<td><a href="/gallery/image-19"><img src="images/gallery/thumbs/honey019.jpg" alt="" /></a></td>
		<td><a href="/gallery/image-20"><img src="images/gallery/thumbs/honey020.jpg" alt="" /></a></td>
		<td> </td>
	</tr>
</table>
<div class="page_numbers"> 
	<ul>
		<li><a class="current">1</a></li>
		<li><a href="/about/jack/2">2</a></li>
		<li><a href="/about/jack/3">3</a></li>
		<li><a href="/about/jack/4">4</a></li>
		<li class="current"><a href="/about/jack/2">Next</a></li> 
		<li class="current"><a href="/about/jack/4">Last</a></li>
	</ul>
</div>
</div>

if you have 20 items per page and 3 columns

it is 6 full rows + 2 items

 

Correcto mundo!

 

You have a total of 21 rows to be displayed, but you are only allowing 20 items to be pulled out per page, make that 21 and you should be fine.

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.