Form Processing: empty TD tags generated with foreach loop

233 views Asked by At

I'm trying to process a pretty simple rental quote form, basically the form gets filled out and then the data gets sent to an email.

My issue is how the item section is being formatted. Right now I'm trying a table and populating it with a foreach loop. The main problem is it's generating empty td tags for some reason.

Here's the output, I've added some borders so you can see the empty cells being generated:

enter image description here

And here's the section of my php code where I'm processing this (if you'd like to see the entire code, just let me know):

<?php
 if(isset($_POST['submit']))
 {
$name = strip_tags($_POST['renter_name']);
$email = strip_tags($_POST['renter_email']);
$message = strip_tags($_POST['renter_message']);
$subject = "Quote Request";
$rentalItems = ($_POST['item']);

// $items  = implode(',', $_POST['item']);

$msg  = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<p><strong>Sent by:</strong> ".$name."</p>";
$msg .= "<p><strong>Items:</strong></p>";

$msg .="<table name='rental_items' style='border-collapse:collapse; border: 1px solid black;';> 
                <thead style='text-align:left;'>
                    <tr>
                        <th sytle='padding-right: 5px;'>Item Name and ID</th>
                        <th>Quantity</th>
                    </tr>    
                </thead>
                <tbody>";
                    foreach($rentalItems as $item) { 
                        $msg .="<tr>
                            <td style='border: 1px solid black;'>". $item['name'] ."</td>
                            <td style='border: 1px solid black;'>".$item['quantity']."</td>
                        </tr>";
                     } 
       $msg .= "</tbody>
</table>";   

$msg .= "<p><strong>Message:</strong> ".$message."</p>";
$msg .= "</body></html>";

Currently I'm running a row and two cells in the loop. But for some reason it's treating each td as its own row.

Ultimately I would like that Quantity number along side the Name and ID info.

I'll be honest with you, I haven't worked with tables in years so it could possibly be something super simple I'm missing or have wrong. Or it could be how I'm structuring this whole thing in general.

Any advice or help would be greatly appreciated. Again, if you need any more info, just let me know.

Here's the item section of the form:

<input type="checkbox" name="item[][name]" value="Name: <?php the_title();?> <?php echo "<br>"?> ID: <?php echo get_post_meta($post->ID, 'id_number', true); ?>" id="<?php the_title(); ?>"/>
<label for="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" class="form-image" /></label>
<input type="text" name="item[][quantity]"/>
1

There are 1 answers

3
Nigel Ren On BEST ANSWER

As you are using item[][name] and item[][quantity] for the data, you will end up with alternate array elements containing the different values...

Array(0 => ("name" =>"somename"), 
         1 => ("quantity" =>1))

When you foreach over this array, it will display one value or the other, but not the 2 together.

So it would be easier (I think) to use a for loop and take it in steps of 2...

for($i = 0; $i < count($rentalItems); $i+=2) { 
    $msg .="<tr>
               <td style='border: 1px solid black;'>". $rentalItems[$i]['name'] ."</td>
               <td style='border: 1px solid black;'>".$rentalItems[$i+1]['quantity']."</td>
            </tr>";
} 

Alternatively - rename the arrays to be item[name][] and item[quantity][] so that the data is separate, but you can then use $item['name'][0] and $item['quantity'][0].