开发者

PHP Pagination Link Generation

开发者 https://www.devze.com 2023-03-24 12:10 出处:网络
I have this script below which i found on SO to generate pagination and broadly speaking, its working great, however because its a cut and paste job from me, i don\'t understand how to actually genera

I have this script below which i found on SO to generate pagination and broadly speaking, its working great, however because its a cut and paste job from me, i don't understand how to actually generate the links which are echoed in the script with the variable $pagination.

What it echos is:

1< a href="index.php?page=2">2< a href="index.php?page=3">3< a hr_ef="?page=2"> Next 

None of which are working (clickable) links, and i also want to be able to style them so would rather output them in HTML, rather than a php echo, something like:

<p><?php 1< a href="index.php?page=2">2< a href="index.php?page=3">3< a hr_ef="?page=2"> Next ?> </p>

Below is the script i'm using:

<?php
/* Set current, prev and next page */
$page = (!isset($_GET['page']))? 1 : $_GET['page']; 
$prev = ($page - 1);
$next = ($page + 1);

/* Max results per page */
$max_results = 10;

/* Calculate the offset */
$from = (($page * $max_results) - $max_results);

/* Query the db for total results.*/
$result = mysql_query("...");
$total_results = mysql_num_rows($result);

$total_pages = ceil($total_results / $max_results);

$pagination = '';

/* Create a PREV link if there is one */
if($page > 1)
{
$pagination .= '< a href="?page='.$prev.'">Previous</a> ';
}

/* Loop through the total pages */
for($i = 1开发者_如何学编程; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
    $pagination .= $i;
}
else
{
    $pagination .= '< a href="index.php?page='.$i.'">'.$i.'</a>';
}
}

/* Print NEXT link if there is one */
if($page < $total_pages)
{
$pagination .= '< a hr_ef="?page='.$next.'"> Next</a>';
}

/* Below is how you query the db for ONLY the results for the current page */
$query ="SELECT * FROM ... LIMIT $from, $max_results";

$result=mysql_query($query) or die(mysql_error());
$rsjobinfo=mysql_fetch_assoc($result);

do {?>

<div>
[Individual Row Output]
</div>
<?php } while ($rsjobinfo=mysql_fetch_assoc($result));

echo $pagination;
?> 

Can someone help? I imagine its a small fix but as always, would appreciate a kick in the right direction.

Thanks Dan


Maybe it's just an editing error, but in your output the <a>-tags don't seem to be closed again. Also, there should be no space like < a> at the beginning of the tag. And < a hr_ef= ... is obviously wrong.

In order to style them, you can add a class attribute to the tags while building the string and do the style-stuff in css.

0

精彩评论

暂无评论...
验证码 换一张
取 消