Help please! Dynamic condition...
Moderator: General Moderators
Help please! Dynamic condition...
Hy people, how can I (if is possible!I don't know) create a dynamic condition for a "if()" instruction?
I mean I have parameters got by a query through a database, and I have to do a if on them. This parameters are variable, so my question is if is possible to get the value of a variable as condition.
I mean:
I create dynamically a var ex: $a= "cond1||cond2..." like a string is then possible to get the string as condition and obtain something like
if(cond1||cond2...)?
How can I pass the content of a variable as condition?
Thanks for any answers!
And sorry for my poor english!
I mean I have parameters got by a query through a database, and I have to do a if on them. This parameters are variable, so my question is if is possible to get the value of a variable as condition.
I mean:
I create dynamically a var ex: $a= "cond1||cond2..." like a string is then possible to get the string as condition and obtain something like
if(cond1||cond2...)?
How can I pass the content of a variable as condition?
Thanks for any answers!
And sorry for my poor english!
dont think so cos if you were to do that the resulting if statement would have to be:
I don't know how it would react. I am pretty sure all it would do is test to see if the value of $a is set... which of course it would be so it would return TRUE.
I may be wrong tho
Code: Select all
$a = "1 > 28";
if($a){
//code here
}I may be wrong tho
i'm thinking m@ndio is right. have you thought about using a switch?
switches were made to replace[php<?php
if($a==1){
#something here
}elseif($a==2){
#something here
}elseif($a==3){
#something here
}elseif($a==4){
#something here
}elseif($a==5){
#something here
}elseif($a==6){
#something here
}elseif($a==7){
#something here
}elseif($a==8){
#something here
}....
}else{
#something here
}?>with
note: in a switch, it will go from where it finds a match until it hits "break;"
so if you miss one, one case will have two pring outs.
switches were made to replace[php<?php
if($a==1){
#something here
}elseif($a==2){
#something here
}elseif($a==3){
#something here
}elseif($a==4){
#something here
}elseif($a==5){
#something here
}elseif($a==6){
#something here
}elseif($a==7){
#something here
}elseif($a==8){
#something here
}....
}else{
#something here
}?>with
Code: Select all
<?php
switch($a){
case '1':
#something here
break;
case '2'
#something here
break;
....
}?>phpso if you miss one, one case will have two pring outs.
This sounds like an application design problem, ie it might be you need to rethink the design of your php scripts - and possibly your database.
If you need to to test a value returned from a database, normally you'd just assign that to a variable but it sounds like you could be storing several bits of data in one db field? That is usually a bad idea - I recommend some further reading on database normalisation (sorry I don't have any links).
Normalisation starts of by having just one value per field, and then goes on to replace repeated values with keys and new tables.
If you need to to test a value returned from a database, normally you'd just assign that to a variable but it sounds like you could be storing several bits of data in one db field? That is usually a bad idea - I recommend some further reading on database normalisation (sorry I don't have any links).
Normalisation starts of by having just one value per field, and then goes on to replace repeated values with keys and new tables.
I'd have to agree with McGruff on this one. However, you could have supplied a little more info on what you were trying to do.
I'm taking the assumption that you have a heap of different conditions and want to execute one peice of code if any of them are true. If this is the case then the following is an exmple of what I'm assuming you have and how you can make it work.
Check the page on eval on php.net for more info...
I'm taking the assumption that you have a heap of different conditions and want to execute one peice of code if any of them are true. If this is the case then the following is an exmple of what I'm assuming you have and how you can make it work.
Code: Select all
<?php
$a = $myrow["param1"] . " == " . $another_array["param2"];
if (eval($a)) {
echo "WooHoo!";
} else {
echo "FALSE";
}
?>explaining me better
First of all thank you a lot for all your answers!
Then I wanna try to explain you better the situation:
I have to print in red colour days of a month of a calendar (it goes month by month).
What is returned by the query from the database are the day of arrival and the day of depart (the range of the day that should become red).
I store this day in two array (the array of arrival and the array of departure).
So I need to compare a variable I use to go through the days of month with this values and print in red or in green otherwise.
The problem is that I can use only if structure couse I print day by day (first line then I change column), so or I use the switch structure suggested by m3rajk, or if it was possible create a dynamic condition as told in the first post.
Create a "string" with the condition is simple but I don't know if is possible to use its content as condition as I told.
If any idea please tell me.
Thanks again!
Bye for the moment
Then I wanna try to explain you better the situation:
I have to print in red colour days of a month of a calendar (it goes month by month).
What is returned by the query from the database are the day of arrival and the day of depart (the range of the day that should become red).
I store this day in two array (the array of arrival and the array of departure).
So I need to compare a variable I use to go through the days of month with this values and print in red or in green otherwise.
The problem is that I can use only if structure couse I print day by day (first line then I change column), so or I use the switch structure suggested by m3rajk, or if it was possible create a dynamic condition as told in the first post.
Create a "string" with the condition is simple but I don't know if is possible to use its content as condition as I told.
If any idea please tell me.
Thanks again!
Bye for the moment
The eval function lets you 'compile' code on the fly:
http://ca2.php.net/manual/en/function.eval.php
http://ca2.php.net/manual/en/function.eval.php
is this correct?
with eval is correct this:
ex:
$a=100;
$b=290;
$cond=$a>$b."||".$b>$a;
if(eval($cond)) bla bla....
is correct?
ex:
$a=100;
$b=290;
$cond=$a>$b."||".$b>$a;
if(eval($cond)) bla bla....
is correct?
Re: is this correct?
Almost. This should produce an error though. There's 2 things that I'm thinking could be wanting in $cond.riccagt wrote:with eval is correct this:
ex:
$a=100;
$b=290;
$cond=$a>$b."||".$b>$a;
if(eval($cond)) bla bla....
is correct?
$cond = ($a>$b) . "||" . ($b>$a);
The first is that you're wanting to evaluate the comparisons (by putting them in the brackets) into the string. This should (using your figures above) give "FALSE||TRUE" in which case eval($cond) will return TRUE.
$cond = "$a > $b || $b > $a";
This second one puts exactly what you see above into your string. eval() would return the same result on this.
hmmm.. i can think of a variatoin of the last post. getting the day today is, and then only highlighting that day in red....
$today = localtime();
now, assuming you have the day then...
$a=$passed_day_on_calendar;
$b=$today[3];
$cond="$a>$b||$b>$a";
that returns true when passed is not today.
other useful parts of today[] are 4, which is the month, and 5, which is the year
$today = localtime();
now, assuming you have the day then...
$a=$passed_day_on_calendar;
$b=$today[3];
$cond="$a>$b||$b>$a";
that returns true when passed is not today.
other useful parts of today[] are 4, which is the month, and 5, which is the year