Jump to content

Recommended Posts

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

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.

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?

@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.

 

 

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

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.

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.