johnmerlino Posted April 3, 2011 Share Posted April 3, 2011 Hey all, I'm wondering the best way to use foreach to break a list of records into groups. For example, the problem with this: foreach($vanity_url as $v){ if($v->approved){ echo "Approved vanity urls:"; echo $v->url; } else { echo "Unapproved vanity urls:"; echo $v->url; } } is it will return this: Approved vanity urls: michellefrancis Unapproved vanity urls: johnmerlino Unapproved vanity urls: ericmayers when I want it to return this: Approved vanity urls: michellefrancis Unapproved vanity urls: johnmerlino ericmayers thanks for response Quote Link to comment https://forums.phpfreaks.com/topic/232541-using-foreach-to-break-a-list-into-groups/ Share on other sites More sharing options...
dcro2 Posted April 3, 2011 Share Posted April 3, 2011 Put 'em into two arrays? //might have scope problems otherwise? $approvedurls = array(); $unapprovedurls = array(); foreach($vanity_url as $v){ if($v->approved){ $approvedurls[] = $v->url; } else { $unapprovedurls[] = $v->url; } } echo "Approved vanity urls:"; foreach($approvedurls as $url) echo $url; echo "Unapproved vanity urls:"; foreach($unapprovedurls as $url) echo $url; I'm still confused how you got that output without any newlines however. Quote Link to comment https://forums.phpfreaks.com/topic/232541-using-foreach-to-break-a-list-into-groups/#findComment-1196129 Share on other sites More sharing options...
PFMaBiSmAd Posted April 3, 2011 Share Posted April 3, 2011 a list of records ^^^ If that means database records, why not just get your query to return the results in the order that you want them and then simply iterate over the records (once) and display them the way you want? Quote Link to comment https://forums.phpfreaks.com/topic/232541-using-foreach-to-break-a-list-into-groups/#findComment-1196131 Share on other sites More sharing options...
johnmerlino Posted April 3, 2011 Author Share Posted April 3, 2011 @PFMaBiSmAd So you are saying create two variables: $approved = "select * from vanity_urls where approved=1"; $unapproved = "select * from vanity_urls where approved=0"; and then iterate through the variables? echo "approved:"; foreach($approved as $a) echo $a->url; echo "unapproved:"; foreach($unapproved as $u) echo $u->url; That is faster than storing them in arrays in php and iterating through the php arrays? Thanks for response. Quote Link to comment https://forums.phpfreaks.com/topic/232541-using-foreach-to-break-a-list-into-groups/#findComment-1196295 Share on other sites More sharing options...
johnmerlino Posted April 3, 2011 Author Share Posted April 3, 2011 or do you mean: $results = "select * from vanity_urls order by approve asc"; foreach($result as $r) echo $r->url; Quote Link to comment https://forums.phpfreaks.com/topic/232541-using-foreach-to-break-a-list-into-groups/#findComment-1196296 Share on other sites More sharing options...
PFMaBiSmAd Posted April 3, 2011 Share Posted April 3, 2011 Assuming you want the approved ones first - select * from vanity_urls order by approved desc Add any other ORDER BY condition after the approved desc, for example - select * from vanity_urls order by approved desc, url asc Quote Link to comment https://forums.phpfreaks.com/topic/232541-using-foreach-to-break-a-list-into-groups/#findComment-1196299 Share on other sites More sharing options...
johnmerlino Posted April 4, 2011 Author Share Posted April 4, 2011 Assuming you want the approved ones first - select * from vanity_urls order by approved desc Add any other ORDER BY condition after the approved desc, for example - select * from vanity_urls order by approved desc, url asc That works pretty well. Thanks for response. Quote Link to comment https://forums.phpfreaks.com/topic/232541-using-foreach-to-break-a-list-into-groups/#findComment-1196819 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.