由于两个select语句,分页无法工作

时间:2011-07-22 14:14:46

标签: php mysql

$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; 
}

对于上面的代码,分页无法正常工作。我认为这是因为两个选择语句。如果我们能把这两者结合起来就没问题。有什么建议?如何组合这两个选择语句?

分页对于选择$today正常工作,但对选择$mydate无法正常工作。例如,如果用户在选择$mydate中点击第二页,则会再次选择$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($_GET['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 />';
}
}
?>

2 个答案:

答案 0 :(得分:0)

如果分页适用于“$ today”那么它必须是POST输入值 - 你确定你得到了mysql可以理解的正确日期吗? 格式应该基本上匹配“$ today”中的内容。

答案 1 :(得分:0)

我想第二次点击不是将myDate发布到下一页。然后,下一页在POST中看不到任何东西,今天就使用了。

为了避免这种情况,您还可以检查日期选择器的GET,然后使用GET传递myDate。

编辑:澄清更新代码以使用GET 对mydate的检查应该改为:

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

这意味着如果datepicker来自GET请求,我们仍然会抓住它 应该更改Create_Links()以添加“&amp; datepicker = $ mydate”(请注意,您需要将$ myDate传递给该函数。