开发者

Pagination not working because of two select statements

开发者 https://www.devze.com 2023-03-22 19:16 出处:网络
$today = date(\'D, d M, Y\'); $sql = \"SELECT * FROM table WHERE date = \'$today\'\"; if ($_POST!=\"\") {
$today = date('D, d M, Y');
$sql = "SELECT * FROM table WHERE date = '$today'";
if ($_POST!="") {   
    $mydate = mysql_real_escape_string($_POST['datepicker']);
    if ($mydate != "") {    
        $sql = "SELECT * FROM table WHERE date = '$mydate'";    
    }       
}
$num_results_per_page = 8;
$num_page_links_per_page = 5;
$pg_param = "";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
pagination($sql, $num_results_per_page, $num_page_links_per_page, $pg_param);
if($pg_error == '')
{
    if(mysql_num_rows($pg_result) > 0)
    {
        while($data = mysql_fetch_assoc($pg_result))
        { 
            echo "";
        }        
        echo "</br>". $pagination_output; 
    }
    else
    {
        echo "No Data.";
    }
}
else
{
    echo $pg_error; 
}

For the above code the pagination is not working properly. I am thinking that it is because of two select statements. It will be OK if we can combine those two. Any suggestions? How can I combine those two select statements?

Pagination is working correctly for select $today, but not working properly for select $mydate. For example if the user clicks page number two in select $mydate, its again going to select $today.

pagination.php

$pg_error = '';
$pg_result = '';
$pagination_output = '';
$max_pages = '';
$page_id = '';
$page_numbers_per_page = '';
$pg_user_param = '';
function pagination($sql, $num_results_per_page, $num_page_links_per_page, $pg_param)
{
global $pg_error, $pg_result, $max_pages, $page_id, $page_numbers_per_page, $pg_user_param; 
$user_sql = $sql;
$page_numbers_per_page = $num_page_links_per_page;
$results_per_page = $num_results_per_page;
$pg_user_param = $pg_param; 
$all_results = mysql_query($user_sql);  
if($all_results)
{   
    if(empty($all_results))
    {
        $total_results = 0; 
    }
    else
    {
        $total_results = mysql_num_rows($all_results); 
    }
    $max_pages = ceil($total_results / $results_per_page);              
    if(isset($_G开发者_如何学CET['page_id']))
    {           
        $page_id = (int) $_GET['page_id'];          

        if($page_id > $max_pages || empty($page_id))
        {
            $page_id = 1;               
        }
    }
    else
    {
        $page_id = 1;           
    }
    $page_id_temp = ($page_id - 1) * $results_per_page;
    $sql_offset = $page_id_temp;
    $user_sql .= " limit $sql_offset, $results_per_page";       
    $pg_result = mysql_query($user_sql);
    Create_Links();     
}
else
{
    $pg_error = 'Error with the sql query you entered: '.mysql_error();
}
}
function Create_Links()
{
global $pagination_output, $max_pages, $page_id, $page_numbers_per_page, $pg_user_param;
$pg_page_name = htmlspecialchars($_SERVER['PHP_SELF'] );

if($max_pages > 1)
{               
    if($page_id > 1)
    {           
        $first_link = '<a href="'.$pg_page_name.'?page_id=1'.$pg_user_param.'">First</a> ';
    }

    if($page_id < $max_pages)
    {           
        $last_link = '<a href="'.$pg_page_name.'?page_id='.$max_pages . $pg_user_param.'">Last</a> ';
    }
    $pre_id = $page_id - 1;
    if($pre_id != 0)
    {
        $pre_link = '<a href="'.$pg_page_name.'?page_id='.$pre_id . $pg_user_param.'">Previous</a> ';
    }       
    $next_id = $page_id + 1;
    if($next_id <= $max_pages)
    {
        $next_link = '<a href="'.$pg_page_name.'?page_id='.$next_id . $pg_user_param.'">Next</a> ';
    }

    if($page_id >= $page_numbers_per_page)
    {

        $start_point = ($page_id - $page_numbers_per_page) + 2;
    }
    else
    {           
        $start_point = 1;
    }

    $loop_num = ($start_point + $page_numbers_per_page) - 1; 
    if($loop_num > $max_pages)
    {
        $loop_num = $max_pages;
    }
    $pagination_output = '<div class="pagination"> ';
    $pagination_output .= $first_link;
    $pagination_output .= $pre_link;        
    for($i = $start_point; $i <= $loop_num; $i++)
    {
        if($i == $page_id)
        {
            $pagination_output .= '<a class="current">'.$i.'</a> ';
        }
        else
        {
            $pagination_output .= '<a href="'.$pg_page_name.'?page_id='.$i . $pg_user_param.'">'.$i.'</a> ';
        }
    }       
    $pagination_output .= $next_link;
    $pagination_output .= $last_link;       
    $pagination_output .= '</div><br />';
}
}
?>


If pagination is working for "$today" then it must be the POST input value - are you making sure you're getting a proper date that mysql can understand? the format should basically match what's in "$today".


I'd imagine that the second click is not POSTing myDate to the next page. The next page then sees nothing in POST and uses today.

To avoid this you could also check for GETs of the datepicker and then pass myDate using GET.

Edit: Clarification on updating the code to use GET The check on mydate should be changed to something like:

if ($_REQUEST!="") {   
    $mydate = mysql_real_escape_string($_REQUEST['datepicker']);
    if ($mydate != "") {    
        $sql = "SELECT * FROM table WHERE date = '$mydate'";    
    }       
}

Which means that if the datepicker is from a GET request we still grab it.
Create_Links() should be changed to add "&datepicker=$mydate" (note that you will need to pass in $myDate to ths function.

0

精彩评论

暂无评论...
验证码 换一张
取 消