iT邦幫忙

1

如何讓PHP列出全部資料

  • 分享至 

  • xImage

這段程式碼瀏覽出來會是兩個下拉式選單,然後我想要讓這兩個選單都是選擇不拘的時候,可以將全部資料列出來,該怎麼做?
VIEW1.PHP其中一段

品牌:<select name="CU" id="CU">
	<option value="*">不拘</option>
	<option value="00">品牌1</option>
	<option value="01">品牌2</option>
	<option value="011">品牌3</option>
	<option value="01A">品牌4</option>
	<option value="02D">品牌5</option>
	<option value="01O">品牌6</option>
</select>
商品:<select name="CC" id="CC">
	<option value="*">不拘</option>
	<option value="A">商品1</option>
	<option value="A1">商品2</option>
	<option value="A2">商品3</option>
	<option value="AB">商品4</option>
	<option value="B">商品5</option>
	<option value="B1">商品6</option>
	<option value="BA">商品7</option>
	<option value="BB">商品8</option>
</select>
<input name="submit" type="submit" value="開始查詢"><br>

VIEW2.PHP其中一段

if($CU=='*'){
$sql = "SELECT * FROM www WHERE CC='$CC'";
}elseif($CC=='*'){
$sql = "SELECT * FROM www WHERE CU='$CU'";
}else{
$sql = "SELECT * FROM www WHERE CU like '$CU' and CC='$CC'";
}
      $result=$db->query($sql);    
    while($row=$result->fetch(PDO::FETCH_OBJ)){    
        
		echo $row->date."\n";
        echo $row->name."\n";  
		echo $row->number."\n";
        echo $row->CC."\n";   
		echo $row->CU."\n";
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
海綿寶寶
iT邦大神 1 級 ‧ 2017-02-13 18:32:54
最佳解答

兩個選單都是選擇不拘的時候,可以將全部資料列出來,該怎麼做?

if ($CU=='*' && $CC=='*'){
   $sql = "SELECT * FROM www";
}

可以了 感謝

這樣就可以了, 真是厲害厲害

2
pratnket
iT邦新手 5 級 ‧ 2017-02-14 01:20:37

VIEW2.PHP其中一段

//POST數值
$CU = $_POST['CU'];
$CC = $_POST['CC'];

//資料庫
$Table = 'www';
//比對值
$search = '*';

//都要等於* CC & CU true 則執行
if(  ($CU == $search) && ($CC == $search) ){
        $sql = "SELECT * FROM www WHERE 1 ";
}else{
    if($CU== $search){
        $sql = "SELECT * FROM {$Table} WHERE CC='" .$CC. "'";
    }else if($CC== $search){
        $sql = "SELECT * FROM {$Table} WHERE CU='" .$CU. "";
    }
}
      $result=$db->query($sql);    
    while($row=$result->fetch(PDO::FETCH_OBJ)){    
        
		echo $row->date."\n";
        echo $row->name."\n";  
		echo $row->number."\n";
        echo $row->CC."\n";   
		echo $row->CU."\n";

建議先規劃好變數在寫程式,這樣只要改...最上頭的變數,就可以特用於其他程式唷!!
幫你整理程式碼...這樣寫不影響到你原本的...

PS.已要離職的美工...所以寫法不是很正規...

1
做工仔人!
iT邦大師 1 級 ‧ 2017-02-14 09:54:44

view2 :

if($CU=='*')  
{
   if ($CC=='*')   ! $CU=='*' AND $CC=='*' !
   {
      $sql = "SELECT * FROM www ";
   }
   else    !$CU=='*' AND $CC <>'*' !         
   {
   $sql = "SELECT * FROM www WHERE CC='$CC'";
   }
}
else
{
    if ($CC=='*')   !$CU <>'*' AND $CC =='*' ! 
    {
      $sql = "SELECT * FROM www WHERE CU='$CU'";
    }else   !$CU <>'*' AND $CC <>'*' ! 
    {
    $sql = "SELECT * FROM www WHERE CU like '$CU' and CC='$CC'";
    }
}
      $result=$db->query($sql);    
    while($row=$result->fetch(PDO::FETCH_OBJ)){    
        
		echo $row->date."\n";
        echo $row->name."\n";  
		echo $row->number."\n";
        echo $row->CC."\n";   
		echo $row->CU."\n";

我要發表回答

立即登入回答