emma57573 Posted August 10, 2008 Share Posted August 10, 2008 What Im doing is a payment form that prints out a list of items purchased. The items are purchased from various sellers. Im trying to group the items by the same seller together I've started by grouping the items by 'order by seller_user_id' Now what I want to do is change how the list of items FROM THE SAME SELLER are displayed. So to start me off below what im trying to do is say :if the seller_user_id has been printed on the page once already then dont print the seller_user_id on the page If the seller_user_id hasnt been printed on the page then print seller_user_id Im trying to do this by putting seller_user_id into an array in the while loop and searching the array for duplicate keys. Im not getting it right thought. If someone could point out what im doing wrong I would be greatful. Thank you <?php $query="select * from MYTABLE where buyer_user_id=".$userid." and to_pay=1 order by seller_user_id DESC"; $sql=mysql_query($query); $rst=mysql_fetch_array($sql); while($rst) { $seller_id= $rst["seller_user_id"]; $countseller= array($seller_id); if (array_key_exists($seller_id, $countseller)){ }else{ echo $rst["seller_user_id"]; } } ?> Link to comment https://forums.phpfreaks.com/topic/119012-array_key_exists/ Share on other sites More sharing options...
JasonLewis Posted August 10, 2008 Share Posted August 10, 2008 Your resetting the $countseller array every loop. First, declare the variable outside the loop and declare it as an empty array. Then in the loop add elements to the array by using this syntax: $countseller[] = $seller_id; Oh and you would want to check if the id is in the array before adding it to the array. And instead of using array_key_exists() use in_array(). Good luck. Link to comment https://forums.phpfreaks.com/topic/119012-array_key_exists/#findComment-612827 Share on other sites More sharing options...
emma57573 Posted August 10, 2008 Author Share Posted August 10, 2008 thank you. My sites down now while its moving to a secure server so I need to wait to test it now. But thanks for the help I will let you know how I get on. Emma Link to comment https://forums.phpfreaks.com/topic/119012-array_key_exists/#findComment-612831 Share on other sites More sharing options...
emma57573 Posted August 15, 2008 Author Share Posted August 15, 2008 Ive just gone back to this and still not getting it right Im trying this if ((in_array($seller_id, $countseller))&&($countseller > 1)){ So Im searcing in the array for $seller_id if that crops up in $countseller array more than once then do whatever i need to do. I hink this is counting the whole of the array though? So how do I just count the $seller_id? Link to comment https://forums.phpfreaks.com/topic/119012-array_key_exists/#findComment-617275 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.