problem with pagination[solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluelad
Forum Commoner
Posts: 34
Joined: Mon Jun 05, 2006 8:34 am

problem with pagination[solved]

Post by bluelad »

I have been making links page. this is d code

Code: Select all

<html>
<head><title>Cool Links</title>
</head>
<body bgcolor="#0A5C8E" link=" #EBEBEB" vlink=" #EBEBEB" alink="#EBEBEB">
<font color="#FFCE00" size="4" font="Times New Roman">

 <br>
<div align="left">To add a link <a href="insert.php" class="mainmenu"><b>click here</b></a>
</div>
</font>
<font color="#FFCE00" size="2" font="Times New Roman">Click Refresh to update
</font>
<br>
<br>
<?php
function MakeClickable($String) {
  $String = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $String);
  $String = ' ' . $String;
  $String = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $String);
  $String = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $String);
  $String = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $String);
  $String = substr($String, 1);
  return($String);
}

$link=mysql_connect("localhost", "root", "");
$db=mysql_select_db("chetan_db", $link);
?>



<table width="100%" align="center" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse" bordercolor="#808080">
<tr>
<td width="4%" align="center"><b><font color="#FFB340">Id</font></b></td>
<td width="36%" align="center"><b><font color="#FFB340">URL</font></b></td>
<td width="60%" align="center"><b><font color="#FFB340">Description</font></b></td>
</tr>
<?php
$sql = "select * from link order by `id` desc";
$query = mysql_query($sql);

while($row=mysql_fetch_array($query))
{
?>
<tr>
<td align="center"><font color="#EBEBEB" size="2.5"><?php echo MakeClickable($row[0]); ?></font></td>
<td align="center"><font color="#EBEBEB" size="2.5"><?php echo MakeClickable($row[1]); ?></font></td>
<td align="left"><font color="#EBEBEB" size="2.5"><?php echo MakeClickable($row[2]); ?></font></td>
</tr> 


<?php
}
?>

</table>

<?php
/*start pagination*/
/*database conection settings*/
mysql_connect("localhost","root","");//host, username and password
mysql_select_db("chetan_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 `link` 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 `link`"));//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
?> 


</body>
</html>
The problem is with the pagination.Instead of showing 2 items per page it shows all the items in all the pages i.e page 1 and page 4 have the same thing?
Last edited by bluelad on Wed Jun 28, 2006 1:15 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Where are you setting your page limits and max's & min's? Also, have you searched these boards for 'pagination'?
bluelad
Forum Commoner
Posts: 34
Joined: Mon Jun 05, 2006 8:34 am

Post by bluelad »

Also, have you searched these boards for 'pagination'?
Yes i have. Infact i have tried all of them but none of them work? Keep showing eroors.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Not sure if this is the same in MySQL but in postgres the select is

Code: Select all

SELECT X FROM Y LIMIT n OFFSET o
rather than LIMIT n,o
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

MySQL limit clause is LIMIT START_AT, LIMIT_TO
Post Reply