dsmupo6631 2014-03-24 22:06
浏览 113
已采纳

PHP BETWEEN子句用法

PHP

I have having problem with my case, statements. I am trying to search books between 2 years but i am having trouble i can search one year using this code perfectly but trying for two is not working. I do understand i am more than likely going about this the wrong way to get desired result but any help would be greatly appreciated. Also i am getting ERROR Notice: Undefined variable: Year1 for the else part of the last case. Thanks.

If Year and Year1 have a value it should look bettwen the two years if Year just has a value just find books in that 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);
?>
  • 写回答

2条回答 默认 最新

  • doumu1873 2014-03-24 22:14
    关注

    Your code:

    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;
        }
    }
    

    shows two issues:

    1. case statements don't work this way. You can't use boolean operators the same way here like when using an if() statement. (see manual)
    2. You cannot expect the iterator variable $key in foreach($getters as $key=>$value) hold both values at the same time, which you imply by saying 'Year' && 'Year1'!

    To solve those issues, you could do something like:

    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;
        }
    }
    

    In this case the block is executed when the foreach($getters) hits the key 'Year'. The if statement now handles 'Year1' correctly by accessing the value in the array directly instead of looking at the iterator variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面