Professional Web Applications Themes

Execute command from array - PHP Development

I have an array, say something like this (to build a form): $commands = array( "Start Date" => "build_date('startdate')", "End Date" => "build_date('enddate')" ); Only it will have many more entries. I want to cycle through the array, and print the first part, but then execute the second part so that it goes to the function build_date (which returns html code to output). Right now I am doing it something like this: echo "<tr><td>Start date</td><td>"; echo build_date('startdate'); echo "</td><td>End date</td><td>"; echo build_date('enddate'); echo "</td></tr>"; Because this is going to have many entries in it, it is getting very repetitive to ...

  1. #1

    Default Execute command from array

    I have an array, say something like this (to build a form):

    $commands = array(
    "Start Date" => "build_date('startdate')",
    "End Date" => "build_date('enddate')"
    );

    Only it will have many more entries. I want to cycle through the array,
    and print the first part, but then execute the second part so that it
    goes to the function build_date (which returns html code to output).

    Right now I am doing it something like this:

    echo "<tr><td>Start date</td><td>";
    echo build_date('startdate');
    echo "</td><td>End date</td><td>";
    echo build_date('enddate');
    echo "</td></tr>";

    Because this is going to have many entries in it, it is getting very
    repetitive to do it this way! I am stuck on how to execute that function
    (and pass the string) with it already being a string itself.

    Any ideas?
    Dave Guest

  2. #2

    Default Re: Execute command from array

    Dave Thomas wrote: 

    When you are able and willing to modify the array, the following might be a
    solution:

    $commands = array(
    "Start Date" => array('build_date', array('startdate')),
    "End Date" => array('build_date', array('enddate'))
    );

    foreach ($commands as $label => $func_args) {
    print "<td>$label</td><td>";
    print call_user_func_array($func_args[0], $func_args[1]);
    print "</td>";
    }

    I have used call_user_func_array instead of call_user_func in case you want
    to call functions with more then one argument.


    JW



    Janwillem Guest

  3. #3

    Default Re: Execute command from array

    JW has the correct solution. Another alternative is to use eval(),
    but, as many have said before: "If eval is the answer, you're asking
    the wrong question."

    Just for kicks, here's how you could do it with eval() :

    $commands = array(
    "Start Date" => "build_date('startdate')",
    "End Date" => "build_date('enddate')"
    );

    foreach($commands as $key => $val) {
    echo "<td>$key</td>";
    echo "<td>" . eval("return($val);") . "</td>";
    }

    ZeldorBlat Guest

Similar Threads

  1. Submit command to execute
    By stuberry in forum Macromedia Director Basics
    Replies: 0
    Last Post: April 15th, 11:06 AM
  2. RUN/execute a Command-Line command from an ASP page
    By Lucas Cowald in forum ASP.NET Building Controls
    Replies: 1
    Last Post: October 22nd, 04:26 PM
  3. RUN/execute a Command-Line command from an ASP page.
    By Lucas Cowald in forum ASP Components
    Replies: 7
    Last Post: October 22nd, 01:08 PM
  4. how to execute command line in ASP
    By david Wilton in forum ASP
    Replies: 2
    Last Post: August 11th, 08:16 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139