I have a query bringing back rows from a database. And my PHP is generating pages and inserting this content. What I'm stuck on is this: I need one thing to happen if a certain field is '0' and another thing to happen if it's '1'.

So I'm trying to:
do {
etc. etc. etc.
if ('certain_field' = 0) { // restart this DO loop
} else {
etc. etc. etc.
}
// since the 'certain_field' == 1 write this as well.
} while (condition == true)

I've seen 'break' and 'exit' but how do I get the do loop to actually restart itself and grab the next row in the query without breaking out of the loop?

    This is kinda lame example but you can put your loop in a function and call itself inside that function:

    $t = array(1,2,3,4,5,6,7,8,9,10,11);
    
    dosomething($t);
    
    function dosomething($t) {
            foreach ($t as $key=>$value) {
                    echo $key.': '.$value."\n";
                    if ($value==5) {
                            $t[$key]='foo';
                            dosomething($t);
                            break;
                    }
            }
            return;
    }
    

      This is kinda funny - because I actually tried something like that earlier. A foreach loop ... but the thing is, the MySQL that comes back in my queries isn't in an array ... and if it is, then I don't know what I'm doing which is more likely the case. Every time I tried to run an array function like foreach on my returns, I'd get an error ... 'invalid data type' or something along those lines. So I scratched this whole approach.

      A few minutes ago, I found the solution to my problem. It's 'continue;' - that's what tells the for or while loop to go test it's validity again skipping the rest of the code. So that's what's inside my if statement now.

      Cahva - thanks so much for the response. I really appreciate you trying to help me out. Funny how we both sought the same solution ... I'm just too stupid to have figured it out like you did. 🙂 Thanks though.

        Write a Reply...