Jump to content

[SOLVED] Pagination Help


RealDrift

Recommended Posts

  • Replies 126
  • Created
  • Last Reply

Top Posters In This Topic

Page1= SELECT * FROM goldies WHERE Sender='WeedHunteR' LIMIT 0,10

Page2= SELECT * FROM goldies WHERE Sender='WeedHunteR' LIMIT 7,10

Page3= SELECT * FROM goldies WHERE Sender='WeedHunteR' LIMIT 14,10

 

thought i might tell u the sql queries the script echoes on each page

Link to comment
Share on other sites

Okay, hopefully this is right now.

 

<?php

$username       = $_SESSION['username'];
$limit          = 10;
$query_count    = "SELECT count(*) FROM goldies WHERE Sender='$username'";
$result_count   = mysql_query($query_count) or die("Error: " . mysql_error());
$totalrows      = mysql_result($result_count, 0, 0);
$numofpages     = ceil($totalrows/$limit);
$rowsperpage = ceil($totalrows/$numofpages);

if (isset($_GET['page'])) $page = $_GET['page'];
else $page = 1;

$offset = ($page - 1) * $rowsperpage;
$query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $offset,10";

echo  $query .'<br>';
$result = mysql_query($query) or die("Error: " . mysql_error());

//Exit if no records to display
if (mysql_num_rows($result) == 0) {
    exit("Nothing to Display!");
}

//Create the table of output data
echo "<table>\n";
while ($row = mysql_fetch_array($result)) {
    //Alternate the row background color
    $bgcolor = ($bgcolor == "#737373")?'#737373':'#737373';
    //Display the row of data
    echo "<tr bgcolor=\"$bgcolor\">\n";
    echo "  <td>".$row["Receiver"]."</td>\n";
    echo "  <td>".$row["Amount"]."</td>\n";
    echo "  <td>".$row["Date"]."</td>\n";
    echo "</tr>";
}
echo "</table>\n";

//Enable the Prev link if not first page
if ($page > 1) {
    echo("<a href=\"creditshistory.php?page=".($page-1)."\">PREV</a> ");
} else {
    echo("PREV ");
}

//Create links for each page in report
for ($i=1; $i<=$numofpages; $i++) {
    if ($i == $page) {
        echo "$i ";
    } else {
        echo "<a href=\"creditshistory.php?page=$i\">$i</a> ";
    }
}

//Enable the Next link if not last page
if ($page < $numofpages) {
    echo("<a href=\"creditshistory.php?page=".($page+1)."\">NEXT</a>");
} else {
    echo("NEXT");
} 

?>

Link to comment
Share on other sites

-shakes head- Try this

 

<?php

$username       = $_SESSION['username'];
$limit          = 10;
$query_count    = "SELECT count(*) FROM goldies WHERE Sender='$username'";
$result_count   = mysql_query($query_count) or die("Error: " . mysql_error());
$totalrows      = mysql_result($result_count, 0, 0);
$numofpages     = ceil($totalrows/$limit);
$rowsperpage    = $totalrows/$numofpages;

if (isset($_GET['page'])) $page = $_GET['page'];
else $page = 1;

$offset = ($page - 1) * $rowsperpage;
$query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $offset,10";

echo  $query .'<br>';
$result = mysql_query($query) or die("Error: " . mysql_error());

//Exit if no records to display
if (mysql_num_rows($result) == 0) {
    exit("Nothing to Display!");
}

//Create the table of output data
echo "<table>\n";
while ($row = mysql_fetch_array($result)) {
    //Alternate the row background color
    $bgcolor = ($bgcolor == "#737373")?'#737373':'#737373';
    //Display the row of data
    echo "<tr bgcolor=\"$bgcolor\">\n";
    echo "  <td>".$row["Receiver"]."</td>\n";
    echo "  <td>".$row["Amount"]."</td>\n";
    echo "  <td>".$row["Date"]."</td>\n";
    echo "</tr>";
}
echo "</table>\n";

//Enable the Prev link if not first page
if ($page > 1) {
    echo("<a href=\"creditshistory.php?page=".($page-1)."\">PREV</a> ");
} else {
    echo("PREV ");
}

//Create links for each page in report
for ($i=1; $i<=$numofpages; $i++) {
    if ($i == $page) {
        echo "$i ";
    } else {
        echo "<a href=\"creditshistory.php?page=$i\">$i</a> ";
    }
}

//Enable the Next link if not last page
if ($page < $numofpages) {
    echo("<a href=\"creditshistory.php?page=".($page+1)."\">NEXT</a>");
} else {
    echo("NEXT");
} 

?>

Link to comment
Share on other sites

Okay...this is like my last hope

 

<?php

$username       = $_SESSION['username'];
$limit          = 10;
$query_count    = "SELECT count(*) FROM goldies WHERE Sender='$username'";
$result_count   = mysql_query($query_count) or die("Error: " . mysql_error());
$totalrows      = mysql_result($result_count, 0, 0);
$numofpages     = ceil($totalrows/$limit);

if (isset($_GET['page'])) $page = $_GET['page'];
else $page = 1;

$offset = ($page - 1) * $limit;
$query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $offset,10";

echo  $query .'<br>';
$result = mysql_query($query) or die("Error: " . mysql_error());

//Exit if no records to display
if (mysql_num_rows($result) == 0) {
    exit("Nothing to Display!");
}

//Create the table of output data
echo "<table>\n";
while ($row = mysql_fetch_array($result)) {
    //Alternate the row background color
    $bgcolor = ($bgcolor == "#737373")?'#737373':'#737373';
    //Display the row of data
    echo "<tr bgcolor=\"$bgcolor\">\n";
    echo "  <td>".$row["Receiver"]."</td>\n";
    echo "  <td>".$row["Amount"]."</td>\n";
    echo "  <td>".$row["Date"]."</td>\n";
    echo "</tr>";
}
echo "</table>\n";

//Enable the Prev link if not first page
if ($page > 1) {
    echo("<a href=\"creditshistory.php?page=".($page-1)."\">PREV</a> ");
} else {
    echo("PREV ");
}

//Create links for each page in report
for ($i=1; $i<=$numofpages; $i++) {
    if ($i == $page) {
        echo "$i ";
    } else {
        echo "<a href=\"creditshistory.php?page=$i\">$i</a> ";
    }
}

//Enable the Next link if not last page
if ($page < $numofpages) {
    echo("<a href=\"creditshistory.php?page=".($page+1)."\">NEXT</a>");
} else {
    echo("NEXT");
} 

?>

Link to comment
Share on other sites

Poco...it....works....

 

so sleepy,

 

thanks a lot for this man. You won't hate me for putting you through this will you?

 

also is this script stable? it wont fuk up onc ei move my site to the real server will it?

 

Finally! It should be fine now.

 

Na, I don't hate you for it. If anything, it made me better. Now I could probably code pagination without looking at any outside resources...blindfolded xP hah.

 

Don't forget to press "topic solved". It should feel good to press it after 100+ posts.

 

And yes, you should be able to change limit to anything you want and it will work.

Link to comment
Share on other sites

Wow, imagine my surprise to come into work and see this post going to 8 pages!

Anyway, I looked over that final code and changing the $limit variable will NOT increase the number of records displayed on a page because there is a bug in the code:
[code]$offset = ($page - 1) * $limit;
$query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $offset,10";

 

Change the "10" at the end of that query to "$limit"

 

Also, there will be errors if the page variable on the query is bad. You should always validate the data. Change this:

if (isset($_GET['page'])) $page = $_GET['page'];
else $page = 1;

 

To this

{code]$page = (isset($_GET['page']) && is_int($_GET['page']))?$_GET['page']:1;[/code]

 

Lastly, if your results start becoming very large you will want to create some code to limit the amount of page numbers that are displayed. But that is another problem to be tackled later.

Link to comment
Share on other sites

[code][quote author=pocobueno1388 link=topic=170599.msg758980#msg758980 date=1197321223]
But if you change $limit to "10" it works? That doesn't make sense...
[/quote]

 

no lol. the changing 10 to $limit is fine. it changes the amount of rows displayed to wahtever $limit is set to but damato told me to:

 

change:

 

Also, there will be errors if the page variable on the query is bad. You should always validate the data.

 

if (isset($_GET['page'])) $page = $_GET['page'];

else $page = 1;

 

To this: due to somethign abotu validating your stuff.

{code]$page = (isset($_GET['page']) && is_int($_GET['page']))?$_GET['page']:1;

 

the $limit thing works, but the pages dnt change now due to what i changed above.

[/code]

Link to comment
Share on other sites

Ah, my error. You can't use is_int() on a GET variable since they are all considered as strings.

 

Instead you should declare the value as an int - if it isn't an int is will be converted to one. So, something like 4.5 will be set to the int 4. A non number value will be converted to the int 0. You can then ensure the value is greater or equal to 1.

 

This will ensure that page is always a whole number that is greater than or equal to 1.

$page = ((int)$_GET['page']<1)?1:(int)$_GET['page'];

 

I have tested this

 

Link to comment
Share on other sites

works like a charm thanks damato

btw any technical names for the function where it hides lots of page numbers?

 

Not that I know of. You just need to decide what the rules are and build it. Let's say you want to show up to 5 pages in front and 5 pages in back; as well as the first and last pages. (that is the same as is used on this site).

 

<?php
//Enable the Prev link if not first page
if ($page > 1) {
    echo("<a href=\"creditshistory.php?page=".($page-1)."\">PREV</a> ");
} else {
    echo("PREV ");
}

//Number of pages to show front/back
$pageSpan = 5;

//Determine first and last pages to show
$first = ($page>($pageSpan+1))?($page-$pageSpan):2;
$last  = ($page<($numofpages-$pageSpan))?($page+$pageSpan)$numofpages-1);

//Show first page
echo "<a href=\"creditshistory.php?page=1\">1</a> \n";

//Show ellipses if next page is not 2
if ($first!=2) {
    echo "... \n";
}

//Create links for each page in report
for ($i=$first; $i<=$last; $i++) {
    if ($i == $page) {
        echo "[$i] ";
    } else {
        echo "<a href=\"creditshistory.php?page=$i\">$i</a> ";
    }
}

//Show ellipses if next page is not last page
if ($last!=($numofpages-1)) {
    echo "... \n";
}

//Show the last page
echo "<a href=\"creditshistory.php?page=$numofpages\">$numofpages</a> \n";

//Enable the Next link if not last page
if ($page < $numofpages) {
    echo("<a href=\"creditshistory.php?page=".($page+1)."\">NEXT</a>");
} else {
    echo("NEXT");
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.