Results 1 to 9 of 9

Thread: if limitation?

  1. #1
    Join Date
    Sep 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default if limitation?

    Is there a limit to the number of conditions you can put in an if clause? I have this:
    if ($choice == 03 || $choice == 04 || $choice == 05 || $choice == 07 || $choice == 08)

    and it works, but if I add any more it causes weird things on my page. I need to have 11 more choices available... is there a different way to do this?

  2. #2
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    I don't think there is a limit. What is the exact situation so we can suggest an alternative?

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You can always split it up more.
    Just do like 7 if statements, each setting the $check to 1 if true, then a final check sees if $check == 1, and it's a little easier to deal with.
    But, that's not a better way, just a bit less complex to look at.

    using if, elseif, then finally else is another way.

    Perhaps in this particular case, you could check if the variable HAS a value, using if (isset($var)) {}.

    Or, since they seem to be in a range, perhaps:
    if ($choice < 10 && $choice > 01) {}


    Also, it might be easier to do a "not if".... so use:
    if ($choice != 09)
    That would be the easy way if all other options were right, and only 09 was wrong. You would also want to use this is the possible values of $choice were more likely to be true, as it would save you time.
    However, you might end up with a weird value, in which case you'd have it being true when it isn't.



    If you have a LOT of possible values, the easy way would be to store them into an array, then do a loop checking if the variable is equal to anything in the array.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Sep 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your replies. What was happening before is that the page would return some values even before a choice was selected. I'm not sure how it was doing that, unless I had screwed up the code somehow when I added the extra choices, but I checked it all carefully...

    What I have done for now, is to make it a search box instead of listing all of the choices individually, so then I can just do the choice off of the search criteria. it is working so far, but I want to add an additional criteria and I am not sure how to do it.

    I currently have a seach for the Branch field, and I would like to also have a search for the Group field, and then if both are filled in, it would check for both, or it would check for which ever one is filled in. Can I do this?

    Here are the parts I have:
    PHP Code:
    <FORM METHOD="get" ACTION="mailtest3.php">
    <
    INPUT TYPE=hidden NAME=choice VALUE=request>
    Select a Branch (Enter the 2 digit branch number)
    <
    INPUT TYPE="text" SIZE="02" NAME="search" VALUE="">
    <
    INPUT TYPE="submit" VALUE="Search!">
    </
    FORM>


    if (
    $choice == request)
    {
    echo 
    "<B>All Emails for Branch ",$search,"</B>";
    $result 
    mysql_query("SELECT * FROM evolinks 
         WHERE Branch LIKE \"%
    $search%\"");
    }; 

  5. #5
    Join Date
    Sep 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Never mind, I think I just got it.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Note-- ($choice == request) isn't valid. request either needs to be $request-- a variable, or "request" as text, a "string".
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Sep 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks, I have updated that. I have also changed from a search box to a dropdown with the choices to save space, but I'm not sure I'm happy with that... but it's working for now...

    Last thing I would like to be able to do is sort the results alphabetically before they are spit out. If my code is the following, can I put a sort in there somewhere, so it sorts the Name?

    Code:
    if ($choice == "all")
    {
    echo "<B>This is a full listing of the email database</B>";
    $result = mysql_query("SELECT * FROM evolinks");
    };
    if ($choice == "request")
    {
    echo "<B>All Selected Emails</B>";
    $result = 
    mysql_query("SELECT * FROM evolinks 
         WHERE Branch LIKE \"%$search%\" AND (Mailgroup LIKE \"%$search2%\" OR Mailgroup LIKE 'B')");
    };
    
    echo "<script type='text/javascript'>"; 
    echo "var students=[];"; 
    
    if ($myrow = @mysql_fetch_array($result)) {
    
    do 
    { 
    echo "students[students.length]=['",$myrow["Name"],"', '",$myrow["email"],"'];"; 
    } while ($myrow = @mysql_fetch_array($result));
    echo "</script>";
    }

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Sep 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thank you! I didn't think it would be so easy

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
  •