NamePros.Com (http://www.namepros.com/)
-   CODE (http://www.namepros.com/code/)
-   -   Pagination (http://www.namepros.com/code/183658-pagination.html)

Xyzer 04-04-2006 06:35 AM

Pagination
 
Hi,

I have this page:
Code:
<?php $connect = mysql_connect("localhost", "username", "password"); mysql_select_db("db", $connect); $select = mysql_query("SELECT * FROM `tutorials` WHERE category = 'photoshop'", $connect); while($row = mysql_fetch_array($select, MYSQL_ASSOC)) { $id = $row['id']; $name = $row['name']; $email = $row['email']; $category = $row['category']; $url = $row['url']; $url2 = $row['url2']; $websitename = $row['websitename']; $tutorialname = $row['tutorialname']; $description = $row['description']; echo " <p> <strong>Tutorial Name:</strong> <a href=\"$url2\">$tutorialname</a><br />\n <strong>Submitted By:</strong> <a href=\"$url\">$websitename</a><br />\n <strong>Description:</strong> $description</p>"; } ?>


I hope you all understand, and what i am trying to do is intergrate pagination, but i dont know where the script will go and how to use the
Code:
$select = mysql_query("SELECT * FROM `tutorials` WHERE category = 'photoshop'", $connect); while($row = mysql_fetch_array($select, MYSQL_ASSOC)) { $id = $row['id']; $name = $row['name']; $email = $row['email']; $category = $row['category']; $url = $row['url']; $url2 = $row['url2']; $websitename = $row['websitename']; $tutorialname = $row['tutorialname']; $description = $row['description']; echo " <p> <strong>Tutorial Name:</strong> <a href=\"$url2\">$tutorialname</a><br />\n <strong>Submitted By:</strong> <a href=\"$url\">$websitename</a><br />\n <strong>Description:</strong> $description</p>"; } ?>

bit which i need. Thanks
Steven

Noobie 04-04-2006 07:50 AM

I use PEAR :: Pager now because i'm just totally in love with PEAR ... but this is how I used to do it (i got this off a tutorial a long time ago and modified it)

PHP Code:
$max_results = 30;    //maximum entries to show per page
if(!isset($_GET['page'])){
    
$page = 1;
}else {
    
$page  = $_GET['page'];
}
$sql = "SELECT * FROM `tutorials` WHERE category = 'photoshop'";
$select = mysql_query($sql, $connect);
$totalrows = mysql_num_rows($select );
$minval = (($page * $max_results) - $max_results);
$sql .= " LIMIT $minval, $max_results";
$select = mysql_query($sql, $connect);
while(
$row = mysql_fetch_array($select, MYSQL_ASSOC)) {
    
$id = $row['id'];
    
$name = $row['name'];
    
$email = $row['email'];
    
$category = $row['category'];
    
$url = $row['url'];
    
$url2 = $row['url2'];
    
$websitename = $row['websitename'];
    
$tutorialname = $row['tutorialname'];
    
$description = $row['description'];

echo
"
    <p>
     <strong>Tutorial Name:</strong> <a href=\"$url2\">$tutorialname</a><br />\n
    <strong>Submitted By:</strong> <a href=\"$url\">$websitename</a><br />\n
    <strong>Description:</strong> $description</p>"
;
}
# pagination
$total_pages = ceil($totalrows / $max_results);
if(
$page > 1){
    
$prev = ($page - 1);
    echo
"<a href=\"?id=$id&page=$prev\">&lt;</a>&nbsp;";
}
for(
$i = 1; $i <= $total_pages; $i++){
      if(
$i % 30 == 0) {echo "<br>\n";} // How many page numbers you want per line
    
if(($page) == $i){
       echo
"$i&nbsp;";
        } else {
     echo
"<a href=\"?id=$id&page=$i\">$i</a>&nbsp;";
        }
    }
    if(
$page < $total_pages){
    
$next = ($page + 1);
    echo
"<a href=\?id=$id&page=$next\">&gt;</a>";
}

?>

Xyzer 04-04-2006 08:06 AM

This displays what i want but the only problem i am having is that my site works using ?id= and when i go to page 2 it goes back to the default page and shows the url as http://www.x6core.com/?page=2

I am using the code in the post above, how can i change this?

Noobie 04-04-2006 08:11 AM

I just edited it ... its a simpler way (just adding your variables like the id manually) of doing it but not the ideal way

Xyzer 04-04-2006 08:22 AM

its still not working, if i click on next it says ?id=5&page=2, and also if i click on it and edit the code to ?id=photoshop&page=2 it still doesnt work.
I worked it out that my code already says
Code:
$id = $row['id'];

but if i take it out where does yours tell it what $id is?

Noobie 04-04-2006 09:40 AM

i'm confused, are you using category=photoshop or id=someid.

Either way, you can enter whatever every varaibles you want there

edit: spelling

Xyzer 04-04-2006 09:42 AM

ok, i have fixed it any for anyone else who wants to know.
Code:
$id = "photoshoptutorials";

SecondVersion 04-04-2006 11:38 AM

Just a note
PHP Code:
if(!isset($_GET['page'])){
    
$page = 1;
}else {
    
$page  = $_GET['page'];
}

Wouldn't recommend using that.

Try this instead
PHP Code:
$page = (!isset($_GET['page'])) ? 1 : intval($_GET['page']);

Noobie 04-04-2006 12:00 PM

yup very true, forgot the intval...

Xyzer 04-04-2006 12:24 PM

What does that piece of code do?

Noobie 04-04-2006 01:49 PM

Same thing as saying
PHP Code:
if(!isset($_GET['page'])){
    
$page = 1;
}else {
    
$page  = intval($_GET['page']);
}

but alot cleaner

page = condition ? true : false;


All times are GMT -7. The time now is 01:53 AM.
Site Sponsors
Advertise your business at NamePros

Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0