Jump to content

Recommended Posts

Hiya peeps.

 

I have a problem with a while and a foreach, whats happening is the results are all correct but they are getting repeated.

 

<?php
            
            $cnt = array();
            $products = array();
            
            foreach($_POST as $key => $value) {
  
               $products[] = $key;

               $cnt[$key] = $value;  
                  
            }
                
                $pcount = count($products);
                $stores = array();
                $prices = array();
                $i = 0;
                         
$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);

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[$price->store] = $price->price;

}
  foreach($prices as $key => $val) {
     
    echo "$key : $val\n<br>";
} 
} 
}
?>

 

this displays

 

1 : 49

1 : 99

1 : 99

2 : 39

1 : 99

2 : 89

1 : 99

2 : 89

3 : 59

1 : 99

2 : 89

3 : 110

 

the results should be

 

1 : 49

2 : 39

3 : 59

1 : 99

2 : 89

3 : 110

 

how do I fix this ?

 

Many thanks

 

James.

Link to comment
https://forums.phpfreaks.com/topic/182435-foreach-while-repeating-results/
Share on other sites

Man this new layout is hard to read!

 

OK. Your problem is that you are loading the $prices array inside the foreach loops (stores -- products).  After printing the $prices array, you do not clear it, so the values are still in it.  Then you add more elements to the array in the next run through the products/stores loop and you print the ENTIRE array.  Try clearing the $prices array ( $prices = array() ) just before the while($price = mysql_fetch_object($resultt)) loop.

Man this new layout is hard to read!

 

OK. Your problem is that you are loading the $prices array inside the foreach loops (stores -- products).  After printing the $prices array, you do not clear it, so the values are still in it.  Then you add more elements to the array in the next run through the products/stores loop and you print the ENTIRE array.  Try clearing the $prices array ( $prices = array() ) just before the while($price = mysql_fetch_object($resultt)) loop.

 

Haha, i know init.

 

And oh.....omg.....looking at it you are so right....its pretty much worked except its printing 2 of the prices from the product id's EG.

 

this is what the array prints

 

1 => 49

1 => 99

2 => 39

2 => 89

3 => 59

3 => 110

 

there should be 3 prices for each product and thank you so much for replying mate you really have save my bacon.

 

Many many thanks

 

James.

Fixed!, all I have to do now is get the $sps var to work right but its only adding half of the data to the array.

 

                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();
            $stores = array();
            $prices = array();
            $sps = array();
            
            foreach($_POST as $key => $value) {
  
               $products[] = $key;

               $cnt[$key] = $value;  
                  
            }
                
                $pcount = count($products);
                
$getstores = "SELECT * FROM `stores`";
$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); 

          $mcount = mysql_num_rows($rungetstores);
           
while($row = mysql_fetch_object($rungetstores)) {   
                  
    foreach($products as $item) {                                  
                   
$queryy = "SELECT * FROM `products` WHERE id = '$item' AND store = '$row->store' LIMIT $mcount";
$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);
                    $prices = array();
                    
while($price = mysql_fetch_object($resultt)) {
    
$prices[$row->store . '_' . $price->id] = $price->price;  
     
} 
   
  foreach($prices as $key => $val) { 
      list($s, $p) = explode("_", $key);
    echo $s . ' => ' . $p . ' => ' . $val . '<br />';
    $sps[$s] = $p; 
    
  }
  
  foreach($sps as $s => $p) {
    
    $total+=$val; 

}
}  
}  

 

if there like a WHERE for php ? or a way of doing foreach($VAR) so i can either do something like so $s WHERE $s or foreach($s) ($s; has 2 value's 4 & 5) so i can do something for all the $s if they have the same value.

 

So basically where it says 5 as the value i need to do something will all of the keys and where it says 4 i need to do something with all them keys aswell.

 

Is this possible ?

 

$s = Array ( [1] => 5 [2] => 5 [3] => 5 [1] => 4 [2] => 4 [3] => 4);

 

Mant thanks

 

James.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.