Jump to content

grouping within a foreach?


samoht

Recommended Posts

hello all,

 

I currently have a list of products spit out from my db via a foreach loop. Right now I am styling these as a <li>

 

I want to style the list so that the annual and the monthly products are grouped together. This will probably mean using <div> instead of <li> and placing another loop inside the foreach to grab two products and put them in one div?

 

Any ideas how this might be structured??

 

should I use a for loop?

 

here is the current code:

$productnames   = '<center><table><tr><td><ul id="prodlist">'."\n";	// list of products that can view this content
			$show2all       = false;											// are one of the products the "all" flag?
			$numcanview     = 0;												// the number of products that can view this post

			foreach ($_product_id as $pid)
			{

				$pid = trim(strtolower($pid));								// make sure no extra spaces and not "ALL" or something

				if ($pid=="all")
				{

					$show2all = true;										// used later

				} else {

					$purl = $this->amprotectamroot.'member.php?price_group='.$allthepricegroups[$pid].'&product_id='.$pid;

					$productnames .= '<li><a href="'.$purl.'">'.$alltheproducts[$pid].'</a></li>'."\n";

					$numcanview++;

				}

Link to comment
Share on other sites

The Annual is simply one product and Monthly is another.

So maybe I could collect them via the Name? But I thought I could use php because I know the output is going to be ordered alphabetically. So I would get a list like

 

ppGold Annual

ppGold Monthly

ppPro Annual

ppPro Monthly

 

etc.

 

So I know that every 2 products will belong together.

 

Link to comment
Share on other sites

If the names of the items are saved in the database, why not use the ORDER BY parameter for a SELECT SQL statement to sort them alphabetically by product name?

 

Trying to do it by PHP is going to require a lot of work both of you and the server and will be far less efficient.

Link to comment
Share on other sites

OK,

 

if I use the ORDER BY - I'm not sure how that gets me what I want?

 

The HTML I am hoping to arrive at would look like:

<div>
  <span>ppGold Annual</span>
  <span>ppGold Monthly</span>  
</div>
<div>
  <span>ppPro Annual</span>
  <span>ppPro Monthly</span>  
</div>

 

 

Link to comment
Share on other sites

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.