Jump to content

array problem


jamesxg1

Recommended Posts

Hiya.

 

Ok this is the code in question.

 

                                <?php
                
                if(!isset($_POST) OR empty($_POST)) {
                    
                    echo '<h1 align="center">There was an error with your order!</h1><h2 align="center"><a href="index.php">Start again</a></h2>';                
                } else {
            
            $cnt = array();
            $products = array();
                
            foreach($_POST as $key => $value) {
       
         $key = (int)str_replace('_cnt','',$key);
                    
               $products[] = $key;

               $cnt[$key] = $value;
            
                  
            }
                
                $pcount = count($products);
                
$getstores = "SELECT `store` FROM `stores` ORDER BY `store` ASC";
$rungetstores = mysql_query($getstores) or trigger_error('<font color="red" size="6"><b>Site Error:</b><br />Could not query the selected database.<br /></font>' . mysql_error(), E_USER_ERROR);

$stores = array();
$prices = array();

while($row = mysql_fetch_array($rungetstores)) {

$stores[] = $row['store'];

}

foreach($stores as $shop) {
         
    foreach($products as $item) {
        
$queryy = "SELECT * FROM `products` WHERE id = '$item' AND store = '$shop' LIMIT $pcount";
$resultt = mysql_query($queryy) or trigger_error('<font color="red" size="6"><b>Site Error:</b><br />Could not query the selected database.<br /></font>' . mysql_error(), E_USER_ERROR);

while($price = mysql_fetch_object($resultt)) {
    
$prices[$shop] = $price->price;

} 
}
}

foreach($prices as $place => $much) {
    
echo $place . ' - ' . $much . '<br />'; 
    
   $total +=$much;       
}
                $query = "SELECT `id`, `name`, `price` FROM `products` WHERE id IN(".join($products,',').") LIMIT $pcount";
                $result = mysql_query($query) or trigger_error('<font color="red" size="6"><b>Site Error:</b><br />Could not query the selected database.<br /></font>' .  mysql_error(), E_USER_ERROR);
             
            if(!mysql_num_rows($result)) {

               echo '<h1 align="center">There was an error with your order!</h1><h2 align="center"><a href="index.php">Start again</a></h2>';

            } else {

               echo '<h1><u>You ordered:</u></h1>';

               while($row = mysql_fetch_assoc($result)) {

                  echo '<h2>'.$cnt[$row['id']].' x '.$row['name'].'</h2>';
                      
                            }

               
               echo '<h1><u>Results</u></h1>';
               echo '<h2>Cheapest at: ****</h2>';
               echo '<h2>Amount: £'.$total.'</h2>';
                                                            
                                                           if($total > 50) {             
                                           
                            echo '<a href="" onclick="" class="buttonn">Deliver it!</a>';
                                
                            } elseif ($total < 50) {
                                
                             echo '<a href="" onclick="" class="buttonn">Deliver it for £9.99!</a>';    
                                
                            }
            }
                }
            ?>

 

Ok here's the problem, when I send 2 product id's to this this

 

 echo $place . ' - ' . $much . '<br />'; 

 

only prints out for 1 product id and i need it to print for 2 and its being a nightmare. Also i need to be able to add the the prices from the array with the 'shop' id together and find the lowest total and echo it.

 

This is the array output

 

Array ( [1] => 49 ) Array ( [1] => 99 ) Array ( [1] => 99 [2] => 39 ) Array ( [1] => 99 [2] => 89 ) Array ( [1] => 99 [2] => 89 [3] => 59 ) Array ( [1] => 99 [2] => 89 [3] => 110 )

 

I have 3 shop id's listen in mysql 1,2 & 3.

 

Many thanks

 

James.

Link to comment
https://forums.phpfreaks.com/topic/182417-array-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.