Forum Moderators: coopster

Message Too Old, No Replies

help with paginating

php mysql pagination

         

cuce

5:21 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



Hi there I'm trying to figure out how to turn the code below into a paginated listing of these results:

<?
include_once($_SERVER['DOCUMENT_ROOT'] ."/assets/includes/sqlconfig.php") ;
$sql = "SELECT * FROM dir WHERE approved = 'yes' ORDER BY name ASC";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
include($_SERVER['DOCUMENT_ROOT'] ."/assets/includes/fieldvars.php") ;
?>

<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
};
?>

Any help would be appreciated! Thanks!

eelixduppy

6:06 pm on Jun 4, 2008 (gmt 0)



Here's a nice thread that should get you started: [webmasterworld.com...]

cuce

6:20 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



I tried a few tutorials and I've had no luck getting it to work.

I got this error using that post above:

Parse error: syntax error, unexpected $end in /home/gogab/public_html/full.php on line 68

[edited by: cuce at 7:06 pm (utc) on June 4, 2008]

eelixduppy

6:57 pm on Jun 4, 2008 (gmt 0)



Can we take a look at the code you started to work on, then, instead of starting from scratch? This is a learning process as much as anything else :)

Please only post what code is relevant, however.

cuce

7:07 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



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") ;

$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>';}
?>