Forum Moderators: coopster

Message Too Old, No Replies

help with pagination

php mysql pagination

         

cuce

7:52 pm on Jun 5, 2008 (gmt 0)

10+ Year Member



I've been trying to figure out pagination, I just followed a tutorial on this site and it didn't quite work for me, can someone please help me determine where I went wrong?

error:Page number is not defined!

here's the code i got that error from:

<?
if(empty($_GET['page'])) {
echo 'Page number is not defined!';
exit();
}
else if(!preg_match("/^([0-9])+$/",$_GET['page'])) {
echo 'Page is not a valid value!';
exit();
}
$num_per_page = (empty($_GET['num']))? 10 : $_GET['num'];
if(!preg_match("/^([0-9])+$/",$num_per_page)) {
echo 'Num is not a valid value!';
exit();
}
$page = $_GET['page'];

include_once($_SERVER['DOCUMENT_ROOT'] ."/assets/includes/sqlconfig.php") ; //vconnection (no problems here)

$qPage = (($page-1)*$num_per_page);

$sql = "SELECT * FROM dir WHERE approved = 'yes' ORDER BY name ASC ".$qPage.",".($num_per_page+1);
$result = mysql_query($sql);
$num = mysql_num_rows($result);
if($num == 0) {
echo 'No more results!';
exit();
}

for($i = 0; $i < $num_per_page; $i++) {
include($_SERVER['DOCUMENT_ROOT'] ."/assets/includes/fieldvars.php") ;
$row = mysql_fetch_array($result);
?>

<p><strong><?php echo $name ?></strong> <?php echo $address ?><br/>
<?php echo $description ?><br/>
<em>phone: <?php echo $phone ?> ¦ tollfree: <?php echo $tollfree ?> ¦ fax: <?php echo $fax ?> <br/>
website: <?php echo $website ?> ¦ email: <?php echo str_replace("@", "<img src=\"/assets/images/e.gif\" alt=\"\">", $email) ?></em></p>

<?php
}
mysql_close($dbh);
if($num > $num_per_page) {echo '<a href="index.php?page='.($page+1).'&num='.$num_per_page.'">Next Page</a>';}
echo '<br/>';
if($page > 1) {echo '<a href="index.php?page='.($page-1).'&num='.$num_per_page.'">Previous Page</a>';}
?>

Receptional Andy

8:00 pm on Jun 5, 2008 (gmt 0)



The code is expecting you to access it via a url like www.example.com/pagination.php?page=1 and will return the error if there is no page= variable set. Call the page with page=1 and it should work.

In addition, I'd suggest that page one shouldn't require a number in the first place. You could achieve this by setting a default page in the first few lines, rather than an error:


if(empty($_GET['page'])) {
# two lines below commented out
#echo 'Page number is not defined!';
#exit();
# if no page is specified, use page 1.
$_GET['page']=1;
}

[edited by: Receptional_Andy at 8:09 pm (utc) on June 5, 2008]

d40sithui

8:02 pm on Jun 5, 2008 (gmt 0)

10+ Year Member



your script fell into the first condition, which means that there is no certain GET variable. try reloading your page with this parameter.

index.php?page=1

[edit]
or do what was suggested above =p
[/edit]

Receptional Andy

8:12 pm on Jun 5, 2008 (gmt 0)



Actually my original code ($page=1) wouldn't work, since I didn't notice $page would be reset by the later code $page=$_GET['page']. I don't like meddling with $_GET directly, but I edited the above to do so in the interest of simplicity.

cuce

8:40 pm on Jun 5, 2008 (gmt 0)

10+ Year Member



Well that got rid of my first error...

but now i get these errors:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/gogab/public_html/full.php on line 38
No more results!
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/gogab/public_html/full.php on line 46

here's my code

<?
if(empty($_GET['page'])) {
$_GET['page']=1;
}
else if(!preg_match("/^([0-9])+$/",$_GET['page'])) {
echo 'Page is not a valid value!';

}
$num_per_page = (empty($_GET['num']))? 10 : $_GET['num'];
if(!preg_match("/^([0-9])+$/",$num_per_page)) {
echo 'Num is not a valid value!';

}
$page = $_GET['page'];

include_once($_SERVER['DOCUMENT_ROOT'] ."/assets/includes/sqlconfig.php") ;

$qPage = (($page-1)*$num_per_page);

$sql = "SELECT * FROM dir WHERE approved = 'yes' ORDER BY name ASC ".$qPage.",".($num_per_page+1);
$result = mysql_query($sql);
$num = mysql_num_rows($result);// line 38
if($num == 0) {
echo 'No more results!';

}

for($i = 0; $i < $num_per_page; $i++) {
include($_SERVER['DOCUMENT_ROOT'] ."/assets/includes/fieldvars.php") ;
$row = mysql_fetch_array($result) ;//line 46
?>

<p><strong><?php echo $name ?></strong> <?php echo $address ?><br/>
<?php echo $description ?><br/>
<em>phone: <?php echo $phone ?> ¦ tollfree: <?php echo $tollfree ?> ¦ fax: <?php echo $fax ?> <br/>
website: <?php echo $website ?> ¦ email: <?php echo str_replace("@", "<img src=\"/assets/images/e.gif\" alt=\"\">", $email) ?></em></p>

<?php
}
mysql_close($dbh);
if($num > $num_per_page) {echo '<a href="index.php?page='.($page+1).'&num='.$num_per_page.'">Next Page</a>';}
echo '<br/>';
if($page > 1) {echo '<a href="index.php?page='.($page-1).'&num='.$num_per_page.'">Previous Page</a>';}
?>

any hwlp would be hugely appreciated thanks!

d40sithui

3:11 pm on Jun 6, 2008 (gmt 0)

10+ Year Member



you're getting this error because your mysql resource is not valid. this can be traced back to your actual query. print it out and see what you have. as andy pointed out a few posts back, $page is resetted after your checks, so it might still be empty. or you forgot "limit" in your query.

try


$sql = "SELECT * FROM dir WHERE approved = 'yes' ORDER BY name ASC LIMIT ".$qPage.",".($num_per_page+1);