Two Simultaneous While Loops?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
VmusicV
Forum Newbie
Posts: 23
Joined: Sun Jun 15, 2003 10:45 pm
Location: Columbus

Two Simultaneous While Loops?

Post by VmusicV »

Hi,
I want to loop through either a) A result set or b) an array only once so I can process the elements once in code? is there a way to do this?



So... ahead of time, while I'm writing the code, I can't tell which 'condition' will exist. Either I'll need to loop through a query result, or loop through an array. The way or the code I use to process either will be the same, but I can't figure out how to make two simultaneous/conditional loops?


Any Ideas?

maybe one idea is to call out a common function from within each of loops separately?

Code: Select all

if($condition_to_use_query === true){
     //// query the data into $result
     while($row  =  mysql_fetch_array($result))
     {
          displayElement($row[0]);
     }
}else{
     ///loop through the array
     foreach($my_array as $key => $value){
              displayElement($row[0]);
     }
}


Any other ideeeeers?

VmusicV
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Post Reply