Reputation: 93
I created a function accepting an array of data and I want to return this HMTL by creating a table with already displayed date But the foreach does not display the HTML and not of any error.
My question is: how can I make this function return the table displaying the array data ??
My Function:
<?php
function GetTableResult($arrayData) {
return "<table id='Grid' class='table table-striped table-bordered table-hover dataTable no-footer DTTT_selectable' role='grid' aria-describedby='dynamic-table_info'>
<thead>
<tr>
<th class='hidden-480' tabindex='0' aria-controls='dynamic-table' rowspan='1' colspan='1' aria-label='teste'>Date</th>
<th class='hidden-480' tabindex='0' aria-controls='dynamic-table' rowspan='1' colspan='1' aria-label='teste'>Name</th>
<th class='hidden-480' tabindex='0' aria-controls='dynamic-table' rowspan='1' colspan='1' aria-label='teste'>Procedure</th>
<th class='hidden-480' tabindex='0' aria-controls='dynamic-table' rowspan='1' colspan='1' aria-label='teste'>Provider Name</th>
<th class='hidden-480' tabindex='0' aria-controls='dynamic-table' rowspan='1' colspan='1' aria-label='teste'>Bravery</th>
<th class='hidden-480' tabindex='0' aria-controls='dynamic-table' rowspan='1' colspan='1' aria-label='teste'>Coparticipation</th>
<th class='hidden-480' tabindex='0' aria-controls='dynamic-table' rowspan='1' colspan='1' aria-label='teste'>Type</th>
<th class='hidden-480' tabindex='0' aria-controls='dynamic-table' rowspan='1' colspan='1' aria-label='teste'>Date Reference`enter code here`</th>
</tr>
</thead>
<tbody>";
foreach($arrayData as $data){
"<tr id='Grid_Linha_1' class='odd' role='row'>
<td class = 'hidden-480'>" . $data['date'] . "</td>
<td class = 'hidden-480'>" . $data['name'] . "</td>
<td class = 'hidden-480'>" . $data['procedure'] . "</td>
<td class = 'hidden-480'>" . $data['provider_name'] . "</td>
<td class = 'hidden-480'>" . $data['bravery'] . "</td>
<td class = 'hidden-480'>" . $data['coparticipation' . "</td>
<td class = 'hidden-480'>" . $data['type'] . "</td>
<td class = 'hidden-480'>" . $data['date_reference'] . "</td>
</tr>";
}
"</tbody>
</table>";
}
Upvotes: 0
Views: 60
Reputation:
Here, is the solution like I said in my comment. You're missing a bracket and try to echo it out instead of returning.
<?php
$arrayData = array(
array(
"date" => "1/1/10",
"name" => "peterparker",
),
array(
"name" => "Clark Kent",
"date" => "2/10/27",
),
);
function GetTableResult($arrayData) {
echo "<table id='Grid' class='table table-striped table-bordered table-hover dataTable no-footer DTTT_selectable' role='grid' aria-describedby='dynamic-table_info'>
<thead>
<tr>
<th class='hidden-480' tabindex='0' aria-controls='dynamic-table' rowspan='1' colspan='1' aria-label='teste'>Date</th>
<th class='hidden-480' tabindex='0' aria-controls='dynamic-table' rowspan='1' colspan='1' aria-label='teste'>Name</th>
</tr>
</thead>
<tbody>";
foreach($arrayData as $data){
echo "<tr id='Grid_Linha_1' class='odd' role='row'>
<td class = 'hidden-480'>" . $data['date'] . "</td>
<td class = 'hidden-480'>" . $data['name'] . "</td>
</tr>";
}
echo "</tbody>
</table>";
}
GetTableResult($arrayData);
Output:
Date Name
1/1/10 peterparker
2/10/27 Clark Kent
Upvotes: 0
Reputation: 3551
This is a simplified example, but you should get the gist:
<?php
function GetTableResult($arrayData) {
$table = '<table>';
foreach($arrayData as $data) {
$table .= "<tr id='Grid_Linha_1' class='odd' role='row'>
<td class = 'hidden-480'>${data['date']}</td>
...
</tr>";
}
$table .= '</table>';
return $table;
}
echo GetTableResult([
['date' => '2017-11-22'],
['date' => '2017-11-23'],
]);
The point is to store the generated HTML into a variable ($table
), append strings to it (with $table .= ...
) and use ${data['field_name']}
inside a string to insert a variable value.
I hope this helps!
Just a general advice: generating complex HTML manually is a painful process. I'd recommend having a look at a templating engine like Twig to ease the pain :).
Upvotes: 1