的PHP

我的案子有问题,陈述。我正在尝试搜索2年之间的图书,但遇到麻烦,我可以完美地使用此代码搜索一年,但尝试两年都无法正常工作。我确实知道我很可能会以错误的方式获得期望的结果,但是任何帮助将不胜感激。
我也收到错误通知:未定义的变量:Year1为最后一种情况的else部分。谢谢。

如果Year和Year1具有值,则在Year仅具有值的那两年就应该查找该年份的书。

<?php
include 'header.php';
include 'searchscript.php';

$sql =  "SELECT DISTINCT bk.title AS Title, bk.bookid AS BookID, bk.year AS Year, bk.publisher AS Publisher, aut.authorname AS Author
         FROM book bk

         JOIN book_category bk_cat
         ON bk_cat.book_id = bk.bookid

         JOIN categories cat
         ON cat.id = bk_cat.category_id

         JOIN books_authors bk_aut
         ON bk_aut.book_id = bk.bookid

         JOIN authors aut
         ON aut.id = bk_aut.author_id";

if(isset($_GET['searchInput'])){
$input = $_GET['searchInput'];
$input = preg_replace('/[^A-Za-z0-9]/', '', $input);
}
if (isset($input)){

    $getters = array();
    $queries = array();

    foreach ($_GET as $key => $value) {
        $temp = is_array($value) ? $value : trim($value);
        if (!empty($temp)){
        if (!in_array($key, $getters)){
            $getters[$key] = $value;
            }
        }
    }

    if (!empty($getters)) {

        foreach($getters as $key => $value){
            ${$key} = $value;
            switch ($key) {
                case 'searchInput':
                    array_push($queries,"(bk.title LIKE '%$searchInput%'
                    || bk.description LIKE '%$searchInput%' || bk.isbn LIKE '%$searchInput%'
                    || bk.keywords LIKE '%$searchInput%' || aut.authorname LIKE '%$searchInput%')");
                break;
                case 'srch_publisher':
                    array_push($queries, "(bk.publisher = '$srch_publisher')");
                break;
                case 'srch_author':
                    array_push($queries, "(bk_aut.author_id = '$srch_author')");
                break;
                case 'srch_category':
                    array_push($queries, "(bk_cat.category_id = '$srch_category')");
                break;
                **case 'Year' && 'Year1':
                    if("$Year1" ==""){
                        array_push($queries, "(bk.year = '$Year')");
                    } else {
                        array_push($queries, "(bk.year BETWEEN '$Year' AND '$Year1')");
                    }
                break;**
        }
    }
}

if(!empty($queries)){
    $sql .= " WHERE ";
    $i = 1;
    foreach ($queries as $query) {
        if($i < count($queries)){
            $sql .= $query." AND ";
        } else {
            $sql .= $query;
        }
        $i++;
    }
}
$sql .= " GROUP BY bk.title ORDER BY bk.title ASC";

}else{
    $sql .= " GROUP BY bk.title ORDER BY bk.title ASC";
}


$rs = mysql_query($sql) or die(mysql_error());
$rows = mysql_fetch_assoc($rs);
$tot_rows = mysql_num_rows($rs);
?>

最佳答案

您的代码:

foreach($getters as $key => $value)
    switch ($key) {
        case 'Year' && 'Year1':
            if("$Year1" ==""){
                array_push($queries, "(bk.year = '$Year')");
            } else {
                array_push($queries, "(bk.year BETWEEN '$Year' AND '$Year1')");
            }
        break;
    }
}


显示两个问题:


case语句不能这样工作。您不能像使用if()语句那样使用布尔运算符。 (see manual
您不能指望$key中的迭代器变量foreach($getters as $key=>$value)同时包含两个值,这是通过说'Year' && 'Year1'来暗示的!


要解决这些问题,您可以执行以下操作:

foreach($getters as $key => $value)
    switch ($key) {
        case 'Year':
            if($getters["Year1"] ==""){
                array_push($queries, "(bk.year = '{$value}')");
            } else {
                array_push($queries, "(bk.year BETWEEN '{$value}' AND '{$getters['Year1']}')");
            }
        break;
    }
}


在这种情况下,当foreach($getters)按下键'Year'时,将执行该块。现在,if语句通过直接访问数组中的值而不是查看迭代器变量来正确处理'Year1'

08-07 19:31