0

I have developed a code for pagination in my application with php and mysql. By passing page numbers in querystring with a question mark,but i want to pass the page number parameter with hash instead of question mark.Following is my code with question mark,Please help me in passing page number parameter with a hash sign in url instead of question mark.Help guys.

<html>
<head>
<title>Paging Using PHP</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$rec_limit = 10;

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('dbname');
/* Get total number of records */
$sql = "SELECT count(*) FROM tablename";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];

if( isset($_GET{'page'} ) )
{
   $page = $_GET{'page'} + 1;
   $offset = $rec_limit * $page ;
}
else
{
   $page = 0;
   $offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);

$sql = "SELECT col1,col2,col3 FROM tablename LIMIT $offset, $rec_limit";

$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "EMP ID :{$row['col1']}  <br> ".
         "EMP NAME : {$row['col2']} <br> ".
         "EMP SALARY : {$row['col3']} <br> ".
         "--------------------------------<br>";
} 

if( $page > 0 )
{
   $last = $page - 2;
   echo "<a href=\"pagination2.php?page=$last\">Last 10 Records</a> |";
   echo "<a href=\"pagination2.php?page=$page\">Next 10 Records</a>";
}
else if( $page == 0 )
{
   echo "<a href=\"pagination2.php?page=$page\">Next 10 Records</a>";
}
else if( $left_rec < $rec_limit )
{
   $last = $page - 2;
   echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a>";
}
mysql_close($conn);
?>

Please help me in passing page number parameter with hash parameter

  • OK. I'll do it: Please be aware that PHP's mysql_ API is deprecated. And never trust anything that anyone submits to your database - least of a $GET! – Strawberry Jul 15 '14 at 10:43
  • k.sir.will change it to mysqli or PDO.Please help on amending the code – user3840361 Jul 15 '14 at 16:37

2 Answers2

0

Anything from the hash onwards does not get sent to the server. If you have to use hashes, you will need to implement the pagination on the client side using something like Sammy.js.

As a second option, you can put the part after the hash in a hidden field if you do a post request to the server as suggested in this question.

Finally, a nice way to set up navigation is using / (Forward slashes) instead of hashes as is used in some CMSes like Joomla. Basically, if you request somewebsite.com/index.php/MyPaginationParameter, it will still go to somewebsite.com/index.php and you can strip out the MyPaginationParameter part from the request URL.

Community
  • 1
  • 1
neelsg
  • 4,802
  • 5
  • 34
  • 58
  • I didnt get the article,can U please highlight my code part where do i need to change – user3840361 Jul 15 '14 at 10:26
  • Can we pass pagination parameters with slash as well? – user3840361 Jul 15 '14 at 11:32
  • Not sure what you are asking here. In my post, I do specify how to use a slash – neelsg Jul 15 '14 at 13:12
  • My main concern is seo.Thats is should not make duplicate pages with question mark,thats why i want to use hash.Using hash be fine for seo sir?And if i have to use hash,then it has to be done with js only.Please clarify sir? – user3840361 Jul 15 '14 at 14:31
  • Using `/` instead of `#` is fine for SEO. That is why that method is used in Content Management Systems. To get the parameter, you need to do something like `array_pop(explode('/' , $_SERVER['REQUEST_URI']))` instead of `$_GET['page']` – neelsg Jul 15 '14 at 14:43
  • If you must use `#`, that can be done with JS only as indicated by my first 2 options – neelsg Jul 15 '14 at 14:46
0

Not going through the code or writing a code, but explaining you the algorithm.

Set how many queries should be displayed per page, lets take example of 25 queries per page.

QueriesPerPage=25;
pageSelected=USER_INPUT;  //if (NO USER_INPUT) pageSelected = 1

So switch(pageSelected)
case 1 //FIRST PAGE
show (0 , QueriesPerPage);

default: //ANY PAGE OTHER THAN 1st
show (QueriesPerPage*(pageSelected-1) , QueriesPerPage*pageSelected);


FuncTIOn show (START,END)
{
SELECT * FROM THIS LIMIT START,END;
}

If you apply this algorithm, it will make an efficient script. Hope this will help you.

user1735921
  • 1,359
  • 1
  • 21
  • 46