分页没有超值到第二页?

时间:2011-07-23 08:12:02

标签: 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; 
}

Pagination正在为今天的选择$正常工作。在select $ mydate的情况下,分页不会对第二页有价值。如果$ mydate点击的第二页,再次转到$今天。即第二次点击不是将$ mydate发布到下一页。如何将值带到第二页?

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 />';
}
}
?>

3 个答案:

答案 0 :(得分:1)

你的代码中的

函数分页没有返回生成的mysql查询资源,你的代码没有从分页函数调用中接收到

你需要 添加返回$ pg_result;在你的分页功能

并添加$ result = pagination(....

编辑: 对不起,我注意到你把pg_result添加到了global,它不需要返回和我说的解决方案,但你之前不需要mysql_query和fetch,我也看不到你打印结果的位置。只有echo“”,你可以尝试我上次编辑的那个,看看它是否适合你。

编辑:

$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);
$pg_result=pagination($sql, $num_results_per_page, $num_page_links_per_page, $pg_param);


if($pg_error == '' && is_resource($pg_result))
{

    if(mysql_num_rows($pg_result) > 0)
    {
        while($data = mysql_fetch_assoc($pg_result))
        { 
            var_dump($data);
        }        
        echo "</br>". $pagination_output; 
    }
    else
    {
        echo "No Data.";
    }
}
else
{
    echo $pg_error; 
}

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();
            return $pg_result;
}
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 />';
}
}
?>

答案 1 :(得分:0)

看起来你混淆了$_POST$_GET的概念。

让您的分页链接指向'<a href="'.$pg_page_name.'?page_id='.$i . $pg_user_param.'"...在进行SQL查询之前,您应该拦截$_GET['page_id']

您的热门代码引用$_POST['datepicker'],但您没有在任何地方提及表单,您的分页链接肯定不会在您的示例中发布该数据。

答案 2 :(得分:0)

您应该使用$pg_param = "";来传递自己的查询参数。例如:&date=$date_value

$pg_param = "&date=$date_value";