Click to See Complete Forum and Search --> : php paging


katiepie
01-30-2006, 04:32 AM
I have pages and pages of thumbnails that I would like to split up with next and previous links. Although I have found a number of tutorials on this subject none are making a whole lot of sense to me (am new to php/mysql).
Can anyone recommend a good tutorial for this as Im really close to giving up!
Thanks!

bathurst_guy
01-30-2006, 11:29 AM
I don't know of a tutorial, but I do know how it can be done. How is your information currently stored? In a db?

katiepie
01-30-2006, 12:39 PM
Im using mysql database, currently using the following to display the results which works perfectly apart from having to scroll down the pages..
Any help would be appreciated!

$type = "deep-sea animals";
$query = "SELECT * FROM ocean WHERE gallery LIKE '%$type%'";
$result = mysql_query($query)
or die ("Couldn't execute query.");

echo "<br><br><table align='center' cellspacing='7'>";
echo "<tr>";
$count = 0;

while ($row = mysql_fetch_array($result))
{
if ($count==4) {
echo "<tr>";
$count = 0;
}
$count++;
extract($row);
echo "<td class='td_thumb' align='center'><a href='data.php?id=$id'><img src='images/thumbs/{$row['Pix']}' border='0' alt='$ComName'></a></td>";
if ($count==4) {
echo "</tr>";
}
}

if ($count<4) {
$remain = 4 - $count;
for ($i=1; $i<=$remain; $i++) {
echo "<td>&nbsp;</td>";
}
echo "</tr>";
}

echo "</table>

bathurst_guy
01-30-2006, 12:51 PM
I havn't tested it, so let me know of errors$type = "deep-sea animals";
$show = 12;#returns 12 results at a time, choose in multiple of 4

$start = (isset($_GET[start]))?$_GET[start]:0;

$howmany = mysql_query("COUNT(*) FROM ocean WHERE gallery LIKE '%$type%'");
$query = "SELECT * FROM ocean WHERE gallery LIKE '%$type%' LIMIT $start, $show";
$result = mysql_query($query)
or die ("Couldn't execute query.");

echo "<br><br><table align='center' cellspacing='7'>";
echo "<tr>";
$count = 0;

while ($row = mysql_fetch_array($result))
{
if ($count==4) {
echo "<tr>";
$count = 0;
}
$count++;
extract($row);
echo "<td class='td_thumb' align='center'><a href='data.php?id=$id'><img src='images/thumbs/{$row['Pix']}' border='0' alt='$ComName'></a></td>";
if ($count==4) {
echo "</tr>";
}
}

if ($count<4) {
$remain = 4 - $count;
for ($i=1; $i<=$remain; $i++) {
echo "<td>&nbsp;</td>";
}
echo "</tr>";
}

echo "</table>";
if($howmany > $start){
$next = $start + $show;
$prev = $start - $show;
echo '<a href="'.$_SERVER['PHP_SELF'].'?start='.$prev.'">Previous '.$show.'</a> | <a href="'.$_SERVER['PHP_SELF'].'?start='.$next.'">Next '.$show.'</a>';
}

katiepie
01-30-2006, 01:02 PM
wow, thank you - it is showing the correct number of results, however the 'next/previous' links arn't showing?

bathurst_guy
01-30-2006, 01:11 PM
try this one, it should at least show some more information$type = "deep-sea animals";
$show = 12;#returns 12 results at a time, choose in multiple of 4

$start = (isset($_GET[start]))?$_GET[start]:0;

$howmany = mysql_query("COUNT(*) FROM ocean WHERE gallery LIKE '%$type%'");
$query = "SELECT * FROM ocean WHERE gallery LIKE '%$type%' LIMIT $start, $show";
$result = mysql_query($query)
or die ("Couldn't execute query.");

echo "<br><br><table align='center' cellspacing='7'>";
echo "<tr>";
$count = 0;

while ($row = mysql_fetch_array($result))
{
if ($count==4) {
echo "<tr>";
$count = 0;
}
$count++;
extract($row);
echo "<td class='td_thumb' align='center'><a href='data.php?id=$id'><img src='images/thumbs/{$row['Pix']}' border='0' alt='$ComName'></a></td>";
if ($count==4) {
echo "</tr>";
}
}

if ($count<4) {
$remain = 4 - $count;
for ($i=1; $i<=$remain; $i++) {
echo "<td>&nbsp;</td>";
}
echo "</tr>";
}

echo "</table>";

$total = $start + $show;

if($howmany > $total){
$next = $start + $show;
$prev = $start - $show;
echo '<p><a href="'.$_SERVER['PHP_SELF'].'?start='.$prev.'">Previous '.$show.'</a> | <a href="'.$_SERVER['PHP_SELF'].'?start='.$next.'">Next '.$show.'</a></p>';
}
echo "<p>Showing records $start to $total of $howmany</p>";
}

katiepie
01-30-2006, 01:27 PM
um, now nothing at all is showing up! Sorry Im a bit useless!

bathurst_guy
01-30-2006, 01:32 PM
nah thats fine, its my fault, im not thinking clearly, its 6:30am here and i havnt slept :)
<?
$type = "deep-sea animals";
$show = 12;#returns 12 results at a time, choose in multiple of 4

$start = (isset($_GET[start]))?$_GET[start]:0;
$type = (isset($_POST[type]))?$_POST[type]:$_GET[type];

$howmany = mysql_query("COUNT(*) FROM ocean WHERE gallery LIKE '%$type%'");
$query = "SELECT * FROM ocean WHERE gallery LIKE '%$type%' LIMIT $start, $show";
$result = mysql_query($query)
or die ("Couldn't execute query.");

echo "<br><br><table align='center' cellspacing='7'>";
echo "<tr>";
$count = 0;

while ($row = mysql_fetch_array($result)){
if ($count==4) {
echo "<tr>";
$count = 0;
}
$count++;
extract($row);
echo "<td class='td_thumb' align='center'><a href='data.php?id=$id'><img src='images/thumbs/{$row['Pix']}' border='0' alt='$ComName'></a></td>";
if ($count==4) {
echo "</tr>";
}
}

if ($count<4) {
$remain = 4 - $count;
for ($i=1; $i<=$remain; $i++) {
echo "<td>&nbsp;</td>";
}
echo "</tr>";
}

echo "</table>";

$total = $start + $show;

if($howmany > $total){
$next = $start + $show;
$prev = $start - $show;
echo '<a href="'.$_SERVER['PHP_SELF'].'?start='.$prev.'&amp;type='.$type.'">Previous '.$show.'</a> | <a href="'.$_SERVER['PHP_SELF'].'?start='.$next.'&amp;type='.$type.'">Next '.$show.'</a>';
}

echo "<p>Showing images $start to $total of $howmany that match '$type'</p>";
?>

katiepie
01-30-2006, 05:05 PM
thats ok, I really appreciate your help! I still cant get it to work (sorry!) Its displaying the right results but just has the following at the bottom?

Showing images 0 to 16 of that match ''

I can now understand what you've done, but cant figure out whats wrong!
thanks again!

bathurst_guy
01-30-2006, 10:50 PM
Ok I see why, see this line$type = (isset($_POST[type]))?$_POST[type]:$_GET[type]; add this in front of it, like soif(!isset($type))
$type = (isset($_POST[type]))?$_POST[type]:$_GET[type];

katiepie
01-31-2006, 12:39 PM
still having problems!.. its still displaying correct number of results and prints:
Showing images 0 to 16 of that match 'deep-sea animals'
but still no prev/next buttons? sorry!

bathurst_guy
01-31-2006, 08:37 PM
AHA!!!
$howmany = mysql_query("SELECT COUNT(*) FROM ocean WHERE gallery LIKE '%$type%'");

katiepie
02-01-2006, 09:55 AM
sorry its me again! Ok my code now reads as follows, displaying 12 of around 40 results, with the text " Showing images 0 to 16 of Resource id #3 that match 'deep-sea animals' " at the bottom, still no prev/next buttons.. Ive been staring at it for ages but still cant see the problem??
(and thank you for all the help!)

Code:

<?php
mysql_connect etc...

mysql_select_db etc...

$type = "deep-sea animals";
$show = 12;#returns 12 results at a time, choose in multiple of 4

$start = (isset($_GET[start]))?$_GET[start]:0;
if(!isset($type))
$type = (isset($_POST[type]))?$_POST[type]:$_GET[type];

$howmany = mysql_query("SELECT COUNT(*) FROM ocean WHERE gallery LIKE '%$type%'");
$query = "SELECT * FROM ocean WHERE gallery LIKE '%$type%' LIMIT $start, $show";
$result = mysql_query($query)
or die ("Couldn't execute query.");

echo "<br><br><table align='center' cellspacing='7'>";
echo "<tr>";
$count = 0;

while ($row = mysql_fetch_array($result)){
if ($count==4) {
echo "<tr>";
$count = 0;
}
$count++;
extract($row);
echo "<td class='td_thumb' align='center'><a href='data.php?id=$id'><img src='images/thumbs/{$row['Pix']}' border='0' alt='$ComName'></a></td>";
if ($count==4) {
echo "</tr>";
}
}

if ($count<4) {
$remain = 4 - $count;
for ($i=1; $i<=$remain; $i++) {
echo "<td>&nbsp;</td>";
}
echo "</tr>";
}

echo "</table>";

$total = $start + $show;

if($howmany > $total){
$next = $start + $show;
$prev = $start - $show;
echo '<a href="'.$_SERVER['PHP_SELF'].'?start='.$prev.'&amp;type='.$type.'">Previous '.$show.'</a> | <a href="'.$_SERVER['PHP_SELF'].'?start='.$next.'&amp;type='.$type.'">Next '.$show.'</a>';
}

echo "<p>Showing images $start to $total of $howmany that match '$type'</p>";
?>

bathurst_guy
02-02-2006, 12:59 AM
Try this <?php
mysql_connect etc...

mysql_select_db etc...

$type = "deep-sea animals";
$show = 12;#returns 12 results at a time, choose in multiple of 4

$start = (isset($_GET[start]))?$_GET[start]:0;
if(!isset($type))
$type = (isset($_POST[type]))?$_POST[type]:$_GET[type];

$howmany = mysql_query("SELECT COUNT(*) FROM ocean WHERE gallery LIKE '%$type%'");
$howmany = mysql_fetch_row($howmany);
$query = "SELECT * FROM ocean WHERE gallery LIKE '%$type%' LIMIT $start, $show";
$result = mysql_query($query)
or die ("Couldn't execute query.");

echo "<br><br><table align='center' cellspacing='7'>";
echo "<tr>";
$count = 0;

while ($row = mysql_fetch_array($result)){
if ($count==4) {
echo "<tr>";
$count = 0;
}
$count++;
extract($row);
echo "<td class='td_thumb' align='center'><a href='data.php?id=$id'><img src='images/thumbs/{$row['Pix']}' border='0' alt='$ComName'></a></td>";
if ($count==4) {
echo "</tr>";
}
}

if ($count<4) {
$remain = 4 - $count;
for ($i=1; $i<=$remain; $i++) {
echo "<td>&nbsp;</td>";
}
echo "</tr>";
}

echo "</table>";

$total = $start + $show;

if($howmany[0] > $total){
$next = $start + $show;
$prev = $start - $show;
echo '<a href="'.$_SERVER['PHP_SELF'].'?start='.$prev.'&amp;type='.$type.'">Previous '.$show.'</a> | <a href="'.$_SERVER['PHP_SELF'].'?start='.$next.'&amp;type='.$type.'">Next '.$show.'</a>';
}

echo "<p>Showing images $start to $total of $howmany[0] that match '$type'</p>";
?>