PHP output array as an HTML table
Try something like this:
$data = //your array
$html = "<table>";
foreach($data as $row) {
$html .= "<tr>";
foreach ($row as $cell) {
$html .= "<td>" . $cell . "</td>";
}
$html .= "</tr>";
}
$html .= "</table>";
Your question is extremenly vague so this is a vague answer, but you should get the general idea :P
Displaying data from an array in a HTML table using PHP
I'm going to make some assumptions, show code, and then explain:
provider
beingTTB
means thatTTB
is available, and theBT
is not, and inverse.- the data structure is an object with a key called products which is an array of many objects
- The download/upload is from the fields that begin with
likely_
- when you say table output, I'm assuming you mean HTML table
With that in mind, check out this code:
$result = new stdClass;
$result->products = [
(object) [
'name' => '20CN ADSL Max',
'likely_down_speed' => 1,
'likely_up_speed' => 0.1,
'availability' => 1,
'availability_date' => null,
'speed_range' => '0.75 to 2.5',
'provider' => 'WBC_20CN',
'technology' => 'ADSL',
'limited_capacity' => null
],
(object) [
'name' => 'TalkTalk FTTC',
'likely_down_speed' => null,
'likely_up_speed' => null,
'availability' => null,
'availability_date' => null,
'speed_range' => null,
'provider' => 'TBB',
'technology' => 'FTTC',
'limited_capacity' => null
],
];
echo "<table><tr>";
echo "<th>Download</th><th>Upload</th><th>BT</th><th>TBB</th>";
echo "</tr>";
foreach ($result->products as $product) {
echo "<tr>";
echo "<td>" . (int) $product->likely_down_speed . "</td>";
echo "<td>" . (int) $product->likely_up_speed . "</td>";
echo "<td>" . ($product->provider === 'TTB' ? "Not Available" : "Available") . "</td>";
echo "<td>" . ($product->provider !== 'TTB' ? "Available" : "Not Available") . "</td>";
echo "</tr>";
}
echo "</table>";
Here's what's going on.
The first variable $result
is what I think your data structure looks like. When there are blank outputs, I made them null.
Then, we create a table with the echo
statements to give the headers that you wanted.
Next, loop through the products. For each product make a row.
Output the first down/up as integers. Note that this will make null values output as zeros. You may want to change this.
Next, based on our provider, we'll either print available or not available. So, if the provider is TTB, that means we'll print that BT is not available, and TTB is.
The end closes up this table.
Display php array in a HTML Table
Here is your solution:-
// Define a result array
$result = [];
// first loop
foreach($arr as $k=>$val){
$id = $val['id'];
$groupArr = explode(",",$val['group']); // get all group ids
$latArr = explode(",",$val['lat']); // get lat array
$langArr = explode(",",$val['lng']); // get lng array
// second loop for group array
foreach($groupArr as $key=>$group){
$temp = []; // define a temp array
$temp['id'] = $id; // assign id
$temp['group'] = $groupArr[$key]; // assign group
$temp['lat'] = $latArr[$key]; // assign latitude
$temp['lng'] = $langArr[$key]; // assign langtitude
$result[$k][] = $temp; // assign record to $result array
}
}
// Table start
echo "<table>";
foreach ($result as $items) {
echo "<tr>";
echo "<th>id</th>";
echo "<th>group</th>";
echo "<th>lat</th>";
echo "<th>lng</th>";
echo "</tr>";
foreach($items as $item){
echo "<tr>";
echo "<td>{$item['id']}</td>";
echo "<td>{$item['group']}</td>";
echo "<td>{$item['lat']}</td>";
echo "<td>{$item['lng']}</td>";
echo "</tr>";
}
}
echo "</table>";
// Table end
output:-
id group lat lng
1 343 12 20
1 727 14 35
id group lat lng
2 555 55 99
2 7271 32 74
2 888 98 26
Display Php Array result in an Html table
You're close:
</thead>
<tbody>
<?php
foreach ($URLS as $URL){
echo'<tr>';
echo'<td>'. $URL['VideoTITLE']."</td>";
echo'<td>'. $URL['GroupName'].'</td>';
echo'<td>'. $URL['ByArtist'].'</td>';
echo'<tr>';
}
?>
</tbody>
Since you're taking the values of the $URLS
array and calling each one $URL
you need to refer to $URL
for each row's value. Not the $row
variable you originally used to populate the array from the database results.
FYI, you may want to look into htmlentities()
to escape your data to help prevent XSS attacks.
Display array result in html table using array_walk or array_map
You are confusing where you put the PHP code.
Try with this:
<?php
function myfunction($value, $key) {
echo '<tr><td>'.$value.'</td><td>Edit</td></tr>';
}
$a = array("a" => "user1", "b" => "user2", "c" => "user3");
$users = $this->db->get('user')->result();
echo '<table><tr><th>Name</th><th>Edit</th></tr><tbody>';
array_walk($a, "myfunction");
echo '</tbody></table>';
?>
How to populate an html table with data from an array in php
You have several problems.
- Array indexes start at
0
, not1
. But it's usually clearer to useforeach
. - You shouldn't hard-code the array lengths, use
count()
. - You're missing several
$
before variable names. - You're not printing the times from
$times
. They should be printed as a header line before the first day. - You shouldn't have
$i = $i + 1;
, as you'll increment$i
twice because of$i++
.
$days = array('Monday', 'Tuesday', 'Wednesday', 'Thurstday', 'Friday');
$times = array('08:00-08:45','09:00-09:45','10:05-10:50','11:05-11:50','12:05-12:50','13:20-14:05','14:20-15:05');
$columns = count($times);
echo "<table border='1'>";
echo "<tr><th>Day</th>;";
foreach ($times as $time) {
echo "<th>$time</th>";
}
echo "</tr>";
foreach ($days as $day) {
echo("<tr><th>$day</th>");
for ($i = 0; $i < $columns; $i++) {
echo "<td></td>"; // empty fields for each period
}
}
echo "</tr>";
echo("</table>");
Echo html table from PHP array
There are plenty of loops you can use to achieve this, the easiest case here is the foreach()
loop.
<!-- conditional loops -->
<?php foreach($data as $single): ?>
<!-- shorthand PHP statement to echo the result -->
<td> <?= $single['First']; ?> </td>
<?php endforeach; ?>
Note - Array index's start at 0, not 1. So the first value would be $data[0]['First']
If you're creating tables for each array inside it, you can use nested loops like this:
<?php for($i = 0; $i <= count($data); $i++):
foreach($data[$i] as $key => $val): ?>
<td>
<!-- ternary expression -->
<tr> <?= ($key == "First") ? $val : ""; ?> </tr>
</td>
<?php endforeach;
endfor;
If you prefer a different approach, you can use while()
loops to achieve the same idea:
<?php $i = 0;
while($i != count($data)): ?>
<tr> <?= $data[$i++]['First']; ?> </tr>
<?php endwhile;
Note for future - It's a lot easier working with bool datatypes rather than 'yes', 'no' strings. For example:
$paid = true;
echo ($paid) ? 'selected' : '';
rather than something like:
echo ($paid == 'No') ? '' : 'selected';
Display an array in html table
Assuming this is PHP and that the alignment is simply based on the index of the array:
<?php
$var['page'] = array('add', 'edit', 'delete', 'search');
$var['category'] = array('add', 'edit', 'export');
$pages = count($var['page']);
$categories = count($var['categories']);
$max = ($pages > $categories ? $pages : $categories);
echo '<table>';
for ($i = 0; $i < $max; $i++)
{
echo '<tr>';
echo "<td>{$var['page'][$i]}</td>";
echo "<td>{$var['category'][$i]}</td>";
echo '</tr>';
}
echo '</table>';
?>
Related Topics
How to Bind Decimal/Double/Float Values with Pdo in PHP
How to Send an HTML Email with an Inline Attached Image with PHP
PHP Optional Parameters - Specify Parameter Value by Name
How to Do Url Rewriting in PHP
How to Deploy Correctly When Using Composer's Develop/Production Switch
How to See the Extensions Loaded by PHP
Best Place for Validation in Model/View/Controller Model
How to Test PHP Locally Without Installing a Server
Laravel Eager Loading - Load Only Specific Columns
How to Get User's Screen Resolution with PHP
Best Practice for Error Handling Using Pdo
Is There a Limit When Using PHP Mail Function
Looping a SQL Insert Statement with Dates
What Does a Leading Zero Do for a PHP Int