View Full Version : if limitation?
oriecat
09-16-2006, 01:19 AM
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?
blm126
09-16-2006, 02:50 AM
I don't think there is a limit. What is the exact situation so we can suggest an alternative?
djr33
09-16-2006, 03:39 AM
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.
oriecat
09-17-2006, 04:04 AM
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:
<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%\"");
};
oriecat
09-17-2006, 04:19 AM
Never mind, I think I just got it. :):)
djr33
09-18-2006, 05:50 AM
Note-- ($choice == request) isn't valid. request either needs to be $request-- a variable, or "request" as text, a "string".
oriecat
09-18-2006, 03:09 PM
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?
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>";
}
djr33
09-19-2006, 12:11 AM
http://www.dynamicdrive.com/forums/showthread.php?t=13166
oriecat
09-19-2006, 12:58 AM
Thank you! I didn't think it would be so easy :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.