SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How to stop if statement with double results

    Hi,
    I have a simple if statement that would render the result just like this:
    Code:
    if (this->stuff) {
         do this stuff;
    } elseif (that->stuff) {
         do that stuff;
    }
    It happens when I join two tables together. I knew that the query has possible 2 results, which straddled between this->stuff and that->stuff, but how do I stop at only the this->stuff process and not go to the else statement after the first if statement executed.

    I hope I make myself clear with the question above.

    Thanks for your help in advance,
    Ket

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,033
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $count 0;
    while ( 
    /* condition goes here */) {
        
    $count++;
        if (
    this->stuff) {
            do 
    this stuff;
        } elseif (
    that->stuff) {
            if ( 
    $count >= ) {
                continue;
            } else {
                do 
    that stuff;
            }
        }

    That will have it ignore the "do that" if the loop has already been run through at least once. If you want to ignore the "do that" after "do this" has been run once, then move the

    PHP Code:
    $count++; 
    and change the $count check:

    PHP Code:
    $count 0;
    while ( 
    /* condition goes here */) {
        
        if (
    this->stuff) {
        
    $count++;    
            do 
    this stuff;
        } elseif (
    that->stuff) {
            if ( 
    $count >= ) {
                continue;
            } else {
                do 
    that stuff;
            }
        }

    Hope that helps.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks SpacePhoenix, Million Thanks

    This works great.

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Or a better query. That'd work too.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,274
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by ketting00 View Post
    It happens when I join two tables together.
    Quote Originally Posted by StarLion View Post
    Or a better query. That'd work too.
    we can only speculate
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    StarLion is right too, better query works like a charm. Only it happens by accident. And before that accident happened, you would like to smash your head to a wall.

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Good queries shouldn't happen by accident. They should happen on purpose.
    If they happen by accident, I guess some more study is needed

  8. #8
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Location
    Virginia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya, acidental queries puzzle me because the command had to come from some where. Studding is needed indeed.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •