Displaying Data from an Array in a HTML Table Using PHP

Displaying data from an array in a HTML table using PHP

I'm going to make some assumptions, show code, and then explain:

  • provider being TTB means that TTB is available, and the BT 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.

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

How to populate an html table with data from an array in php

You have several problems.

  1. Array indexes start at 0, not 1. But it's usually clearer to use foreach.
  2. You shouldn't hard-code the array lengths, use count().
  3. You're missing several $ before variable names.
  4. You're not printing the times from $times. They should be printed as a header line before the first day.
  5. 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>");

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 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>';
?>

merge 3 arrays and display the data inside horizontale HTML table

It looks like you're just missing the opening and closing <tr>s, in the outer for loop. You could add the tags like this:

for ($i = 0; $i < count($mg); $i++) {
$html .= '<tr>'; //open row
for($j = 0; $j < count($mg); $j++) {
$html .='<td>'.$mg[$i][$j].'</td>';
}
$html .= '</tr>'; //close row
}


Related Topics



Leave a reply



Submit