homepage Welcome to WebmasterWorld Guest from 23.23.57.182
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Help with pagination
Showing blank page
neo2sxn



 
Msg#: 4444054 posted 7:52 am on Apr 21, 2012 (gmt 0)


System: The following message was cut out of thread at: http://www.webmasterworld.com/php/4439894.htm [webmasterworld.com] by eelixduppy - 8:20 pm on Apr 22, 2012 (est -5)


Hello eelixduppy,

I am almost at the end of my project but right now i need your little help.
Actualy i am fetching the $_post data to a pagination script. It shows me the right content on first page.But when i click on the second page then it goes blank, May be because when i click on second page then it refresh the page and then it search again post data which is not there because it has been refresh, please tell me what can be the solution ? this is my coding -


<?php include("include/db.php");?>
<?php
@$my=$_POST['search_category']; // Storing the fetched $_POST data in $my
@$my2=$_POST['sub_category']; // Storing the fetched $_POST data $my2
?>

<?php
/*
Place code to connect to your DB here.
*/
$tbl_name="software";//your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;

/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name where search_category = '$my' AND sub_category = '$my2'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

/* Setup vars for query. */
$targetpage = "soft.php"; //your file name (the name of this file)
$limit = 1; //how many items to show per page
@$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0;//if no page var is given, set start to 0

/* Get data. */
$sql = "SELECT * FROM $tbl_name where search_category = '$my' AND sub_category = '$my2' ORDER BY id DESC LIMIT $start, $limit";
$result = mysql_query($sql);

/* Setup page vars for display. */
if ($page == 0) $page = 1;//if no page var is given, default to 1.
$prev = $page - 1;//previous page is page - 1
$next = $page + 1;//next page is page + 1
$lastpage = ceil($total_pages/$limit);//lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;//last page minus 1

/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\">&laquo; previous</a>";
else
$pagination.= "<span class=\"disabled\">&laquo; previous</span>";

//pages
if ($lastpage < 7 + ($adjacents * 2))//not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2))//enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}

//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\">next &raquo;</a>";
else
$pagination.= "<span class=\"disabled\">next &raquo;</span>";
$pagination.= "</div>\n";
}

?>

<?php
while($row = mysql_fetch_array($result)) {
?>

<img src="upload/<?php echo $row['search_category'];?>/<?php echo $row['sub_category'];?>/Software/preview/<?php echo $row['cat'];?>/<?php echo $row['previewfile'];?>"height="180" width="130"/>

<?php
}
?>
<?php $start++; } ?>

<?php echo ($pagination);
?>

 

enigma1

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4444054 posted 7:40 am on Apr 24, 2012 (gmt 0)

when i click on second page then it refresh the page and then it search again post data which is not there because it has been refresh

Yes, there are couple of ways if don't mind the use of js/ajax you could use some js code to send the posted arguments during initial form submission and page number clicks.

Another way is to have a form surrounding the next page numbers which will post the user input.

Another way is to use a $_GET form instead of a $_POST one.

Each method has the pros and cons, for instance with js and form links the browser's back button by default won't do what you expect. The $_GET method it's easier to implement but also means you may expose searches to spiders which maybe a problem.

neo2sxn



 
Msg#: 4444054 posted 7:51 am on Apr 24, 2012 (gmt 0)

$_POST method is used by the first page's dropdown menu so it is my problem to use only $_POST method ....

enigma1

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4444054 posted 9:46 am on Apr 24, 2012 (gmt 0)

The simple way since you want to use $_POST hook the search/submit button with a jscript and send the posted args via ajax to the server. Here is an example with jQuery
[api.jquery.com...]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved