Dynadot โ€” .com Registration $8.99

Pagination

Spaceship Spaceship
Watch
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
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
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:
$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\"><</a>ย "; 
} 
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ย "; 
        } else { 
     echo "<a href=\"?id=$id&page=$i\">$i</a>ย "; 
    	} 
	}
	if($page < $total_pages){ 
    $next = ($page + 1); 
    echo "<a href=\?id=$id&page=$next\">></a>"; 
} 

?>
 
Last edited:
0
•••
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?
 
0
•••
I just edited it ... its a simpler way (just adding your variables like the id manually) of doing it but not the ideal way
 
0
•••
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?
 
Last edited:
0
•••
i'm confused, are you using category=photoshop or id=someid.

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

edit: spelling
 
Last edited:
0
•••
ok, i have fixed it any for anyone else who wants to know.
Code:
$id = "photoshoptutorials";
 
0
•••
Just a note
PHP:
if(!isset($_GET['page'])){
    $page = 1;
}else {
    $page  = $_GET['page'];
}
Wouldn't recommend using that.

Try this instead
PHP:
$page = (!isset($_GET['page'])) ? 1 : intval($_GET['page']);
 
Last edited:
0
•••
yup very true, forgot the intval...
 
0
•••
What does that piece of code do?
 
0
•••
Same thing as saying
PHP:
if(!isset($_GET['page'])){ 
    $page = 1; 
}else { 
    $page  = intval($_GET['page']); 
}
but alot cleaner

page = condition ? true : false;
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back