Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Condictional Loop


Condictional Loop

Reply
  #1 (permalink)  
Old 11-06-09, 10:35 AM
protocode protocode is offline
Newbie Coder
 
Join Date: May 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Condictional Loop

I do not have example code to post, for right now I am just thinking conceptually. What I can't figure out is how to loop values under a header (where the values are associated with the header).

Problem: Pulled information is displayed like this:

Value A_1, Value B_1, Value C_1
Value A_1, Value B_1, Value C_2
Value A_1, Value B_2, Value C_3

Desired Output:

Value A_1
Value B_1
Value C_1
Value C_2
Value B_2
Value C_3
The header (A_1) should display ONCE then the rest of the information display, repeatedly, until there is a change in the header.

Thank you in advance

Last edited by protocode; 11-06-09 at 10:43 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-06-09, 11:38 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
There are various ways to solve this kind of problem. You can create parent-child relationships between the elements or use a hierarchical 'tree' to organize the elements, then display them based on the parent-child relationship or their depth in the tree.

You could also solve it programmatically by storing the current "master" header value and checking to see when it changes as you loop through the values> When it does, you then fetch the next header value and continue.

Without knowing a little more about what your intent or goal is, it's difficult to get more specific.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 11-06-09, 01:24 PM
protocode protocode is offline
Newbie Coder
 
Join Date: May 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
I'm not quite following on the parent-child relationship. And would the loop be best constructed with a while or for?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 11-06-09, 01:28 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
I would build an array, then use a foreach loop to climb it. That would be the easiest way to build a parent child relationship.

Well there is an easier, but it involves women...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 11-06-09, 02:42 PM
protocode protocode is offline
Newbie Coder
 
Join Date: May 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
I've searched for a parent-child relationship scheme. Do you have an info source to direct me to?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 11-06-09, 03:12 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Quote:
Value A_1, Value B_1, Value C_1
Value A_1, Value B_1, Value C_2
Value A_1, Value B_2, Value C_3
PHP Code:

$aPC=array(array('A_1'=>array('B_1'=>array('C_1','C_2'),'B_2'=>array('C_3'));


foreach (
$aPC as $k=>$v)
{
  echo 
$k.'<br />';
  foreach (
$v as $kk=>$vv)
  {
    echo 
$kk.'<br />';
    foreach(
$vv as $kkk=>$vvv)
      echo 
$vvv.'<br />';
  }

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to wirehopper For This Useful Post:
protocode (11-06-09)
  #7 (permalink)  
Old 11-06-09, 04:20 PM
protocode protocode is offline
Newbie Coder
 
Join Date: May 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Thank you very much this has set me in the right direction.

I added some more and got basically what I was looking for.
PHP Code:

$aPC = array('top'=> 

                    (
                    array(
                            
'A_1'=>
                                    array(
                                            
'B_1'=>array('C_1' => 'Test C_1','C_2'),
                                            
'B_2'=>array('C_3'
                                            ),
                            
'A_2'=>
                                    array(
                                            
'B_3'=>array('C_4')
                                          )
                    )
            )
        );

foreach (
$aPC as $k=>$v)
{
  
//echo $k.'<br />';
  
foreach ($v as $kk=>$vv)
  {
    echo 
$kk.'<br />';
    foreach(
$vv as $kkk=>$vvv)
    {
      echo 
"&nbsp;".$kkk.'<br />';
      foreach(
$vvv as $kkkk=>$vvvv)
      {
          echo 
"&nbsp;&nbsp;".$vvvv.'<br />';
      }    
      }
  }

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop items in session for Paypal form mdhall PHP 0 05-03-09 06:48 PM
infinite loop versus guarded blocks UnrealEd Everything Java 0 11-11-07 03:53 PM
Update multiple rows outside loop - need help ElvansX PHP 1 12-03-06 02:55 AM
Total mysql_num_rows on while loop dihan PHP 2 08-21-05 04:31 PM
While loop jaishalg PHP 1 11-23-04 04:36 PM


All times are GMT -5. The time now is 11:40 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.