gpt4 book ai didi

PHP - 搜索结果不分页

转载 作者:行者123 更新时间:2023-11-29 03:23:14 27 4
gpt4 key购买 nike

如果在数据库中找到的项目超过 10 个,我会尝试对搜索结果进行分页。出于某种原因,即使代码识别出超过 10 个项目并为后续页面创建链接,所有搜索结果都只列在第一页上。任何人都可以帮忙吗?代码如下:

for($i = 0; $i < $terms_count; $i++)
{
$search_terms_array[$i] = trim($search_terms_array[$i]);
${"query".$i} = $this->mysqli_link->query("SELECT prod_id, prod_tags FROM table WHERE prod_tags LIKE '%" . $search_terms_array[$i] . "%'");
if(${"query".$i}->num_rows < 1)
{
$zerocount++;
}
else
{
$rows = array();
while($row = ${"query".$i}->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
$search_id_results[] = $row['prod_id'];
}
}
}
if($zerocount == $terms_count)
{
echo $this->err_handle->fetch_error_text("search_terms_0_results");
return;
}
else
{
$search_results = array_values(array_unique($search_id_results));
$search_results_count = count($search_results);
$search_page_count = ceil($search_results_count / 10);
$search_page_first_result = ($search_page - 1) * 10;
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo "&nbsp;<strong>" . $i . "</strong>";
}
else
{
echo '&nbsp;<a href="index.php?section=products&amp;action=search&amp;page=' . $i . '">' . $i . '</a>';
}
}
echo '</p><p>&nbsp;</p><p>&nbsp;</p>';
for($i = 0; $i < $search_results_count; $i++)
{
$query = $this->mysqli_link->query("SELECT * FROM table WHERE prod_id='" . $search_results[$i] . "' LIMIT " . $search_page_first_result . ", 10");
while($row = $query->fetch_array())
{
echo "<h4><a href=\"index.php?section=products&amp;subsection=" . $row['prod_category'] . "&amp;prodid=" . $row['prod_id'] . "\">" . $row['prod_name'] . "</h4></a><p><img src=\"includes/images/product_images/" . $row['prod_category'] . "/" . $row['prod_pic_filename'] . "\" alt=\"\" width=\"150\" height=\"200\" /></p><p>Price: £" . $row['prod_price'] . "</p><p>" . $row['prod_code'] . "</p><input type=\"number\" name=\"prod_qty\" maxlength=\"2\" /><input type=\"submit\" name=\"add_to_basket\" value=\"Add To Basket\" /></form></p><p>&nbsp;</p><p>&nbsp;</p>";
}
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo "&nbsp;<strong>" . $i . "</strong>";
}
else
{
echo '&nbsp;<a href="index.php?section=products&amp;action=search&amp;page=' . $i . '">' . $i . '</a>';
}
}
echo '</p><p>&nbsp;</p><p>&nbsp;</p>';
}

最佳答案

好的,所以我找到了解决这个问题的方法,现在完整的功能如下:

public function product_search($search_terms, $search_page, $search_flag_check)
{
if($search_flag_check == "invalid")
{
echo $this->err_handle->fetch_error_text("invalid_ns_term");
return;
}
if($search_terms == "")
{
echo $this->err_handle->fetch_error_text("no_search_string");
return;
}
$search_terms = htmlspecialchars($search_terms);
$search_terms = $this->mysqli_link->real_escape_string(filter_var($search_terms, FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_FLAG_NO_ENCODE_QUOTES));
$search_terms_array = explode(" ", $search_terms);
$terms_count = count($search_terms_array);
$zerocount = 0;
$search_id_results = array();
for($i = 0; $i < $terms_count; $i++)
{
$search_terms_array[$i] = trim($search_terms_array[$i]);
${"query".$i} = $this->mysqli_link->query("SELECT prod_id, prod_tags FROM table WHERE prod_tags LIKE '%" . $search_terms_array[$i] . "%'");
if(${"query".$i}->num_rows < 1)
{
$zerocount++;
}
else
{
$rows = array();
while($row = ${"query".$i}->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
$search_id_results[] = $row['prod_id'];
}
}
}
if($zerocount == $terms_count)
{
echo $this->err_handle->fetch_error_text("search_terms_0_results");
return;
}
else
{
$search_results = array_values(array_unique($search_id_results));
$search_results_count = count($search_results);
$search_page_count = ceil($search_results_count / 10);
$search_page_first_result = ($search_page - 1) * 10;
$search_page_results_limit = 10;
if($search_page_first_result < 1)
{
$search_page_first_result = 1;
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo "&nbsp;<strong>" . $i . "</strong>";
}
else
{
echo '&nbsp;<a href="index.php?section=products&amp;action=search&amp;page=' . $i . '">' . $i . '</a>';
}
}
echo '</p><p>&nbsp;</p><p>&nbsp;</p>';
$search_page_upper_limit = $search_page_first_result + 9;
if(array_key_exists($search_page_upper_limit, $search_results))
{
$search_results_limit = $search_page_first_result + $search_page_results_limit;
}
else
{
end($search_results);
$search_results_limit = key($search_results);
reset($search_results);
}
for($i = $search_page_first_result; $i <= $search_results_limit; $i++)
{
$query2 = $this->mysqli_link->query("SELECT * FROM table WHERE prod_id='" . $search_results[$i] . "'");
$row = $query2->fetch_array();

echo "<h4><a href=\"index.php?section=products&amp;subsection=" . $row['prod_category'] . "&amp;prodid=" . $row['prod_id'] . "\">" . $row['prod_name'] . "</h4></a><p><img src=\"includes/images/product_images/" . $row['prod_category'] . "/" . $row['prod_pic_filename'] . "\" alt=\"\" width=\"150\" height=\"200\" /></p><p>Price: £" . $row['prod_price'] . "</p><p>" . $row['prod_code'] . "</p><input type=\"number\" name=\"prod_qty\" maxlength=\"2\" /><input type=\"submit\" name=\"add_to_basket\" value=\"Add To Basket\" /></form></p><p>&nbsp;</p><p>&nbsp;</p>";

}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo "&nbsp;<strong>" . $i . "</strong>";
}
else
{
echo '&nbsp;<a href="index.php?section=products&amp;action=search&amp;page=' . $i . '">' . $i . '</a>';
}
}
echo '</p><p>&nbsp;</p><p>&nbsp;</p>';
}
}

我想了想,差点放弃,但后来我想到了使用数组键来计算每个搜索页面的限制,在对相关的 PHP 数组函数进行了一些谷歌搜索后,一切都明白了很好。我知道代码现在可能不是很整洁,可能有优化/改进它的方法,但目前它可以完成工作。

关于PHP - 搜索结果不分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40538068/

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