0

我有一个带有一些复选框的页面,如果只选择一个复选框,它工作正常,但是如果我选择 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>";
    }
}

?>

如果有人可以提供帮助或指出正确的方向,我将非常感激

4

1 回答 1

0

您确定要将所有内容都放在该foreach循环中吗?看起来您根本不需要该foreach循环,因为您将条件项作为一个组使用,而不是单独使用:

$clean_criteria = array_map('mysql_real_escape_string', $_REQUEST['criteria']);
$criteria = implode("','", $clean_criteria);

这意味着您的查询将对指定的每个条件执行一次,但每个查询都已经查看了所有条件。您从不引用$keyor $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>";
}
?>
于 2013-04-14T14:43:48.367 回答