Reputation: 2277
I have a pagination inside an user profile.
When I'm inside the test profile the URL is profile.php?uid=1
But now I want to use a pagination to wiev the information from a SQL database. When I try the pagination on another page it works fine, but as you know the pagination change the URL depends on what side your on.
So my problem is that the pagination change my profile.php?uid=1 to profile.php?page=2
Do you have any tips on how to fix that error ?
This is my pagination code.
/*pagination */
$per_page = 5;
//$pages_query = mysql_query("SELECT COUNT('user_id') FROM users");
$pages_query = mysql_query("SELECT COUNT('file_name') FROM users WHERE `user_name` = '{$_SESSION['username']}' ") or die(mysql_error());
$pages = ceil(mysql_result($pages_query, 0) /$per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
<?php
$data = mysql_query("SELECT * FROM users LIMIT $start, $per_page")
or die(mysql_error());
echo "<table border cellpadding=1>";
while($info = mysql_fetch_array( $data ))
{ ?><?php
echo "<tr>";
// echo "<td>".$info['user_id'] . "</td> ";
echo "<td>".$info['user_name'] . "</td> ";
// echo "<td>".$info['file_name'] . "</td> ";
?>
<td> <a href="download.php?user_id=<?php echo $info['user_id'];?>"> <?php echo $info['file_name']; ?></a></td>
<?php
echo "<td>".date('d/m/Y') . "</td>" ;
}
echo "</tr>";
echo "</table>";
if($pages >= 1 && $page <=$pages){
for ($x = 1; $x<=$pages; $x++){
echo ($x == $page) ? '<strong><a href="?page='.$x.'">' .$x. '</a> </strong>' : '<a href="?page=' .$x. '">' .$x. ' </a> ';
}
}
?>
/*pagination */
And the profile
function fetch_user_info($uid){ $uid = (int)$uid;
$sql = "SELECT
`user_name` AS `username`,
`user_email` AS `email`
FROM `users`
WHERE `user_id` = {$uid} ";
$result = mysql_query($sql);
return mysql_fetch_assoc($result);
}
$data = mysql_query("SELECT * FROM users") or die(mysql_error());
$files = mysql_query("SELECT `file_name` FROM users WHERE `user_name` = '{$_SESSION['username']}' ") or die(mysql_error());
$user_info = fetch_user_info($_GET['uid']);
I really dont have a clue on how to fix this =/
Upvotes: 0
Views: 246
Reputation: 204
Typo:
$page = (isset($_REQUEST['page'])) ? 1 : $_REQUEST['page'];
Changed To
$page = (!isset($_REQUEST['page'])) ? 1 : $_REQUEST['page'];
Its a very simple solution. You basically "read" the URL/query string in the pagination code and automatically "append that to your URL. I wrote some simple plug and play code for you - that will easily do the trick (All you need is just 1 extra line of code and one include file - 'inc_pagination.php', - to ANY page that needs pagination. no modification of existing code):
/*pagination */
$per_page = 5;
include 'inc_pagination.php'; /* FIRST LINE ADDED TO YOUR CODE*/
$pages_query = mysql_query("SELECT COUNT('file_name') FROM users WHERE `user_name` = '{$_SESSION['username']}' ") or die(mysql_error());
$pages = ceil(mysql_result($pages_query, 0) /$per_page);
/* REMOVING THESE TWO LINES */
/* $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page; */
$data = mysql_query("SELECT * FROM users LIMIT $start, $per_page") or die(mysql_error());
echo "<table border cellpadding=1>";
while($info = mysql_fetch_array( $data ))
{ ?><?php
echo "<tr>";
echo "<td>".$info['user_name'] . "</td> ";
?>
<td> <a href="download.php?user_id=<?php echo $info['user_id'];?>"> <?php echo $info['file_name']; ?></a></td>
<?php
echo "<td>".date('d/m/Y') . "</td>" ;
}
echo "</tr>";
echo "</table>";
/* 2nd LINE ADDED (Replaces Existing Page Code) - Add this wherever you need page numbers displayed */
echo paginate_me(ceil(mysql_result($pages_query, 0)/$per_page), $per_page);
Please note that you need to obviously remove your existing pagination code block:
if($pages >= 1 && $page <=$pages){
for ($x = 1; $x<=$pages; $x++){
echo ($x == $page) ? '<strong><a href="?page='.$x.'">' .$x. '</a> </strong>' : '<a href="?page=' .$x. '">' .$x. ' </a> ';
}
}
And finally here is the code for include file 'inc_pagination.php'. Please note that this one has all the bells and whistles - including javascript for hover/mouseover and other visual effects - I can also include a really simple text only version if you like!
<?
//The number of page numbers you want displayed accross page in pagination bar
$nav_links_per_row = 35;
$page = (!isset($_REQUEST['page'])) ? 1 : $_REQUEST['page'];
$start = ($page == 1) ? 0 : ($page - 1) * $per_page ;
$per_page = (!$per_page) ? 5 : $per_page;
function paginate_me($no_of_pages, $per_page, $nav_links_per_row = 35) {
$page = 1;
$page = $_REQUEST['page'];
$x_mid_start = 1;
$x_mid_end = $no_of_pages;
/* START: Here is the main code that detects all your existing QUERY STRING and AUTOMATICALLY includes them in the Page Links */
$x_url = urldecode($_SERVER['HTTP_REFERER']);
parse_str($x_url, $output);
$qs = explode('?',urldecode($x_url));
/* END: QUERY STRING DETECT CODE */
$p_me .= "<br><table align = 'Center' width = '90%' cellpadding = '0' border = '0'><tr>";
if ($no_of_pages > 35)
{
// Go Back 5
if ( $page > 5 ){
$bg_color = "White";
$ObjTopBar = '"jTopBar_start"';
$q_str = $_SERVER['QUERY_STRING'];
$jump = (int)$page - 5;
$title = "User Files: Jump Back 5 Pages";
$this_page = str_replace("page=$page","page=$jump","?".$q_str);
$p_me .= "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: #333333; vertical-align:middle;padding-top: 2px; overflow:auto'>-</div></a></td>";
}
//1 to 10
for ($i = 1; $i <= 8; $i++)
{
if ($i == $page) $bg_color = "orange";
else $bg_color = "#333333";
if (!$page) $bg_color = "orange";
$ObjTopBar = '"jTopBar'.$i.'"';
$q_str = $_SERVER['QUERY_STRING'];
$this_page = str_replace("page=$page","page=$i","?".$q_str);
$title = "User Files: Jump to Page $i";
$p_me .= "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'>".$i."</div> </a></td>";
}
//Set Starting Page Numbers for Mid Block If Page Count is > 35
$p_me .= "<td> <font color=white>... </td>";
$x_mid_start = floor($no_of_pages / 2) - 5 ;
$x_mid_end = floor($no_of_pages / 2) + 5 ;
//if selected page is between half - 5 to half + 5 SHOW start: selected page, end: selected page + 9
if (($page > 8) && ($page < $no_of_pages - 8)) {
$x_mid_start = $page - 5;
$x_mid_end = $page + 5;
}
}
//Empty Blocks...First Few Spaces...if Page Count < 35
if ($no_of_pages < 35) {
for ($k = 1; $k <= floor((35 - $no_of_pages) / 2); $k++)
{
$ObjTopBarLinks = '"divBEnd_'.$k.'"';
$ObjTopBarEmpty = '"divBStart'.$k.'"';
$p_me .= "<td><div id=".$ObjTopBarEmpty." onMouseOver='javascript: onMouseOverOutE(".$ObjTopBarLinks.")' onMouseOut='javascript: onMouseOverOutE(".$ObjTopBarLinks.")' style='cursor: pointer; cursor: hand; border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: grey; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'> </div></td>";
}
}
// Print Page Numbers in the Center
for ($i = $x_mid_start; $i <= $x_mid_end; $i++)
{
if ($i == $page) $bg_color = "orange";
else $bg_color = "#333333";
if (!$page) $bg_color = "orange";
$ObjTopBar = '"jTopBar'.$i.'"';
$q_str = $_SERVER['QUERY_STRING'];
$this_page = str_replace("page=$page","page=$i","?".$q_str);
$title = "User Files: Jump to Page $i";
$p_me .= "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'>".$i."</div> </a></td>";
}
//Empty Blocks...Last Few Spaces...If Page Count < 35
if ($no_of_pages < 35) {
for ($k = 1; $k <= ceil(($nav_links_per_row - $x_mid_end)/2); $k++)
{
$ObjTopBarLinks = '"divBStart'.$k.'"';
$ObjTopBarEmpty = '"divBEnd_'.$k.'"';
$p_me .= "<td><div id=".$ObjTopBarEmpty." onMouseOver='javascript: onMouseOverOutE(".$ObjTopBarLinks.")' onMouseOut='javascript: onMouseOverOutE(".$ObjTopBarLinks.")' style='cursor: pointer; cursor: hand; border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: grey; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'> </div></td>";
}
}
else
{
//Page Count is > 35
$p_me .= "<td> <font color=white>... </td>";
//no_of_pages-10 to end
for ($i = $no_of_pages - 8; $i <= $no_of_pages; $i++)
{
if ($i == $page) $bg_color = "orange";
else $bg_color = "#333333";
if (!$page) $bg_color = "orange";
$ObjTopBar = '"jTopBar'.$i.'"';
$q_str = $_SERVER['QUERY_STRING'];
$this_page = str_replace("page=$page","page=$i","?".$q_str);
$title = "User Files: Jump to Page $i";
$p_me .= "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'>".$i."</div> </a></td>";
}
// Go Forward 5
if ( $page < $no_of_pages - 5 ){
$bg_color = "White";
$ObjTopBar = '"jTopBar_end"';
$q_str = $_SERVER['QUERY_STRING'];
$jump = (int)$page + 5;
$title = "User Files: Jump Forward 5 Pages";
$this_page = str_replace("page=$page","page=$jump","?".$q_str);
$p_me .= "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: #333333; vertical-align:middle;padding-top: 2px; overflow:auto'>+</div></a></td>"; }
}
$p_me .= "</tr></table><br>";
return $p_me;
}
?>
<script type="text/javascript">
function onMouseOverOut(Obj) {
setTimeout(function() { onMouseOverOut1(Obj) }, 300);
}
function onMouseOverOut1(x) {
var ODiv = document.getElementById(x);
var currBg = ODiv.style.background;
if (currBg == "orange") return false;
if (x == "jTopBar_start") return false;
if (x == "jTopBar_end") return false;
if (ODiv.style.background.indexOf("red") < 0)
ODiv.style.background = "red";
else
if (ODiv.style.background.indexOf("#333333") < 0)
ODiv.style.background = "#333333";
}
function onMouseOverOutE(Obj) {
setTimeout(function() { onMouseOverOutE1(Obj) }, 300);
}
function onMouseOverOutE1(Obj) {
var ODiv = document.getElementById(Obj);
if (ODiv.style.border.indexOf("grey") < 0)
ODiv.style.border = "1px solid grey";
else
if (ODiv.style.border.indexOf("silver") < 0)
ODiv.style.border = "1px solid silver";
if (ODiv.style.background.indexOf("white") < 0)
ODiv.style.background = "white";
else
if (ODiv.style.background.indexOf("grey") < 0)
ODiv.style.background = "grey";
}
</script>
Upvotes: 0
Reputation: 11447
Looks like you're not passing the user id in your pager links, try changing this line...
echo ($x == $page) ? '<strong><a href="?page='.$x.'">' .$x. '</a> </strong>' : '<a href="?page=' .$x. '">' .$x. ' </a> ';
to
echo ($x == $page) ? '<strong><a href="?uid='.$_GET['uid'].'&page='.$x.'">' .$x. '</a> </strong>' : '<a href="?uid='.$_GET['uid'].'&page=' .$x. '">' .$x. ' </a> ';
Upvotes: 1
Reputation: 2318
Do you have uid
on the session?
try
echo ($x == $page) ? '<strong><a href="?page='.$x.'">' .$x. '</a> </strong>' : '<a href="?user_id=' . $_SESSION['uid'] . '&page=' .$x. '">' .$x. ' </a> ';
But, actually you do not really need it if you add session for your uid
. You can change fetch_user_info
function fetch_user_info(){
$sql = "SELECT
`user_name` AS `username`,
`user_email` AS `email`
FROM `users`
WHERE `user_id` = " . $_SESSION['uid'];
$result = mysql_query($sql);
return mysql_fetch_assoc($result);
}
Upvotes: 0