Jump to content


pagination help....


11 replies to this topic

#1 markc1822

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male
  • Location:New York

Posted 21 December 2006 - 08:22 PM

I need some help installing a Pagination script on to my site. I have been reading and trying to follow some of the tutorial on it, but since I don’t know the first thing about php it’s hard to follow. I was wondering if someone could install it on to my site for me. Or explain how I can do it.

Something like this script:
http://www.pixel2life.com/publish/tutorial...n_to_your_site/

Thanks

#2 pirateXcore

    Young Padawan

  • Members
  • Pip
  • 281 posts
  • Gender:Male

Posted 21 December 2006 - 08:47 PM

I wouldn't recommend starting with that if you don't understand php....
but have you managed to get any of it to work? if so, where are you encountering problems?

#3 markc1822

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male
  • Location:New York

Posted 21 December 2006 - 10:45 PM

No, I didn’t get anything to work, I was not sure how to start. I know it needs to connect to mysql but I don’t see any script to allow it to connect.

What would you recommend me starting with?

#4 pirateXcore

    Young Padawan

  • Members
  • Pip
  • 281 posts
  • Gender:Male

Posted 21 December 2006 - 11:23 PM

The very basics.....go to http://w3schools.com and there's a php section there that will help you start.
The code to connect to your database is this:
$connect = @mysql_connect("localhost","username","userpassword");
if (!$connect)
{
  die('Connection failed ' . mysql_error());
}
mysql_select_db("databasename", $connect) or die(mysql_error());

okay, so that should get you connected "username" is the username for the database you're going to use (you should know this from when you created the database) "password" is the password for that username (you should know this too...if not your SOL cause it's hardcoded.)
"databasename" is the name of the database you're connecting too.

The rest of the stuff you should be able to figure out...i hope...the code connects to the database..that's it just add that at the beginnign of your php code.
at the end of your php code, just before you add ?> put mysql_close($connect) to close the connection.
Hope that helps some.
I guess I can explain the rest of the code i posted.

$connect = @mysql_connect("localhost","username","userpassword");
finds the username & password to get into your database

if (!$connect)
{
die('Connection failed ' . mysql_error());
}
if it can't connect....it'll post an error telling you why

mysql_select_db("databasename", $connect) or die(mysql_error());
find the database if it connected before....if it doesn't then it gives you an error again.

Edit: error fixed

Edited by tgs, 22 December 2006 - 02:53 AM.


#5 Jynxis

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 22 December 2006 - 01:52 AM

i already see an error with your code tgs.

its
mysql_select_db("databasename", $connect) or die(mysql_error());
not
mysql_select_db("databasename, $connect) or die(mysql_error());

You missed out the ending "(quote)

#6 pirateXcore

    Young Padawan

  • Members
  • Pip
  • 281 posts
  • Gender:Male

Posted 22 December 2006 - 02:53 AM

whoops.
Thanks for getting that one

#7 joe

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Location:stuck in the middle of cyber space

Posted 22 December 2006 - 05:04 AM

View PostPlaGuEX, on Dec 22 2006, 01:52 PM, said:

i already see an error with your code tgs.

its
mysql_select_db("databasename", $connect) or die(mysql_error());
not
mysql_select_db("databasename, $connect) or die(mysql_error());

You missed out the ending "(quote)

sometimes people forget something.... :) :(
it's ok...
keep coding....

regards...

#8 markc1822

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male
  • Location:New York

Posted 22 December 2006 - 02:28 PM

The entire code will look something like this? With the username and password replaced with my information.

I am curious about something, lets say I have about 5 pages www.mywebsite.com/page1 , page2, page3 ……..
What do I have with these pages so that script will recognize them?


$connect = @mysql_connect("localhost","username","userpassword");
if (!$connect)
{
  die('Connection failed ' . mysql_error());
}
mysql_select_db("databasename", $connect) or die(mysql_error());

mysql_close($connect)

<?php

    if($_GET['page']) // Is page defined?

    {

        $page = $_GET['page']; // Set to the page defined

    }else{

        $page = 1; // Set to default page 1

    }

$max = 10; // Set maximum to 10

 

$cur = (($page * $max) - $max); // Work out what results to show

 

$getdata = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT $cur, $max") or die(mysql_error()); // select the results

$data = mysql_fetch_array($getdata); // get the data

 

$counttotal = mysql_query("SELECT * FROM `table` ") or die(mysql_error()); // select all records        

$counttotal = mysql_num_rows($counttotal); // count records

 

$total_pages = ceil($counttotal / $max); // dive the total, by the maximum results to show 

 

if($page > 1){ // is the page number more than 1?

                $prev = ($page - 1); // if so, do the following. take 1 away from the current page number

                echo '<a href="?page=' . $prev . '">« Previous</a>'; // echo a previous page link

                }

 

for($i = 1; $i <= $total_pages; $i++) // for each page number

                {

                    if($page == $i) // if this page were about to echo = the current page

                        {

                            echo'<b>' . $i .'</b> '; // echo the page number bold

                                } else {

                            echo '<a href="?page=' . $i . '">' . $i . '</a> '; // echo a link to the page

                        }

                }

 

if($page < $total_pages){ // is there a next page?

                    $next = ($page + 1); // if so, add 1 to the current

                echo '<a href="?page=' . $next . '">Next »</a>'; // echo the next page link

                    } 

?> // end php script


#9 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 22 December 2006 - 04:47 PM

Fixed and condensed code.

<?php
$connect = @mysql_connect('localhost', 'username', 'userpassword'); // MySQL Connect
if(!$connect)  die('Connection failed ' .mysql_error()); // Connection Error?
mysql_select_db('databasenamed', $connect) or die(mysql_error()); // Select MySQL Database
$page = $_GET['page'] ? (int)$_GET['page'] : 1; // Set Page Number
$max = 10; // Maximum Limit per Page
$cur = (($page * $max) - $max); // What Results to Show
$data = mysql_fetch_array(mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT $cur, $max")) or die(mysql_error()); // Get Results
$counttotal = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `table` ")) or die(mysql_error()); // Count all Records
$counttotal = $counttotal[0]; // Count of all Records
$total_pages = ceil($counttotal / $max); // Find Total Pages Available
if($page > 1) echo '<a href="?page='.($page - 1).'">&laquo; Previous</a>'; // Echo Previous Page if Exists
for($i = 1; $i <= $total_pages; $i++) echo ($page == $i) ? "<strong>{$i}</strong>" : '<a href="?page='.$i.'">'.$i.'</a>'; // Loops and Echo each Available Page
if($page < $total_pages) echo '<a href="?page='.($page +  1).'">Next &raquo;</a>'; // Echo Next Page if Exists
?>

This is extrememly basic pagination, and could definetely be further expanded. I have a very expandable pagination class I wrote that can be used for anything with ease, if I ever get it back from my corrupted laptop, and if you're interested, I can get you a copy of it. :)

#10 markc1822

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male
  • Location:New York

Posted 22 December 2006 - 08:47 PM

sure Demonslay, if you can get it back i will try it. I used your condensed code version and i am getting this message:

Table 'page_nav123.table' doesn't exist

guess i need to setup the tables?

how would i do that?

#11 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 23 December 2006 - 09:48 AM

Well you should have a MySQL database and everything already set up in the first place...

Simply look in the PHP section here on P2L, theres plenty of PHP/MySQL integration tutorials, and basic MySQL 101 tutorials.
Then you'd obviously change the query.

Also I made a small mistake in my code, since you will most probably be using more than one result, you'll want to use a loop on your data, so just a minor change of one line.

$data = mysql_fetch_array(mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT $cur, $max")) or die(mysql_error()); // Get Results

Change to:
$data = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT $cur, $max") or die(mysql_error()); // Get Results

Then later on with your results, as you'll find in any simple database integration tutorial, you'd echo everything like this.
while($r = mysql_fetch_assoc($data)){
print_r($r); // Just test if all data is present
echo $r['row']; // Or echo bits, as results are returned as an array
}


#12 FTH8er

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 07 May 2007 - 01:16 PM

I was wondering if I was using a new code like this:

<center>
<?php
ob_start(); // allows us to use cookies
include("config.php");
include("bbcode.php");

$q = mysql_query("SELECT * FROM `news` ORDER BY id DESC LIMIT 5");
// querys the database
if (mysql_num_rows($q) == "5") { // if there is nothing than we echo an error

	echo ("There is no news in the database!"); // opps nothing
} 
while($r=mysql_fetch_array($q)){

	$id = $r["id"]; // news id
	$title = $r["title"]; // news title
	$author = $r["author"]; // news author
	$postdate = $r["postdate"]; // news date
	$content = bbcode($r["content"]); // news content with bbcode wrapped around it

	echo ("<tr>
				<td><a href=\"?page=news_comments&view=news&id=$id\">$title</a> Posted by <a href=\"?page=members&user=$author\">$author</a> At $postdate<br></td>
			</tr>
			   <tr>
				<td>$content<br>[<a href=\"?page=news_comments&view=addcomment&id=$id\">Add a comment</a>] [<a href=\"?page=news_comments&view=news&id=$id\">View comments</a>]<br><br></td>
			</tr>");
}
?>
</center>
where I would put this pagination like this:

<?php
/*database conection settings*/
mysql_connect("host","user","pass");//host, username and password
mysql_select_db("db");//database name
$num = $_GET['num'];//Get the numeration of the page
if(empty($num)){//if the numeration is empty
$num = 1;//the numeration is 1
};
$limit = 2;//Sets the limit of results to display in each page, change if you want.
/*
The query will start selecting the numeration, for example 2, less 1, that would be 1
* the limits of results to show per page, 2 in this case so it would be 1*2 = 2, it will
start from 2;) if the limit would be 5 and the numeration would be 3 if would be (3-1)*5 = 10
*/
$start = ($num-1)*$limit;
$start = round($start,0);//rounds the result
/*
This query will select the contrene FROM the start and with a limit of 2, in this case,
because the variable $limit is 2
You can add a WHERE something= '$something' for example, or delete the ORDER by `id`, or change it,
etc
*/
$query = "SELECT * FROM `example` ORDER by `id` LIMIT $start, $limit";
$result = mysql_query($query);//now it makes the query and names it as result
/*
While will repeat this query and mysql_fect_array allow me array the content
*/
while ($r = mysql_fetch_array($result)){
echo "$r[title]<br>";//Echoes the content
};
$totalpages = mysql_num_rows(mysql_query("SELECT * from `example`"));//Get the total number of results
/*
Total resutls/ the limit, in this example is 2, so if there are 10 total result and the limit is 2
there will be 5 pages.
*/
$totalpages = $totalpages / $limit;
$totalpages = round($totalpages,0);
$c = 0;//The variable c is 0
echo "<br>";//make a <br> to separate the results from the [1][2]...
while($c<$totalpages){//while c is < than the total pages
$page = $c + 1;//sets the variable $page as 0 + 1 = 1
if($_GET['num']==$page){//Gets the number of the page and if its the same that the page
echo "[$page] ";//its only echoes the page, not the url to this page
}else{//else
echo "<a href=?num=$page>[$page] </a>";//it echoes the url to the page
}
$c = $c+1;
}
echo "<br>".$totalpages." Pages in total.";//echoes the total pages
?>

I dont know where I would include the pagination in there. Like What I would put in there to paginate it. Please Help. Cause I really need a pagination lol. Please and Thx.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users