Grow your CSS skills. Land your dream job.

use function variables like wordpress

  • # July 21, 2010 at 11:13 pm

    hey guys, what i mean about the variables is something like:

    Code:
    test_function(‘title=home&foo=bar&this=that’);

    how can I make something like this work on my website?

    Rob
    # July 22, 2010 at 2:04 pm

    you need to build a function then pass the data into it :)

    for example:

    Code:
    function greet($name=NULL) {

    return "Hello $name";

    }

    Then you call the function and pass in the name…

    Code:
    echo greet("Makeshift");

    //Outputs: "Hello Makeshift"

    # July 22, 2010 at 5:34 pm

    I know that, but how do i get the function to get each variable passed using ampersands and equal signs like that?

    I made this little script for it:

    Code:
    function _li($args){
    $args = explode(‘&’, $args);

    $args1 = $args[0];
    $args1 = explode(‘=’, $args1);
    $args1 = array($args1[0] => $args1[1]);
    foreach($args1 as $key => $val){
    if($key == ‘text’){$text = $val;}
    elseif($key == ‘title’){$title = $val;}
    elseif($key == ‘href’){$href = $val;}
    }

    $args2 = $args[1];
    $args2 = explode(‘=’, $args2);
    $args2 = array($args2[0] => $args2[1]);
    foreach($args2 as $key => $val){
    if($key == ‘text’){$text = $val;}
    elseif($key == ‘title’){$title = $val;}
    elseif($key == ‘href’){$href = $val;}
    }

    $args3 = $args[2];
    $args3 = explode(‘=’, $args3);
    $args3 = array($args3[0] => $args3[1]);
    foreach($args3 as $key => $val){
    if($key == ‘text’){$text = $val;}
    elseif($key == ‘title’){$title = $val;}
    elseif($key == ‘href’){$href = $val;}
    }
    }

    (there’s more to it, but this splits the function variable)

    and the usage..

    Code:
    _li(‘text=Home&title=Go Back Home’);

    but this will only take the first 3 variables, of course I can make it as many as I want, but every time i make a new function for something like this, I don’t wanna have to set it for how many variables it’s looking for…

    Rob
    # July 22, 2010 at 7:54 pm

    Well – is there any reason why you need to pass them like that firstly? Could you not just create an array and throw that in?

    Because I am sure you know this already, that format is how your browser will POST and GET… I am not sure how WP are doing it in their code but I suppose you could explode the data into an array and use it that way…

    $var = explode(‘&’, $string);

    That would give you an array like

    [0]=>title=home
    [1]=>foo=bar
    [2]=>this=that

    Then you are atleast half way there – maybe then you could split them again…

    EDIT:

    Was just playing around with a double explode idea and thought I would check the PHP.net info on explode – and I found this awesome function :D

    Code:
    function doubleExplode ($del1, $del2, $array){

    $array1 = explode("$del1", $array);

    foreach($array1 as $key=>$value){
    $array2 = explode("$del2", $value);
    foreach($array2 as $key2=>$value2){
    $array3[] = $value2;
    }
    }

    $afinal = array();

    for ( $i = 0; $i <= count($array3); $i += 2) {
    if($array3[$i]!=""){
    $afinal[trim($array3[$i])] = trim($array3[$i+1]);
    }
    }
    return $afinal;
    }

    So you basically use it like this…

    $string = "title=home&foo=bar&this=that";
    $myVar = doubleExplode(‘&’, ‘=’, $string);

    and you get back a nice array:

    Array
    (
    [title] => home
    [foo] => bar
    [this] => that
    )

    Is that more the kind of thing you were looking for? :)

    credits to this awesome post:
    http://www.php.net/manual/en/function.explode.php#98741

    # July 23, 2010 at 6:51 am

    oh, wow. works like a charm :) thank you very much.

    # July 23, 2010 at 12:21 pm

    "Double Explode" .. We work in an amazing industry.

    Rob
    # July 24, 2010 at 5:34 am

    lol nice :D

    At least they didn’t call the function "boomBoom" lol

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".