mysql,PDO和php有多个选择搜索

时间:2016-03-19 12:28:04

标签: php mysql sql pdo

我有商店产品清单

的表格数据
  <form method="post" action="results.php">
    <select name="product[]" multiple>
        <?php $showp=$db->query("SELECT * FROM product ORDER BY productname ASC"); 
            $showte = $showp->fetchAll();
            foreach($showte as $runshow){
            $productname=$runshow['productname'];
            $productid=$runshow['id']; ?>
                <option value="<?php echo $productname; ?>"><?php echo $productname; ?></option>
        <?php } ?>
    </select>
    <input type="submit" name="submit" value="Search"/>
</form>

现在用户搜索多个值:

的search.php

  if(isset($_POST['submit'])){
        foreach($_POST['product'] as $productselect){
        echo $productselect."<br>";
        $query = $db->query("SELECT * FROM product WHERE testid LIKE '$productselect' ");
        $new = $query->fetchAll();
            if(count($new)){
                foreach($new as $show){ 
                    echo $show['shopname']; 
                    echo "productname = ".$show['productname']."<br/>"; }
            }
            else{ echo "No Result Found<br/>"; }    
        }
    }/* serech if isset close */

现在是result.php

   suger 

   suraj store 
   ram store

   oil 

   suraj store 
   ram store

   batter 

   ram store 

现在选择用户是suger,oil,batter

用户结果显示为: -

   suraj store 

   suger
   oil 


   ram store

   suger
   oil 
   batter

结果显示还可以,但我希望在商店列表中显示

<kentor.authServices entityId="https://mysite.azurewebsites.net/AuthServices"
               returnUrl="https://mysite.azurewebsites.net/"
               authenticateRequestSigningBehavior="Never">
<identityProviders>
  <add entityId="http://www.okta.com/1111111"
       allowUnsolicitedAuthnResponse="true" binding="HttpRedirect" 
       metadataLocation="https://dev-11111.oktapreview.com/app/1111111/sso/saml/metadata"
       loadMetadata="true">
    <signingCertificate fileName="~/App_Data/okta.cert" />
  </add>
</identityProviders>

请说明我是怎么做的..

提前感谢您的支持

1 个答案:

答案 0 :(得分:1)

不要在result.php中回显查询结果 而是制作包含商店名称和与之相关的产品的数组。

而不是:

echo $show['shopname']; 
echo "productname = ".$show['productname']."<br/>";

这样做:

$shopProducts[$show['shopname']][] = $show['productname'];

在文件打印结果的最后:

foreach ($shopProducts as $shopName) {
    echo $shopName . ":\n";
    foreach ($shopProducts[$shopName] as $product) echo "$product\n";
}