Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

Pagination

SpaceshipSpaceship
SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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
Close
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
Close
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
Close
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
Close
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
Close
ok, i have fixed it any for anyone else who wants to know.
Code:
$id = "photoshoptutorials";
 
0
Close
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
Close
yup very true, forgot the intval...
 
0
Close
What does that piece of code do?
 
0
Close
Same thing as saying
PHP:
if(!isset($_GET['page'])){ 
    $page = 1; 
}else { 
    $page  = intval($_GET['page']); 
}
but alot cleaner

page = condition ? true : false;
 
0
Close
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back