2014-02-12 67 views
0

我的代碼檢查是否有$ GET值,如果不是,則分配數組的所有值。 看起來像簡單的事情,不知道爲什麼它不工作。SQL中使用的PHP變量

if(isset($_GET["smonth"])) {$smonth= $_GET["smonth"]; 
} 
else {$smonth =12;} working , but not what I want 
else {$smonth =array (1,2,3,4,5,6,7,8,9,10,11) ;} 

之後,我想在SQL中使用它:

and d.month_of_year = '".$smonth."%' 

這將是像

and month_of_year = (all values of array) or 1 value) 

我的問題: 如果有什麼是檢查最好的解決辦法,活躍的月份是可用的?如果不是,將所有個月query.Thank您

+0

你想在今年 – user2754532

回答

0

內置in_array和內爆的PHP函數應該解決您的問題:

in_array('1', $_GET["smonth"]); // checks if January is in $_GET["smonth"] 
implode("," , $_GET["smonth"]); // Pull all of the values out of $_GET["smonth"] as a A STRING 
0

嘗試在聲明中and d.month_of_year IN (" . implode(',', $smonth) . ")

+0

它適用於Arrray的當月進行查詢,但不適合單月 – kaulainais

+0

是的,因爲破滅串接的數組。所以,如果你有一個月,你還必須將它存儲在一個數組中。 – Thiefbrain

0

=運營商檢查單一的價值。如果您想檢查多個值,請使用in

and d.month_of_year in (".$smonth.") 

也有%存在,這與LIKE查詢工作。

0
<?php 

    if(isset($_GET['month'])){ 
    $month = date('m'); //This would give you the index of the current month. 

    $array = array('01','02','02'); 

    $query = "select * from table where month = "; 
    if(in_array($month,$array)){ 
     $query = "select * from table where month = '".$month."'"; 

     //Then query here 
    } 
    else 
    { 
    $query = "select * from table"; 
     $where = ""; 
     foreach($month as $m){ 
      $where .= ' month = "'.$m.'" and '; 
     } 

     //There would be a ending and pls just try remove it 

     $query .= $where; 

     // then query here 
     } 


    } 
?>