$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 />';
}
}
?>
@ saam1234你通過-POST方法發送數據時,點擊頁面二號? – triclosan
@tricolson ...我認爲不是...是的,這是問題嗎?如何解決它? – saam1234