How to show the data properly in the table

hi i have an php code which is read an json field value and displayed in the table format my code is working fine but the data is not arranged properly how to fix this one can anyone help me on this

$post_id = get_the_ID();
$dataproducts = get_field( "data_product", $post_id);
$data = json_decode($dataproducts);
$table = '
<table class="company-sub-table">
<colgroup>
<col>
<col>
<col>
<col>
</colgroup>
<thead>
<tr>
    <th>Name</th>
    <th>Description</th>
    <th>Uses Data Product</th>
    <th>Customer Type</th>
</tr>
</thead>
<tbody>
';
foreach ($data as $dataproducts_var) {
  foreach ($dataproducts_var->name as $name_of_dp)
  {
 
  echo "<script>console.log('Console: " . $name_of_dp . "' );</script>";
    $name_dp = $name_of_dp;
  echo "<script>console.log('Console: " . $name_dp . "' );</script>";
    $table .= '
        <tr>
            <td>' . $name_dp . '</td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    ';
}
    
    foreach ($dataproducts_var->description as $description)
    {
        $description_to_print = $description;
        $table .= '
            <tr>
                <td></td>
                <td>' . $description_to_print . '</td>
                <td></td>
                <td></td>
            </tr>
            ';
    }
    foreach ($dataproducts_var->uses_data_product as $uses_dp)
    {
        $uses_dp_to_display = $uses_dp;
        $table .= '
            <tr>
                <td></td>
                <td></td>
                <td>' . $uses_dp_to_display  . '</td>
                <td></td>
            </tr>
            ';
    }
    foreach ($dataproducts_var->customer_type as $c_type)
    {
        $c_type_to_display = $c_type;
        $table .= '
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td>' . $c_type_to_display . '</td>
            </tr>
            ';
    }
}
$table .= '
    </tbody>
</table>
';
echo $table;

this is the table looks like

this is the json field value---------- [{"name":["JDLink "],"description":["Solution that fits any make/model of equipment, including older John Deere and non-John Deere equipment. Places essential machine information such as location, machine hours, and alerts at the usersu2019 fingertips, leaving them in complete control to increase machine productivity."],"uses_data_product":["GPS Data, Equipment/Maintanence Data"],"customer_type":["Vehicle Fleet Operators"]},{"name":["Granular Insights"],"description":["John Deere's telematics system supports operational improvements by providing information such as field ops data like hours, acres and quantities of inputs applied, and crops harvested. Premium services are sold at 6$ per Acre. "],"uses_data_product":["Farm Yeild Data"],"customer_type":["Farmers"]}]

1 answer

  • answered 2022-04-07 11:15 Diego D

    If I correctly got your problem, you just needed to render those values in the same line. So I grouped all your foreach loops in a single one that is looping through the first array $dataproducts_var->name. The loop will also use a counter that will keep track of the current index in that array that will be used to access to the values belonging to same tuple from the other arrays:

        /**
         * Loops through the items in the array $dataproducts_var->name
         *
         * The foreach statement includes the $loopIndex var, that will be
         * needed to reach the values from the other arrays belonging to same row
         */
        foreach ($dataproducts_var->name as $loopIndex => $name_of_dp)
    
            /**
             *   Collects data, to show in the table row,
             *   from the other arrays in the $dataproducts_var object
             *   using the $loopIndex counter
             * 
             *   The routines expects those arrays to have all the same size and their
             *   values to be at the same position if they belong to the same row here
             */
            $description_to_print =  $dataproducts_var->description[$loopIndex];    
            $uses_dp_to_display = $dataproducts_var->uses_data_product[$loopIndex];
            $c_type_to_display = $dataproducts_var->customer_type[$loopIndex];    
    
            $table .= '
                <tr>
                    <td>' . $name_of_dp . '</td>
                    <td>' . $description_to_print . '</td>
                    <td>' . $uses_dp_to_display . '</td>
                    <td>' . $c_type_to_display . '</td>
                </tr>
                ';
        }    
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum