0

Bottom Line: How to append array values to the HTML Table cell in PHP ?

Detailed Situation: I have an array like this:

Array
(
    [PROD-150] => Cancelled
    [PROD-80245] => Pending
    [PROD-BOOK-65] => Due
)

And I have a HTML table like this:

<tr>
   <th>Order Code</th>
   <th>Order Date</th>
   <th>Customer Code</th>
   <th>Customer Name</th>
   <th>Products Code</th>
   <th>Products Image</th>
   <th>Products Name</th>
   <th>Product Amount</th>
   <th>Commission Percentage</th>
   <th>Commission Amount</th>
   <th>Order Status</th>
   <th>Payment Status</th>
</tr>

Here's the how values are shown in the above table:

foreach ($affiCodeAndAmount as $key => $value) {                    
    $query = "SELECT * FROM products WHERE ProdCode = '".$key."'";
    $validate->Query($query);
    while ($rows = $validate->FetchAllDatas()) {
        foreach ($prdOrdQty as $i) {
            $productTotalAmount = $i * $rows["ProdRate"];
        }
        echo "
        <tr>
            <td>".$orderCode."</td>                     
            <td>".$orderDate."</td>
            <td>".$customerCode."</td>
            <td>".$customerName."</td>
            <td>".$key."</td>
            <td align='center'>
                <img src='//placehold.it/50x50'>
            </td>
            <td>".$rows["ProdName"]."</td>
            <td>".$productTotalAmount."</td>
            <td>".$rows["ProdAffiCommission"]."</td>
            <td>".$value."</td>
            <td>".$status."</td>";
            foreach($array as $val) { // $array is the above array
               echo "<td>".$val."</td>";
            }
        </tr>";
    }
}

When I run the code, the Payment Status Column is like this:

CancelledPendingDue

I want to display the array values in each cell separately with respect to the number of products. It should be like this:

<th>Payment Status</th>
    <td>Cancelled</td>
    <td>Pending</td>
    <td>Due</td>

How do I achieve that ? Kindly help me out. Thanks.

P.S.:: All the values are coming from the database. So the table data can vary accordingly.. It can be any x number.

EDIT 1:

Here's what I have done:

$table = "<tr>";
$table .= "<td>".$orderCode."</td>";                    
$table .= "<td>".$orderDate."</td>";
$table .= "<td>".$customerCode."</td>";
$table .= "<td>".$customerName."</td>";
$table .= "<td>".$key."</td>";
$table .= "<td align='center'>
    <img src='//placehold.it/50x50>";
$table .= "</td>";
$table .= "<td>".$rows["ProdName"]."</td>";
$table .= "<td>".$productTotalAmount."</td>";
$table .= "<td>".$rows["ProdAffiCommission"]."</td>";
$table .= "<td>".$value."</td>";
$table .= "<td>".$status."</td>";
$table .= "<td>".$tab."</td>";
foreach ($bc as $value) {
    $table .= "<td>".$value."</td>";
}
$table .= "</tr>";
echo $table;

Output for that is like this:

<th>Payment Status</th>
    <td>Cancelled</td>
    <td>Cancelled</td>
    <td>Cancelled</td>

<th></th>
    <td>Pending</td>
    <td>Pending</td>
    <td>Pending</td>

<th></th>
    <td>Due</td>
    <td>Due</td>
    <td>Due</td>
Saiyan Prince
  • 3,930
  • 4
  • 28
  • 71

1 Answers1

0

You have a strange construct in your echo. Use HEREDOC

$row = <<<EOT 
   <tr>
        <td>$orderCode</td>                     
        <td>$orderDate</td>
        <td>$customerCode</td>
        <td>$customerName</td>
        <td>$key</td>
        <td align='center'>
            <img src='//placehold.it/50x50'>
        </td>
        <td>$rows["ProdName"]</td>
        <td>$productTotalAmount</td>
        <td>$rows["ProdAffiCommission"]</td>
        <td>$value</td>
        <td>$status</td>;
   EOT
   foreach($array as $val) { // $array is the above array
      $row.= "<td>".$val."</td>";
   }
   $row.="</tr>";
   echo $row;

or something like this, suggested by What is the best way to add two strings together?

$row="<tr>";
$row.= "<td>".$orderCode."</td>";                     
$row.= "<td>".$orderDate."</td>";
$row.= "<td>".$customerCode."</td>";
$row.= "<td>".$customerName."</td>";
$row.= "<td>".$key."</td>";
$row.= "<td align='center'>";
$row.= "    <img src='//placehold.it/50x50'>";
$row.= "</td>";
$row.= "<td>".$rows["ProdName"]."</td>";
$row.= "<td>".$productTotalAmount."</td>";
$row.= "<td>".$rows["ProdAffiCommission"]."</td>";
$row.= "<td>".$value."</td>";
$row.= "<td>".$status."</td>";
foreach($array as $val) { // $array is the above array
  $row.="<td>".$val."</td>";
 }
$row.= "</tr>";
echo $row;
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236