Skip to main content

Using an array (or xml) to program a function



Ultimate Goal





Is to make something like Magento offers - basically a logic builder, and as shown by this post on Stackoverflow: jQuery (or any web tool) Nested Expression Builder So far I have made jQuery to build a tree and get the data that I want the builder to use, check, and set. Now I just need to parse the checks and add it into various places in a script I'm making - but I am unsure how to process it dynamically so that these checks can be performed, which will lead to some actions occurring/data being changed automatically.





Maybe we can call this dynamic expression processing?





Original Post





Forgive me, I know what I would like to do, but have little idea how to do it - so I'm looking for some inspiration. I have allowed a multidimensional array to be generated, and the array would contain certain 'commands' and logic functions, and when a condition is true it is executed.





In it's most basic form, the array would contain a set of if statements, where if the statement were true, then would would proceed to the next array item and go down a level, if it were false, then you'd proceed to the next array item with no children (an unmarried sibling, i guess we could call it). Once there is nothing left to process, since nothing is true, then nothing would happen.





I'd imagine that maybe the best way to 'feed' the data in would be via XML - though would this be possible, I mean, to keep going deeper, else go down, essentially until there is a true condition?





Basically, the array takes the following form (though I not 100% sure I've written it correctly, but I think it looks right :s):







[0][0] => array('function' => 'if', 'check' => 'day', 'condition' => 'equals', 'value' => '3');

[0][1][0] => array('function' => 'set', 'name' => 'date_day', 'value' => 'wednesday');

[1][0] => array('function' => 'if', 'check' => 'day', 'condition' => 'equals', 'value' => '4');

[1][1][0] => array('function' => 'set', 'name' => 'date_day', 'value' => 'thursday');







So the above would be - if day=3, then set date_day as wednesday; else if day=4, then set date_day as thursday





Which I'd imagine would correspond to (though i have no idea if you can sub item):







<items>

<item>

<function>if</function>

<check>day</check>

<condition>equals</condition>

<value>3</value>

<item>

<function>set</function>

<name>date_day</name>

<value>wednesday</value>

</item>

</item>

<item>

<function>if</function>

<check>day</check>

<condition>equals</condition>

<value>4</value>

<item>

<function>set</function>

<name>date_day</name>

<value>thursday</value>

</item>

</item>

</items>







Which would basically make the following statements in a function of some sort:







function ($current_data){

LOOP

if(FUNCTION == "if"){

if(CHECK CONDITION VALUE){

**go to next item deeper in the chain**

} else {

**go to sibling item**

}

} else if(FUNCTION == "set"){

define(NAME, VALUE);

}

ENDLOOP

}







I know the above can be done using the date() function, but this is a very basic example. Another example could involve check to see if the colour entered was red, and if it were, then set something based on this colour, else do something else if it were blue. Another could be to set the template to be for US visitors if the US flag was clicked on. The point is that it could basically fulfil any action and do a check and give a result - basically like programming - but where the function data is feed in by PHP or XML





I'm sure there must be something out there that can accomplish this, but I just have no idea were to start exactly, so any assistance would be great - and yes I know there could be some security concerns, but I plan on having checks in place checking that the checks , conditions , values , etc are safe (so this needs to be able to be factored in).





Many many thanks!


Comments

  1. Okay JSON vs. XML aside, here's how I would process that array...

    $array = xmldecode($xml);
    $resultFound = false;
    $i = 0;

    while(!$resultFound && $i < count($array)) {
    if (myFunction($array[$i]) {
    $resultFound = true;
    }
    $i++;
    }

    if (!$resultFound) {
    // error condition
    }

    function myFunction($array) {
    $function = $array[0]['function'];

    switch($function) {
    case 'if':
    $checkVariable = $array[0]['check'];
    $condition = $array[0]['condition'];
    $checkValue = $array[0]['value'];
    switch($checkVariable)
    case 'day':
    switch($condition) {
    case 'equals':
    if (GLOBAL_DAY == $checkValue) {
    return myFunction($array[1]);
    } else {
    return false;
    }
    break;
    case 'less than':
    if (GLOBAL_DAY < $checkValue) {
    return myFunction($array[1]);
    } else {
    return false;
    }
    break;
    }
    break;
    }
    break;
    case 'set':
    $setVariable = $array[0]['name'];
    $setValue = $array[0]['value'];
    switch($setVariable) {
    case 'date_day':
    GLOBAL_DATE_DAY = $setValue;
    return true;
    break;
    }
    break;
    }
    }

    ReplyDelete
  2. Your problem is very similar to that of form validation, so I would look at some popular jQuery form validation plugins or MVC frameworks like CakePHP. These typically all have stock building blocks for the most common validation rules that the user can easily put together and pass specific arguments to to cover most scenarios.

    The notable difference between your problem and these examples is that form validation frameworks are aimed at developers, so they can simply write custom functions if they need to glue multiple rules together to form a more complex rule. However, you can still achieve something that works for probably 98% of all use cases by doing something like:

    $offers = array(
    'pet feed sale' => array(
    'category' => array(1, 2, 3)
    'total' => '>100',
    'weekday' => array('mon', 'wed')
    'set' => array(
    'discount' => 80
    'shipping' => 0
    )
    ),
    'medication sale' => array(
    'category' => 4
    'date' => '2012-1-28',
    'set' => array(
    'discount' => 50
    )
    )
    );


    And if the user needs apply more complex pricing structures then they could, for instance, break the "pet feed sale" rule into 3 offers, one for dog food, one for cat food, and one for fish food. There might be more repetition, but it makes it much easier to implement than a full parser.

    Also, most non-programmers probably handle repetition a lot better than complicated logic and control flow.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex