1

I have an HTML form that's submitting to a PHP script and send it as an email. The problem I'm having is iterating the array and getting the output formatted into a table correctly.

My input fields look like this:

<input class="form-control" type="text" name="alternate[][qty]" />
<input class="form-control" type="text" name="alternate[][height]" />

I am iterating the array like this:

if ( isset( $_POST['alternate']))
        {
            foreach($_POST['alternate'] as $alt)
            {
                $message .= "<tr><td>" . $alt['qty'] . "</td><td>" . $alt['height'] . "</td></tr>";
            }
        }

I'm getting the correct values from the array but they are not formatted correctly. I am looking for an output something like this:

123 45

but instead it breaks across two rows like this:

enter image description here

How can I get both values on the same line?

EDIT:

Using

    echo '<pre>';
    print_r($_POST['alternate']);
    echo '</pre>';

I get Array ( [0] => Array ( [qty] => 54 )

    [1] => Array
        (
            [height] => 5
        )

    [2] => Array
        (
            [qty] => 34
        )

    [3] => Array
        (
            [height] => 5
        )

    [4] => Array
        (
            [qty] => 36
        )

    [5] => Array
        (
            [height] => 45
        )
    ...
)

which makes it look like I actually have 6 arrays...? That would explain why I'm getting each cell on a separate row, but I still don't understand how to fix it...

3
  • So that's what it looks like, but what does the HTML contain? Commented Mar 18, 2015 at 17:35
  • @Mr.Llama This is the generated HTML <tr><td>123</td><td></td></tr> <tr><td></td><td>45</td></tr> Commented Mar 18, 2015 at 17:51
  • @mack updated my solution, take a look. Commented Mar 18, 2015 at 20:47

3 Answers 3

1

You're iterating through every element in $_POST['alternate'] and creating a row for each iteration. There are two elements, thus two rows.

There's no need to iterate since you already know which elements you'll get:

if ( isset( $_POST['alternate']))
{
  $message = "<tr><td>{$_POST['alternate']['qty']}</td><td>{$_POST['alternate']['height']}</td></tr>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

The 'alternates' array contains multiple items, each item has a qty and a height. The final output should show the qty and height for each array item in a single row.
If your alternates contains multiple items you'll probably need two foreach then. Do a print_r($_POST['alternate']) to get a hang of the structure of your alternates and see if you can solve it on your own. If not, come back and ask another question.
0

Give this a shot, I hope that's what you're opting for.

if(isset($_POST['alternate']))
{
    $message = "<tr>";
    foreach($_POST['alternate'] as $alt)
    {
        if(isset($alt['qty']))
            $message .= "<td>" . $alt['qty'] . "</td>";
        elseif(isset($alt['height']))
            $message .= "<td>" . $alt['height'] . "</td>";
    }
    $message .= "</tr>";
}

This gave me <tr><td>123</td><td>45</td></tr>.


EDITED SOLUTION

This script actually takes care of <tr> tags inside of the loop.

if(isset($_POST['alternate']))
{
    foreach($_POST['alternate'] as $alt)
    {
        if(isset($alt['qty']))
            $message .= "<tr><td>" . $alt['qty'] . "</td>";
        elseif(isset($alt['height']))
            $message .= "<td>" . $alt['height'] . "</td></tr>";
    }
}

Try it out and let me know.

2 Comments

Thanks @jvitasek, that does give me the values on the same line, the only problem is that the 'alternate' array contains multiple values. If I do not include the <tr> inside of the loop, all the values end up on one huge row.
@mack Do you still need the solution? I can edit it to make it so.
0

Your HTML actually declares separate array entries. You need to group them by defining keys for the array. Something like:

<input class="form-control" type="text" name="alternate[0][qty]" />
<input class="form-control" type="text" name="alternate[0][height]" />

Then the next group of fields uses "1" and so on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.