SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Jul 2000
    Location
    North Central AR
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a number of functions, that I want to put into an array and loop through each until all have compelted their tasks. Question is, the user would have a choice of one, some or all. So how do I fill the array from their choices, whatever they pick, 1, 5 or all of the choices? The choices will come from form input. Any suggestions or where I could look to work it out?

    Thanks

    ronnie
    The largest selection of PLR Articles on the Planet!
    Many Private Label Rights Products.

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just simply insert it into the array of jobs like:
    $array[] = $somejob;

    The array will be expanded and later on, you just use the capacity of the array (count()) to loop through each.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    SitePoint Addict
    Join Date
    Jul 2000
    Location
    North Central AR
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply Son.

    I guess what I'm having a hard time with, is getting it from the form to the script and into an array, thats were I am lost, guess I should have explained better... I would probably have checkboxs, and the values could be function_a, another checkbox, value=function_b, and so on. So what ever was checked, would be passed onto the script. But getting, what was choosen, into an array is what has me puzzled. Maybe that is what your explaining, and I'm still lost..
    Thanks,

    ronnie
    The largest selection of PLR Articles on the Planet!
    Many Private Label Rights Products.

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you code your html form right - the values from the check boxes will be posted by the form as an array. I've listed this code from http://www.devshed.com/Server_Side/P...ics/page6.html as an example:
    Code:
    <FORM>
    <INPUT TYPE="checkbox" name="petArray[]" value="dog"> Dog<br>
    <INPUT TYPE="checkbox" name="petArray[]" value="cat"> Cat<br>
    <INPUT TYPE="checkbox" name="petArray[]" value="fish"> Fish<br>
    </FORM>
    I added quotation marks where they should be and used more conventional naming (hehe - why did I bother copying this).

    Now you might want write some code like the example below to execute the functions:
    Code:
    foreach ($petArray as $pet) {
       switch ($pet) {
          case "dog" : 
             woof();
             break;
          case "cat" : 
             meow();
             break;
          case "fish" : 
             swish();
             break;
       }
    }
    BTW - what is up with the \[php] tags - it outputs \<br> at my line breaks!?!
    Last edited by freakysid; Mar 28, 2001 at 00:24.

  5. #5
    SitePoint Addict
    Join Date
    Jul 2000
    Location
    North Central AR
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply Freaky.

    I understand what your saying, and I don't see why it would'nt work. But is'nt there extra code there? I mean I would have every function listed, and if found "run" the function. Why could'nt I just, like your example, put the functions choosen into the array, count the array, and loop though it till all functions "ran"?

    This may sound stupid, but I don't know how to say "run" the functions.

    So far, the form passes the input to the script, and into my array. Then count the objects in the array, then loop through till all functions have run. But how do I say "run" each function?

    $count = count($my_array);

    for ($i=0; $i<$count; $i++)
    {
    how would I say do each function in the
    array till they there is none?
    }

    This is probably real beginner stuff, but I am lost. How would I do that?
    Thanks

    ronnie
    The largest selection of PLR Articles on the Planet!
    Many Private Label Rights Products.

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I know that switch statements are big ugly mothers that mess up the poetic flow of your code But I don't know any more elegant solution. Others might.

    You could put the switch statement into a function which receives a string as an arguement then switch on the string. Then, inside your for loop, call this function passing the value of $pet (from my example) for each element of the array. Same logic but it might make your code a bit cleaner.

    Anyway, just code something that works and be done with it! There is no Nobel prize for programming


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
  •