我有一个带有一些复选框的页面,如果只选中了一个复选框,它就可以正常工作,但是如果我选择了2个或更多的复选框,结果就会显示得很好,但脚本切断后页面的其余部分就会显示出来。任何会导致这种情况发生的想法。
<?php
if (isset($_POST['criteria']) && !empty($_POST['criteria'])) {
foreach($_POST['criteria'] as $key => $value) {
// get the function
include ($_SERVER['DOCUMENT_ROOT'] . '/scripts/pagination-functions.php');
$page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 14;
$startpoint = ($page * $limit) - $limit;
// Runs mysql_real_escape_string() on every value encountered.
$clean_criteria = array_map('mysql_real_escape_string', $_REQUEST['criteria']);
// Convert the array into a string.
$criteria = implode("','", $clean_criteria);
// to make pagination
$statement = "table WHERE page IN ('$criteria') ORDER BY long_title ASC";
if (!$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}")) {
echo "Cannot parse query";
}
elseif (mysql_num_rows($query) == 0) {
echo "No records found";
}
else {
echo "";
while ($row = mysql_fetch_assoc($query)) {
echo "" . $row['name'] . "<br />
" . $row['description'] . "";
}
}
echo "<div class=\"pagination\">";
echo pagination($statement, $limit, $page);
echo "</div>";
}
}
?>如果有人能帮助我或指出正确的方向,我将不胜感激。
发布于 2013-04-14 22:43:48
您确定要将所有内容都放在那个foreach循环中吗?看起来您根本不需要使用foreach循环,因为您将条件项作为一个组使用,而不是单独使用:
$clean_criteria = array_map('mysql_real_escape_string', $_REQUEST['criteria']);
$criteria = implode("','", $clean_criteria);这意味着您的查询将针对每个指定的单个条件执行一次,但每个查询都已经查看了所有条件。您从来没有引用过$key或$value,所以我不认为这个循环有用。
重新编写的代码示例
<?php
if (!empty($_POST['criteria'])) {
// get the function
include ($_SERVER['DOCUMENT_ROOT'] . '/scripts/pagination-functions.php');
$page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 14;
$startpoint = ($page * $limit) - $limit;
// Runs mysql_real_escape_string() on every value encountered.
$clean_criteria = array_map('mysql_real_escape_string', $_POST['criteria']);
// Convert the array into a string.
$criteria = implode("','", $clean_criteria);
// to make pagination
$statement = "table WHERE page IN ('$criteria') ORDER BY long_title ASC";
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}");
if (!$query) {
echo "Cannot parse query";
} elseif (mysql_num_rows($query) == 0) {
echo "No records found";
} else {
while ($row = mysql_fetch_assoc($query)) {
echo $row['name'] . "<br />" . $row['description'] . "<br /><br />";
}
}
echo "<div class=\"pagination\">";
echo pagination($statement, $limit, $page);
echo "</div>";
}
?>https://stackoverflow.com/questions/16000192
复制相似问题