Help please! Dynamic condition...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
riccagt
Forum Newbie
Posts: 3
Joined: Sun Jul 06, 2003 10:15 am

Help please! Dynamic condition...

Post by riccagt »

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!
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

dont think so cos if you were to do that the resulting if statement would have to be:

Code: Select all

$a = "1 > 28";

if($a){
//code here
}
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
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

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

Code: Select all

<?php
switch($a){
case '1':
#something here
break;
case '2'
#something here
break;
....
}?>php
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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

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.
User avatar
Unifex
Forum Newbie
Posts: 14
Joined: Mon Jul 07, 2003 12:29 am

Post by Unifex »

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.

Code: Select all

<?php
$a = $myrow["param1"] . " == " . $another_array["param2"];

if (eval($a)) {
  echo "WooHoo!";
} else {
  echo "FALSE";
}
?>
Check the page on eval on php.net for more info...
riccagt
Forum Newbie
Posts: 3
Joined: Sun Jul 06, 2003 10:15 am

explaining me better

Post by riccagt »

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
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

The eval function lets you 'compile' code on the fly:

http://ca2.php.net/manual/en/function.eval.php
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Can you post a code snippet, ie the bit where you test what bgcolor to display?
riccagt
Forum Newbie
Posts: 3
Joined: Sun Jul 06, 2003 10:15 am

is this correct?

Post by riccagt »

with eval is correct this:

ex:

$a=100;
$b=290;

$cond=$a>$b."||".$b>$a;

if(eval($cond)) bla bla....

is correct?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Have you looked at bit shifting and all that fun?
User avatar
Unifex
Forum Newbie
Posts: 14
Joined: Mon Jul 07, 2003 12:29 am

Re: is this correct?

Post by Unifex »

riccagt wrote:with eval is correct this:

ex:

$a=100;
$b=290;

$cond=$a>$b."||".$b>$a;

if(eval($cond)) bla bla....

is correct?
Almost. This should produce an error though. There's 2 things that I'm thinking could be wanting in $cond.

$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.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

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
Post Reply