1

i have a table data of shop product list

 id | product name | shop Name 
 ----------------------------
 1  | Suger        | Ram Store 
 -----------------------------
 2  | Oil          | Suraj Store 
 -------------------------------
 3  | Batter       | Ram Store 
 ------------------------------
 4  | Maida        | Ram Store 
 --------------------------------
 5  | Rice         | Ram Store 
 -------------------------------- 
 6  | Suger        | Suraj Store 
--------------------------------
 7  | Oil          | Ram Store 
 -------------------------------- 
 8  | Rice         | Suraj Store 

now user search multiple value :

search.php

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

now is result.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 */

now select user is suger , oil , batter

user result show is : --

   suger 

   suraj store 
   ram store

   oil 

   suraj store 
   ram store

   batter 

   ram store 

result show is ok but i want show in shop list like

   suraj store 

   suger
   oil 


   ram store

   suger
   oil 
   batter

please say how i do this ..

thanks in advance for your support

4
  • print_r($new); is what? Commented Mar 19, 2016 at 12:36
  • foreach($new as $show){ echo $show['shopname']; echo "productname = ".$show['productname']."<br/>"; } } should shop_name first and name second only // this code is correct Commented Mar 19, 2016 at 12:36
  • where is the problem and how to complete this work .. Commented Mar 19, 2016 at 13:03
  • print_r($new); can you show the output Commented Mar 21, 2016 at 4:03

1 Answer 1

1

Do not echo results of query in result.php Instead make array which will contain shop names and products associated with them.

Instead of:

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

Do sth like this:

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

And on the very end of file print results:

foreach ($shopProducts as $shopName) {
    echo $shopName . ":\n";
    foreach ($shopProducts[$shopName] as $product) echo "$product\n";
}
Sign up to request clarification or add additional context in comments.

1 Comment

I need additional information, because illegal offset type error usually shows up when object is being used as key in array, and code I've suggested should not produce such error. Probably some other things you've done with code are producing this error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.