Log in

View Full Version : pagination help



insanemonkey
10-05-2007, 07:50 PM
ok I have got the code on my webpage but the echo is not working or its not displaying the next page or any numbers... this is what i have for the echo numbers..



<?
include("dbconnect.php");

$pagenumber = 10;
if(!isset($_GET['cursors'])) { $page = 1;}else { $page = (int)$_GET['cursors'];}

$from = (($cursors * $pagenumber) - $pagenumber);
$getnews = mysql_query("select * from db ORDER BY id DESC LIMIT $from,

$pagenumber");
while($r=mysql_fetch_array($getnews)){
extract($r);
echo("
<table width='100%' cellspacing='1' cellpadding='5' bgcolor='#333333' align='center'><tr><td class='title2'><center><font size='4'><a>$news</a></font><BR><BR><img style=\"cursor: url('$url')\" src=\"$img\" width=\"100\"><BR>Body<BR><textarea><style><!--BODY{cursor:url(\"$url\");}--></style><a href=\"http://www.xudas.com\"><img src=\"http://www.xudas.com/images/xudasaffy.gif\" border=\"0\"></a></textarea><br><a style=\"cursor: url('$url')\" href=\"#\">Hover over a Link</a><BR><textarea><style>A:hover{cursor: url(\"$url\");}</style>
<a href=\"http://www.xudas.com\"><img src=\"http://www.xudas.com/images/xudasaffy.gif\"
border=\"0\"></a></textarea><br><br></td></tr></table><BR>
");
}
echo "Pages: ";

$total_results = mysql_fetch_array(mysql_query("SELECT COUNT(*) as num FROM db where

parentid='0'"));
$total_pages = ceil($total_results['num'] / $pagenumber);
if ($cursors > 1) { $prev = ($cursors - 1);
echo "$i";
}for($i = 1;
$i <= $total_pages; $i++) { if ($cursors == $i) {
echo "<a href=\"index.php?cursors=$i\">$i</a> ";
} else {
echo "<a href=\"index.php?cursors=$i\">$i</a> ";
}}
if ($cursors < $total_pages) {
$next = ($cursors + 1);
echo "<a href=\"index.php?cursors=$next\"> Older </a>";}

?>

Thats the complete code that I am useing, and I cant figure out why its not paginating it...

It paginates it but doesn't display the numbers... plz help..

is there anything wrong with that code...

insanemonkey
10-05-2007, 10:28 PM
can anyone help me...

djr33
10-06-2007, 04:25 AM
That code is just plain ugly. Too may line breaks in weird places, tabs are way off, the html output was near unreadable, etc.
So, I rewrote it in a more standard, possible-to-read format.
Please, when asking for help, at least make the code decently formatted. And, 3 hours is not long enough to start bumping the thread. You've been here long enough to know that. Bumping like that just annoys me [and others]. I'm not inclined to hurry, then, certainly.

<?php
include("dbconnect.php");
$pagenumber = 10;
if(!isset($_GET['cursors'])) {
$page = 1;
}
else {
$page = (int)$_GET['cursors'];
}
$from = (($cursors * $pagenumber) - $pagenumber);
$getnews = mysql_query("select * from db ORDER BY id DESC LIMIT $from, $pagenumber");
while($r=mysql_fetch_array($getnews)){
echo <<<OUT
<table width="100%" cellspacing="1" cellpadding="5" bgcolor="#333333" align="center">
<tr>
<td class="title2">
<div align="center">
<font size="4">
<a href="{$r['news']}">{$r['news']}</a>
</font><BR><BR>
<img style="cursor: url('{$r['url']}')" src="$img" width="100"><BR>
Body<BR>
<textarea><style>
<!--BODY{cursor:url("{$r['url']}");}-->
</style>
<a href="http://www.xudas.com"><img src="http://www.xudas.com/images/xudasaffy.gif" border="0"></a></textarea><br>
<a style="cursor: url('{$r['url']}')" href="#">Hover over a Link</a><BR>
<textarea><style>
A:hover{
cursor: url('{$r['url']}');
}
</style>
<a href="http://www.xudas.com"><img src="http://www.xudas.com/images/xudasaffy.gif" border="0"></a></textarea><br><br>
</td>
</tr>
</table><BR>
OUT;
}
echo "Pages: ";
$total_results = mysql_fetch_array(mysql_query("SELECT COUNT(*) as num FROM db where parentid='0'"));
$total_pages = ceil($total_results['num'] / $pagenumber);
if ($cursors > 1) {
$prev = ($cursors - 1);
echo "$i";
}
for($i = 1; $i <= $total_pages; $i++) {
if ($cursors == $i) {
echo "<a href=\"index.php?cursors=$i\">$i</a> ";
}
else {
echo "<a href=\"index.php?cursors=$i\">$i</a> ";
}
}
if ($cursors < $total_pages) {
$next = ($cursors + 1);
echo "<a href=\"index.php?cursors=$next\"> Older </a>";
}
?>

The only things I changed:
1. Formatting.
2. Removed extract()... bad practice, and can end up doing weird things later on. Very hard to track. Used $r['aaa'] instead.
3. Used heredoc for the output, and reformatted the html accordingly, as well as formatting the text better for the textareas.
4. You had some very bizarre tags, mostly <a> tags without any properties, etc. I replaced those with my best guess at what you'd want for values and/or hrefs.
5. Don't use <center>. Replaced with <div align="center">. Also, you should look into CSS for the page, but I'm not going to get into that now.

Now.... what's wrong, specifically?

Do you get an error? What? Which line number?
If not, you will need to use trial and error to track the data.
Use echo/print_r() to track data through the script.
Report the value of the variables/arrays to see where they have the incorrect value.
A crucial place is the mysql query. You can also echo the query to verify it is correct.

insanemonkey
10-06-2007, 04:43 AM
im sorry, and I also made the html part (echo) code so no one else would like try thinking about taking it or I don't know why, sorry about that..

oh sorry about that bumping I won't do it again, im just frustrated with life..

And the code is amazing how you changed it around. Thankyou..

but it didn't work for me.. all I got was a blank page..

djr33
10-06-2007, 04:58 AM
I didn't change anything functional on the script. I just made it more readable, so it is possible to fix it.

As I said, you'll need to do some trial and error output to track the values through the script.

Be sure error reporting is turned on error_reporting(E_ALL); at the top.

If you get no errors and no output, then there may be an open loop, but I don't see anything like that. You should just, if it comes down to it, go line by line to output. Even just outputting echo 'test'; will determine where the script fails, then you can figure out how to best fix that.

These are normal debugging practices and you just need to try them to get used to them. Not difficult, really. Time consuming, if anything.

insanemonkey
10-06-2007, 09:08 AM
Oh yay thankyou for helping me out...

djr33
10-06-2007, 09:40 AM
Sure. Once you find the problem area (I can't do that at all since it would require testing line by line--- and it would be a big time commitment), go ahead and post if you aren't sure how to fix it.