Skip to content
Advertisement

All database items are appearing in each row instead of in their own

I have a form where users can order multiple parts and an email alert is sent with the data. Everything works great, but when there are multiple parts ordered each item appears multiple times. I would like to have each item appear once, on its own line.

This is the PHP that is generating the table:

$requestpartnumberarraybr = implode("<br>", $requestpartnumber);
$requestpartdescriptionarraybr = implode("<br>", $requestpartdescription);
$requestpartquantityarraybr = implode("<br>", $requestpartquantity);
$requestpartpricearraybr = implode("<br>", $requestpartprice);
$warehousearraybr = implode("<br>", $warehouse);
$binarraybr = implode("<br>", $bin);
$onhandarraybr = implode("<br>", $onhand);
$availablearraybr = implode("<br>", $available);

foreach($requestpartnumber as $key=>$val) {   
    $message_middle.='
    <tr>
        <td>' . $requestpartnumberarraybr. '</td>
        <td>' . $requestpartdescriptionarraybr . '</td>
        <td>' . $requestpartquantityarraybr. '</td>
        <td>' . $requestpartpricearraybr . '</td>
        <td>' . $warehousearraybr . '</td>
        <td>' . $binarraybr . '</td>
        <td>' . $onhandarraybr . '</td>
        <td>' . $availablearraybr . '</td>                               
    </tr>';
}

That code produces this – note that there are multiple items in each row, and then they are repeated:

Table with each item ordered appearing multiple times

It’s supposed to look like this, though, with each item just having one row to itself:

Table with each item having its own row

Advertisement

Answer

The issue you are experiencing is that you are concatenating every value in the list, and then for each element in the list you are displaying that concatenated value.

//Here you are compressing the array of part numbers into one variable with all the values in the list
$requestpartnumberarraybr = implode("<br>", $requestpartnumber);
...
//Here you loop over each item in the array
foreach($requestpartnumber as $key=>$val) {
...
    //Here, in the loop, you write the variable that has all elements in the array
    '<td align="center" style="border:1px solid black!important;"><font color="black">' . $requestpartnumberarraybr. '</font></td>'

So that’s why it is happening.


As for what to do instead, it seems a little unclear how your information is coming in, but you should be able to simply use $val instead of $requestpartnumberarraybr when adding to your table variable, and for the other values you should be able to use $key on their arrays, like so: $requestpartdescription[$key].

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement