2011-07-22 80 views
1
$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; 
} 

對於上面的代碼,分頁不能正常工作。我在想這是因爲兩個選擇語句。如果我們可以將這兩者結合起來就沒問題。有什麼建議麼?我怎樣才能結合這兩個選擇語句?分頁不起作用,因爲有兩條select語句

分頁對於選擇$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 />'; 
} 
} 
?> 
+0

@ saam1234你通過-POST方法發送數據時,點擊頁面二號? – triclosan

+0

@tricolson ...我認爲不是...是的,這是問題嗎?如何解決它? – saam1234

回答

0

如果分頁正在爲「今天$」,那麼它必須是POST輸入值 - 你要確保你得到MySQL能夠了解一個合適的日期? 格式應該與「今日$」中的內容基本匹配。

+0

'$ mydate'分頁輸入正在越來越多。但是,如果點擊第2頁...它再次轉向'$今天' – saam1234

+0

@samaura ...格式都很好... – saam1234

0

我想像第二次點擊不會將myDate發佈到下一頁。下一頁然後在POST中看不到任何內容,並在今天使用。

爲了避免這種情況,您還可以檢查datepicker的GET,然後使用GET傳遞myDate。

編輯:在更新代碼使用GET 上MyDate中檢查應改爲類似澄清:

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

這意味着,如果日期選擇是從一個GET請求,我們還是抓住它。
Create_Links()應改爲增加「&日期選擇器= $指明MyDate」(請注意,您需要在$指明MyDate傳遞給部份功能。

+0

saam1234

+0

@jim ...是的,你是正確的。第二次點擊不會發布$ mydate到下一頁...我編輯了我的問題,並添加了pagi nation.php。請看一看。我如何將$ mydate傳遞給第二頁? – saam1234

+0

@ saam1234增加了更多細節 – Jim