Grow your CSS skills. Land your dream job.

Multi Array Form

  • # December 7, 2009 at 11:55 am

    Hello.

    I’m trying to create a simple form where you can send 3 or more names along with a description for each.

    For an example:

    Code:

    1. Name:
    1. Description:

    2. Name:
    2. Description:

    3. Name:
    3. Description:

    Now when it submits the 3 Names & Descriptions to send.php I want to output them like so:

    1. Name: Bob Smith
    1. Description: Here is a small description about them

    2. Name: Jane Doe
    2. Description: She is pretty and a very nice person

    3. Name: Carrie Smith
    3. Description: She is related to Mr. Smith

    and so on…

    My problem is setting up an proper php script to display each Name and Description based on how many are submitted rather it be 3 or 23…

    Thank you.

    # December 7, 2009 at 12:05 pm

    I fiddled with this a year ago, and I think it was possible if you use these names:

    Code:
    1. Name:
    1. Description:

    That may be totally wrong, but it’s a start.

    If that won’t work I would try name="name[1]" (and auto incrament it for each one).

    For either of these, on the "send.php" page, you should be able to do this to print out all the names with something like this:

    Quote:
    var_dump($_POST);

    Let me know if that helps…

    # December 12, 2009 at 6:33 pm

    Hey,

    I normally do it like these :

    First, simply do as AshtonSanders said.

    Code:

    1. Name:
    1. Description:

    2. Name:
    2. Description:

    3. Name:
    3. Description:

    This will make a name array and a description array when the data is sent over POST.

    This should be the POST you would get :

    Code:
    Array
    (
    [name] => Array
    (
    [0] => Name 1
    [1] => Name 2
    [2] => Name 3
    )

    [description] => Array
    (
    [0] => Description 1
    [1] => Description 2
    [2] => Description 3
    )
    )

    Then, on the PHP side, you can do something like this :

    Code:
    < ?php
    $names = $_POST['name'];
    $descs = $_POST['description'];
    foreach($names as $key => $value)
    {
    echo “Name “.($key+1).” : “.$names[$key].”n”;
    echo “Description “.($key+1).” : “.$descs[$key].”n”;
    echo “—-n”;
    }
    ?>

    or this : (essentially the same, but this one is cleaner)

    Code:
    < ?php
    $names = $_POST['name'];
    $descs = $_POST['description'];
    $total_elements = sizeof($names);
    for($i=0;$i<$total_elements;$i++)
    {
    echo "Name ".($i+1)." : ".$names[$i]."n";
    echo "Description ".($i+1)." : ".$descs[$i]."n";
    echo "----n";
    }
    ?>

    So, you have two equally created arrays and each individual Key on the Names array corresponds to the Key on the Descriptions array.

    So anyways, i hope this helps!

    Cheers =)

    # December 12, 2009 at 8:32 pm

    Thank you HarZens!

    This was very helpful. Exactly what I needed.

    # December 15, 2009 at 6:03 pm

    I also would do what HarZens suggests. Usually to display the data in an array of fluctuating size, a foreach loop is easiest. So basically in the most general terms

    Code:
    foreach ($theContent as $content)
    {
    echo ‘
    ‘ . ‘Some stuff’ . ‘


    }

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

You must be logged in to reply to this topic.

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