...

View Full Version : how to put a condition in a string and then check it later



ewel
12-21-2007, 09:52 AM
My little project is nearing completion but there is (hopefully only) one more thing I am not getting to work, and I would like to ask for your help to solve it.

The problem that I have is getting a variable consisting of a php condition out of an xml file into the php code to work and work at the right time. That's as good an explanation as I can give in summary.

In more detail, I am making a module for a CMS (Joomla) and that means there is an xml file with parameters and a php file which does the job. The xml file produces a page of options in the CMS backend, and the php file pulls the parameters from the xml file in this way:
$myparam1 = $params->get( 'myparam1' );
Then in the php I use these parameters to check conditions, such as
if ($myparam1 == "filled in") { doSomething(); }
What I want to do is to have a field in the xml where a condition can be filled in, in a text field and using variables already defined in the php, which becomes, say, $myparam2, such as
$option=="com_content"
Then I want to put that condition into my php checking process and check if the condition is fulfilled or not, for example:
$myparam1 = $params->get( 'myparam1' );
$myparam2 = $params->get( 'myparam2' );
if ($myparam1 == "filled in") {
doSomething();
if ($myparam2) {
$setsomething=1;
}
}
What I want to achieve is that myparam2 is checked at that point so that the effect would be like this:
$myparam1 = $params->get( 'myparam1' );
$myparam2 = $params->get( 'myparam2' );
if ($myparam1 == "filled in") {
doSomething();
if ($option=="com_content") {
$setsomething=1;
}
}
Unfortunately, this does not work at all. I think the actual effect is more like this:
$myparam1 = $params->get( 'myparam1' );
$myparam2 = $option=="com_content";
if ($myparam1 == "filled in") {
doSomething();
if (/*nothing*/) {
$setsomething=1;
}
}
I got the impression that at the moment $param2 is declared the condition is executed. So I experimented with declaring it and calling in these ways, but that did not help:
$myparam2 = (string) $params->get( 'myparam2' );
$myparam2 = strval($params->get( 'myparam2' ));
if (eval($myparam2)) {

So now I am out of inspiration and wondering how to get this to work, and therefore I would like to ask the most popular forum question:
Please help!

GJay
12-21-2007, 10:48 AM
I don't understand, could you perhaps use real values rather than (hopefully...) made up 'my' things?
What is the actual value of the 'myparam' variables? Can you lose the calls to the $params object and show some actual strings in the examples? Your 'what I want to achieve' example does nothing with $myparam2, but uses $option...is that a typo? If not, what's the relevance?

psykx
12-21-2007, 12:46 PM
in theory the pseudo code should work, can you post your real code?

also would it work to have two if switches next to each other e.g
if(){}
if(){}

ewel
12-21-2007, 01:57 PM
Thanks for your response - This is one of very few forums where I never failed to get a reply!

I thought giving an example would help but of course the real code is more clarifying - and perhaps shows that I made a basic mistake. So, here is some of the real code (pasting all of it would be too much and largely useless repetition or irrelevant)...

In the xml:

<param name="moap_inexclude" type="list" default="" label="Include or exclude specified pages" description="If set to 'Exclude mode' Mod OnAnyPage will display on all pages except the pages specified. If set to 'Include mode' Mod OnAnyPage will not display on any pages except the pages specified.">
<option value="">Set mode:</option>
<option value="">-----------------------------------------</option>
<option value="exclude">Exclude mode (visible except as specified)</option>
<option value="include">Include mode (hidden except as specified)</option>
<option value="">-----------------------------------------</option>
<option value="module">Module test mode (always visible)</option>
</param>
<param name="moap_front" type="radio" default="" label="Front page" description="Select 'Exclude' in 'Exclude mode' and 'Include' in 'Include mode'.">
<option value="">Not set</option>
<option value="-1">Exclude (hidden)</option>
<option value="1">Include (visible)</option>
</param>
<param name="moap_view" type="radio" default="" label="All content items" description="Select 'Exclude' in 'Exclude mode' and 'Include' in 'Include mode'.">
<option value="">Not set</option>
<option value="-1">Exclude (hidden)</option>
<option value="1">Include (visible)</option>
</param>
<param name="moap_blogcat" type="radio" default="" label="All category blogs" description="Select 'Exclude' in 'Exclude mode' and 'Include' in 'Include mode'.">
<option value="">Not set</option>
<option value="-1">Exclude (hidden)</option>
<option value="1">Include (visible)</option>
</param>
<param name="moap_cat" type="radio" default="" label="All category tables" description="Select 'Exclude' in 'Exclude mode' and 'Include' in 'Include mode'.">
<option value="">Not set</option>
<option value="-1">Exclude (hidden)</option>
<option value="1">Include (visible)</option>
</param>
<param name="moap_udefconds" type="text" default="" label="User defined condition" description="Specify a user defined condition."/>
<param name="moap_udefcond" type="radio" default="" label=" " description="Select 'Exclude' in 'Exclude mode' and 'Include' in 'Include mode'.">
<option value="">Not set</option>
<option value="-1">Exclude (hidden)</option>
<option value="1">Include (visible)</option>
</param>

In the php:

$option = trim( mosGetParam( $_REQUEST, 'option', null ) );
$task = trim( mosGetParam( $_REQUEST, 'task', null ) );
$view = trim( mosGetParam( $_REQUEST, 'view', null ) );
$id = intval( mosGetParam( $_REQUEST, 'id', null ) );
$moap_inexclude = $params->get( 'moap_inexclude' );
$moap_front = $params->get( 'moap_front' );
$moap_view = $params->get( 'moap_view' );
$moap_blogcat = $params->get( 'moap_blogcat' );
$moap_cat = $params->get( 'moap_cat' );
$moap_udefcond = $params->get( 'moap_udefcond' );
$moap_udefconds = strval($params->get( 'moap_udefconds' ));
/*more params/variables here*/
if ($moap_inexclude == "module") {
$callModornot = 1;
} else {
if ($moap_inexclude == "exclude") {
$callModornot = 1;
if (
($moap_front == -1 && ($option=="com_frontpage")) ||
($moap_view == -1 && ($option=="com_content" && $task=="view")) ||
($moap_blogcat == -1 && ($option=="com_content" && ($task=="blogcategory" || $view=="blogcategory"))) ||
($moap_cat == -1 && ($option=="com_content" && ($task=="category" || $view=="category"))) ||
/*more conditions here*/
($moap_udefcond == -1 && eval($moap_udefconds))
) {
$callModornot = 0;
}
}
/*include mode comes here as a copy of exclude mode but $var==-1 becomes $var==1*/
}

Then the code goes on to create output dependent on whether $callModornot is 0 or 1. As you can see I experimented with strval and eval but to no avail.

GJay
12-21-2007, 02:32 PM
I still don't understand how you're getting from the XML to the values at the top...what is a value that $moap_udefconds is likely to have? The easiest way of expressing this without any ambiguity is to paste the output of var_dump($moap_udefconds); with an actual value.

ewel
12-21-2007, 03:42 PM
mosGetParam is the Joomla way to find the values of certain standard variables which also tend to appear in urls. $params->get is the way user-set parameters are drawn from the Joomla back-end control panel for the module, in which the parameters are built up by the xml file.

Basically all parameters except the first and udefconds have a value of -1 or 1, and the first one is 'include', 'exclude' or 'module'. These are set by radio buttons and a dropdown, but moap_udefconds is a text field where site administrators who use this module can fill in a condition at will. Some examples of what people might fill in are:
$option=="com_content"
$task=="mail"
$view!="print"
$id>7

So a module user could for example choose exclude mode (i.e. moap_front is exclude), with Front page on exclude (i.e. moap_front is -1) and a user-defined condition as above set to exclude (i.e. moap_udefcond is -1 and moap_udefconds is $task=="mail").

In this example, after $params->get has done its job, the variables with their values should be as follows (and they are, except for moap_udefconds):
variable $moap_inexclude has a value of "exclude"
variable $moap_front has a value of -1
variable $moap_udefcond has a value of -1
variable $moap_udefconds should have a value of "$task=="mail"

Then, in my list of conditions the condition $moap_inexclude == "module" is not true but the conditions $moap_inexclude == "exclude" and $moap_front == -1 are true. If a site visitor is on the front page then $option=="com_frontpage" will also be true. As to moap_udefconds, the idea is that the condition filled in by the administrator will be checked in the line ($moap_udefcond == -1 && eval($moap_udefconds)). If in this example a site visitor is on a page where $task=="mail" then the user-defined condition is true (again, the value of task is set by the MCS system and drawn from there by mosGetParam).

For that to happen, where the code says ($moap_udefcond == -1 && eval($moap_udefconds)) this should have the same effect as if I had written ($moap_udefcond == -1 && $task=="mail"). But that does not seem to be the case. Instead, it seems like $moap_udefconds = strval($params->get( 'moap_udefconds' )); has the same effect as if I had written $moap_udefconds = strval($task=="mail");.

That's as good as I can explain it. But what did you mean by pasting the output of var_dump($moap_udefconds); with an actual value?

dumpfi
12-21-2007, 07:52 PM
Try something like this:
$myparam1 = $params->get( 'myparam1' );
$myparam2 = $params->get( 'myparam2' );
if ($myparam1 == "filled in") {
doSomething();
if (!ctype_space($myparam2) && eval('return ('.$myparam2.');')) {
$setsomething=1;
}
}dumpfi

ewel
12-22-2007, 12:33 AM
Thanks dumpfi !! I did not get that to work but was very close and set my nose in the right direction for finding a solution after an evening of Google research.. I found the solution at http://www.php.net/eval in a post of Nova912 who did something very similar to what you suggested.

So, what works is this: eval("return ".$moap_udefconds.";").

Aside from tidying up and testing that concludes my little project :D - Thanks everyone for helping out!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum