jamesxg1 Posted November 21, 2009 Share Posted November 21, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182435-foreach-while-repeating-results/ Share on other sites More sharing options...
jamesxg1 Posted November 21, 2009 Author Share Posted November 21, 2009 Not feeling the love . Lol. Many thanks James. Quote Link to comment https://forums.phpfreaks.com/topic/182435-foreach-while-repeating-results/#findComment-962784 Share on other sites More sharing options...
jamesxg1 Posted November 21, 2009 Author Share Posted November 21, 2009 Advice, help, tutorials. Anything?, anyone ? . Many thanks James. Quote Link to comment https://forums.phpfreaks.com/topic/182435-foreach-while-repeating-results/#findComment-962801 Share on other sites More sharing options...
jamesxg1 Posted November 21, 2009 Author Share Posted November 21, 2009 Please guys all i need is some help or advice. Many thanks James. Quote Link to comment https://forums.phpfreaks.com/topic/182435-foreach-while-repeating-results/#findComment-962818 Share on other sites More sharing options...
jamesxg1 Posted November 21, 2009 Author Share Posted November 21, 2009 I really really need this peeps. Please. Many thanks James. Quote Link to comment https://forums.phpfreaks.com/topic/182435-foreach-while-repeating-results/#findComment-962851 Share on other sites More sharing options...
jamesxg1 Posted November 22, 2009 Author Share Posted November 22, 2009 Ok guys well how do i remove the duplicate keys and values ? Many thanks James. Quote Link to comment https://forums.phpfreaks.com/topic/182435-foreach-while-repeating-results/#findComment-962875 Share on other sites More sharing options...
jamesxg1 Posted November 22, 2009 Author Share Posted November 22, 2009 I have tried array_unique() and array_flip(array_flip()) none of them are working. Many thanks James. Quote Link to comment https://forums.phpfreaks.com/topic/182435-foreach-while-repeating-results/#findComment-962876 Share on other sites More sharing options...
DavidAM Posted November 22, 2009 Share Posted November 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182435-foreach-while-repeating-results/#findComment-962888 Share on other sites More sharing options...
jamesxg1 Posted November 22, 2009 Author Share Posted November 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182435-foreach-while-repeating-results/#findComment-962896 Share on other sites More sharing options...
jamesxg1 Posted November 22, 2009 Author Share Posted November 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182435-foreach-while-repeating-results/#findComment-962929 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.