开发者

How do pagination for two select statement together?

Following the php query:

<?php
$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.php

<?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_res开发者_开发技巧ults = 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();
}
}
?>

For the first query pagination is working fine ($today select). But for second one ($mydate select) is not working properly. Its agian going to $today select. Any idea?


You can do a UNION to combine the SQL statements into one result set and use pagination on that.

http://dev.mysql.com/doc/refman/5.0/en/union.html

$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'";    

Can be:

$today = date('D, d M, Y');
if($_POST!=""){
$mydate = mysql_real_escape_string($_POST['datepicker']);
if($mydate!=""){    
$sql = "SELECT * FROM table WHERE date = '$mydate' UNION SELECT * FROM table WHERE date = '$today'";    
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜