gpt4 book ai didi

php - 由于两个 select 语句,分页不起作用

转载 作者:行者123 更新时间:2023-11-30 23:36:41 24 4
gpt4 key购买 nike

$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

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

最佳答案

如果分页适用于“$today”,那么它必须是 POST 输入值 - 您是否确保获得 mysql 可以理解的正确日期?格式应该基本上与“$today”中的内容相匹配。

关于php - 由于两个 select 语句,分页不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6791343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com