I am trying to create a table using PHP.
For some reason I can't display the key. Any idea why?
Here's the table.
Here's my code that I tried:
<table>
<?php
$users = array(
array('first_name' => 'RS', 'last_name' => 'AD'),
array('first_name' => 'SQ', 'last_name' => 'FS'),
array('first_name' => 'SA', 'last_name' => 'Guillen'),
array('first_name' => 'AS', 'last_name' => 'Gs')
);
$array_number = count($users)-1;
foreach($users as $key => $user) {
for($i=0; $i<=$array_number; $i++){
echo $users[$i]['first_name'].' '. $users[$i]['last_name'].'<br>';
}
}
?>
</table>
Any idea?
Try this
<table>
<tr>
<th>User #</th>
<th>First Name</th>
<th>Last Name</th>
<th>Full Name</th>
<th>Full Name in UpperCase</th>
</tr>
<?php
$users = array(
array('first_name' => 'Michael', 'last_name' => 'Choi'),
array('first_name' => 'John', 'last_name' => 'Supsupin'),
array('first_name' => 'Mark', 'last_name' => 'Guillen'),
array('first_name' => 'KB', 'last_name' => 'Tonel')
);
foreach ($users as $key => $user) {
echo "<tr>";
echo "<td>" , $key + 1 , "</td>";
echo "<td>" . $user['first_name'] . "</td>";
echo "<td>" . $user['last_name'] . "</td>";
echo "<td>" . $user['first_name'] . " " . $user['last_name'] . "</td>";
echo "<td>" . strtoupper($user['first_name']) . " " . strtoupper($user['last_name']) . "</td>";
echo "<td></td>";
echo "</tr>";
}
?>
</table>
Brief Explanation:
As you have shown within your image you need to define predefined headings which can be done using <th></th> and rest of those values you were getting from an $users array which can be placed within loop.
Note: Here you need to use single loop not nested loops as shown in example
The problem is that you loop over the array twice, once with the foreach and inside that with the for. But you need only one loop. Choose one.
Also, make it a habit to always use htmlspecialchars when echoing the contents of a variable. In this case it won't matter, but sometimes, you won't know where the variable has been!
Also, see Amol's answer.
You should use <tr> and <td>.
<table border="1">
<?php
$users = array(
array('first_name' => 'Michael', 'last_name' => 'Choi'),
array('first_name' => 'John', 'last_name' => 'Supsupin'),
array('first_name' => 'Mark', 'last_name' => 'Guillen'),
array('first_name' => 'KB', 'last_name' => 'Tonel')
);
$array_number = count($users)-1;
for($i=0; $i<=$array_number; $i++){
?>
<tr> <td><?php echo $users[$i]['first_name']; ?></td><td><?php echo $users[$i]['last_name']; ?></td> </tr>
<?php }
?>
</table>
<table> <?php
$users = array( array('first_name' => 'Michael', 'last_name' => 'Choi'), array('first_name' => 'John', 'last_name' => 'Supsupin'), array('first_name' => 'Mark', 'last_name' => 'Guillen'), array('first_name' => 'KB', 'last_name' => 'Tonel') );
$i = 1;
foreach($users as $key => $user) {
echo "<tr>";
echo "<td>" . $i . "</td>";
echo "<td>" . $user['first_name'] . "</td>";
echo "<td>" . $user['last_name'] . "</td>";
echo "</tr>"; $i++;
}
?>
</table>
Hope that will help
<table>
<tr>
<th>User #</th>
<th>First Name</th>
<th>Last Name</th>
<th>Full Name</th>
<th>Full Name in UpperCase</th>
</tr>
<?php
$users = array(
array('first_name' => 'Michael', 'last_name' => 'Choi'),
array('first_name' => 'John', 'last_name' => 'Supsupin'),
array('first_name' => 'Mark', 'last_name' => 'Guillen'),
array('first_name' => 'KB', 'last_name' => 'Tonel')
);
foreach ($users as $key => $user) {
echo "<tr>";
echo "<td>" , $key + 1 , "</td>";
echo "<td>" . $user['first_name'] . "</td>";
echo "<td>" . $user['last_name'] . "</td>";
echo "<td>" . $user['first_name'] . " " . $user['last_name'] . "</td>";
echo "<td>" . strtoupper($user['first_name']) . " " . strtoupper($user['last_name']) . "</td>";
echo "<td></td>";
echo "</tr>";
}
?>
`
Related
I have a multidimensional array of fruits below.
$fruits = [
'ORANGE' =>
[
'Size' => '0.20',
'Cost' => '0.49',
'Lbs.' => '0.60',
]
'LEMON' =>
[
'Size' => '0.15',
'Cost' => '0.29',
'Lbs.' => '0.20',
]
];
I want to display the fruit array like below, but it is not working as expected.
-----| ORANGE | LEMON |
Size | 0.20 | 0.15 |
Cost | 0.49 | 0.29 |
Lbs. | 0.60 | 0.20 |
My code below is not quite doing what I expected. Any suggestions? Thank you!
echo '<table id="fruits" style="width:400px;border:1px solid black;">' . PHP_EOL;
echo '<tbody>' . PHP_EOL;
foreach ($fruits as $fruitkey => $fruitvalue) {
echo '<th>' . $fruitkey . '</th>';
foreach ($fruitvalue as $key => $value) {
echo '<tr>' . PHP_EOL;
echo '<td>' . PHP_EOL;
echo $key . PHP_EOL;
echo '</td>' . PHP_EOL;
echo '<td>' . PHP_EOL;;
echo number_format($value, 2) . PHP_EOL;
echo '</td>' . PHP_EOL;
echo '</tr>' . PHP_EOL;
}
}
echo '</tbody>' . PHP_EOL;
echo '</table">' . PHP_EOL;
Can you try the below code
<?php
$fruits = [
'ORANGE' =>
[
'Size' => '0.20',
'Cost' => '0.49',
'Lbs.' => '0.60',
],
'LEMON' =>
[
'Size' => '0.15',
'Cost' => '0.29',
'Lbs.' => '0.20',
]
];
$keys = array_keys($fruits);
if(!empty($keys))
$innerKeys = array_keys($fruits[$keys[0]]);
echo '<table id="fruits" style="width:400px;border:1px solid black;">';
echo '<thead><tr>';
echo '<td>----</td>';
foreach($keys as $key)
echo '<td>'.$key.'</td>';
echo '</tr></thead>';
echo '<tbody>';
foreach($innerKeys as $inKey){
echo '<tr>';
echo '<td>'.$inKey.'</td>';
foreach($fruits as $fKey => $val){
echo '<td>'.$val[$inKey].'</td>';
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
Demo Link
Actually This are my real data from API.
$main2 = array(
'sup1' => array(
'01-Jun-2019-TO-03-Jun-2019' => array("connection_count" => 54, "source_type" => 'TATA'),
'04-Jun-2019-TO-08-Jun-2019' => array("connection_count" => 10, "source_type" => 'Rel'),
),
'sup2' => array(
'01-Jun-2019-TO-03-Jun-2019' => array("connection_count" => 54, "source_type" => 'TCS'),
'04-Jun-2019-TO-08-Jun-2019' => array("connection_count" => 55, "source_type" => 'Jio'),
),
);
We need same out put using this array.
we create table all date in header like first key date as display in header.
-then data display supplier sup1 in first row
sup2 in second row row
like below out put
|Supplier Name |01-Jun-2019-TO-03-Jun-2019 |04-Jun-2019-TO-08-Jun-2019|<br/>
|sup1|54|TATA|10|Rel|
<br/>
|sup2|54|TCS|55|JIO|
Here is your code modified:
<?php
$main2 = array(
'01-Jun-2019-TO-03-Jun-2019' => array(
'sup1' => array("connection_count" => 54, "source_type" => 'TATA'),
'sup2' => array("connection_count" => 10, "source_type" => 'Rel'),
),
'04-Jun-2019-TO-08-Jun-2019' => array(
'sup1' => array("connection_count" => 54, "source_type" => 'TCS'),
'sup2' => array("connection_count" => 55, "source_type" => 'Jio'),
),
);
$suplist = array('sup1', 'sup2');
echo "<table border=1><tr>";
echo "<td>Supplier Name</td>";
foreach ($main2 as $key => $value) {
echo "<td colspan=2>" . $key . "</td>";
}
echo "</tr>";
foreach ($suplist as $supplier) {
echo "<tr><td>$supplier</td>";
foreach ($main2 as $value) {
echo "<td>" . $value[$supplier]['connection_count'] . "</td>";
echo "<td>" . $value[$supplier]['source_type'] . "</td>";
}
echo "</tr>";
}
echo "</table>";
And here is sample output (reformatted for clarity):
<table border=1>
<tr>
<td>Supplier Name</td>
<td colspan=2>01-Jun-2019-TO-03-Jun-2019</td>
<td colspan=2>04-Jun-2019-TO-08-Jun-2019</td>
</tr>
<tr>
<td>sup1</td>
<td>54</td>
<td>TATA</td>
<td>54</td>
<td>TCS</td>
</tr>
<tr>
<td>sup2</td>
<td>10</td>
<td>Rel</td>
<td>55</td>
<td>Jio</td>
</tr>
</table>
Here is your code which works with updated input data format:
$main2 = array(
'sup1' => array(
'01-Jun-2019-TO-03-Jun-2019' => array("connection_count" => 54, "source_type" => 'TATA'),
'04-Jun-2019-TO-08-Jun-2019' => array("connection_count" => 10, "source_type" => 'Rel'),
),
'sup2' => array(
'01-Jun-2019-TO-03-Jun-2019' => array("connection_count" => 54, "source_type" => 'TCS'),
'04-Jun-2019-TO-08-Jun-2019' => array("connection_count" => 55, "source_type" => 'Jio'),
),
);
$suppliers = array_keys($main2);
$dateRanges = [];
foreach ($main2 as $supplierData) {
$dateRanges = array_merge($dateRanges, array_keys($supplierData));
}
$dateRanges = array_values(array_unique($dateRanges));
sort($dateRanges);
echo "<table border=1><tr>";
echo "<td>Supplier Name</td>";
foreach ($dateRanges as $dateRange) {
echo "<td colspan=2>" . $dateRange . "</td>";
}
echo "</tr>";
foreach ($suppliers as $supplier) {
echo "<tr><td>$supplier</td>";
foreach ($dateRanges as $dateRange) {
if (isset($main2[$supplier][$dateRange])) {
echo "<td>" . $main2[$supplier][$dateRange]['connection_count'] . "</td>";
echo "<td>" . $main2[$supplier][$dateRange]['source_type'] . "</td>";
} else {
echo "<td></td><td></td>";
}
}
echo "</tr>";
}
echo "</table>";
And here is sample output (formatted for clarity):
<table border=1>
<tr>
<td>Supplier Name</td>
<td colspan=2>01-Jun-2019-TO-03-Jun-2019</td>
<td colspan=2>04-Jun-2019-TO-08-Jun-2019</td>
</tr>
<tr>
<td>sup1</td>
<td>54</td>
<td>TATA</td>
<td>10</td>
<td>Rel</td>
</tr>
<tr>
<td>sup2</td>
<td>54</td>
<td>TCS</td>
<td>55</td>
<td>Jio</td>
</tr>
</table>
I have an array like this:-
$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test',
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test',
)
);
Now I show this data in HTML table like this for user 'test' :-
<thead>
<tr>
<th>Amount</th>
<th>User</th>
</thead>
<tbody>
<tr>
<td><?php
foreach ($str as $new_str) {
if ($new_str['user'] == "test") {
echo $new_str['amount'];
echo "<br />";
}
}
?></td><td><?php
foreach ($str as $new_str) {
if ($new_str['user'] == "test") {
echo $new_str['user'];
echo "<br />";
}
}
?></td>
</tr>
</tbody>
But now the problem is that when I use this code it shows the amount and user as a whole instead of two different rows. How can I fix this? Any help?
You just need to move your foreach loop outside of the <tr>...</tr> structure. This should work:
<?php foreach($str as $new_str){
if($new_str['user']=="test"){
echo "<tr><td>" . $new_str['amount'] . "</td><td>" . $new_str['user'] . "</td></tr>";
}
}
?>
Output (for your data)
<tr><td>0.9</td><td>test</td></tr>
<tr><td>1.4</td><td>test</td></tr>
Your tr was not repeating. output image I hope this will help.
<?php
$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test' ,
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test',
)
);
?>
<table>
<thead>
<tr>
<th>Amount</th>
<th>User</th>
</tr>
</thead>
<tbody>
<?php foreach($str as $new_str) {
if($new_str['user'] == "test"){
echo '<tr>';
echo '<td>'.$new_str['amount'].'</td>';
echo '<td>'.$new_str['user'].'</td>';
echo '</tr>';
}
} ?>
</tbody>
</table>
Right i will start by showing you the code as it will make sense quickly
private static $cases = [
'100' => [
'id' => '1',
'case_no' => '1',
'name' => 'First Case'
],
'101' => [
'id' => '2',
'case_no' => '2',
'name' => 'Second Case'
],
];
Now i want a table with 3 columns and two rows.
The column headers should be id case_no and name.
foreach($results as $key => $value){
echo '<tr>';
echo "<td>";
echo
echo "</td>";
echo "<td>";
echo
echo "</td>";
echo "<td>";
echo
echo "</td>";
echo '</tr>';
}
what do i need to put in there to make this happen?
EDIT::
In my model i find a specific case by the $case_no someone searches for.
public static function findByCaseNo($case_no)
{
foreach (self::$cases as $case) {
if (strcasecmp($case['case_no'], $case_no) === 0) {
return new static($case);
}
}
return null;
}
Then my Controller send back to the view results as that specific case
return $this->render('search', [
'model' => $model,
'dataProvider' => $model->findAll(),
'results' => $case,
'case_no' => $case_no
]);
You either need two loops or call it key by key
echo "<table>";
foreach($cases as $key=> $value){
echo "<tr>";
echo "<td>".$value['id']."</td>";
echo "<td>".$value['case_no']."</td>";
echo "<td>".$value['name']."</td>";
echo "</tr>";
}
echo "</table>";
Hey I have an array where I would like to sum the values of the first-level children at various levels of the array.
<?php
$group = Array('Electronics' => Array(
'6 - Cameras & Supplies' => Array(
'Cameras' => Array(
'Camcorders' => Array (
'Action Camcorders' => Array (
'total_ty_yest_sales' => 70.83,
'total_wo_dotcom_ty_yest_sales' => 401.59,
'east_ty_yest_sales' => 65.20
),
'Standard Camcorders' => Array (
'total_ty_yest_sales' => 96.09,
'total_wo_dotcom_ty_yest_sales' => 96.09,
'east_ty_yest_sales' => 68.21
),
'Surveillance' => Array(
'total_ty_yest_sales' => 84.00,
'total_wo_dotcom_ty_yest_sales' => 84.00,
'east_ty_yest_sales' => 26.00
)
),
'subCameras' => Array (
'Big Zoom' => Array(
'total_ty_yest_sales' => 31.66,
'total_wo_dotcom_ty_yest_sales' => 13.68,
'east_ty_yest_sales' => 1.47
),
'Pegged Cameras' => Array(
'total_ty_yest_sales' => 13.50,
'total_wo_dotcom_ty_yest_sales' => 5.50,
'east_ty_yest_sales' => 5.00
),
'Point-N-Shoot' => Array(
'total_ty_yest_sales' => 46.61,
'total_wo_dotcom_ty_yest_sales' => 10.35,
'east_ty_yest_sales' => 4.06
),
'Rugged Cameras' => Array(
'total_ty_yest_sales' => 87.04,
'total_wo_dotcom_ty_yest_sales' => 87.04,
'east_ty_yest_sales' => 65.20
),
'SLR' => Array(
'total_ty_yest_sales' => 50.19,
'total_wo_dotcom_ty_yest_sales' => 9.40,
'east_ty_yest_sales' => 1.37
)
)
) )
));
$totalSum = 0;
echo "<table>\n
<thead></thead>\n
<tbody>\n";
foreach($group as $gmm => $acctg_dept_nbrs) {
echo "<tr class=\"header\">\n
<td>" . $gmm . "</td>\n";
foreach ($acctg_dept_nbrs as $acctg_dept_nbr => $dept_catg_grp_descs) {
echo "<tr class=\"header\">\n
<td style=\"padding-left: 1em;\">" . $acctg_dept_nbr . "</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
$total_acctg_dept_nbr = 0;
foreach($dept_catg_grp_descs as $dept_catg_grp_desc => $dept_category_descs) {
echo "<tr class=\"header\">\n
<td style=\"padding-left: 2em;\">" . $dept_catg_grp_desc . "</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
$total_dept_catg_grp_desc = 0;
foreach($dept_category_descs as $dept_category_desc => $dept_subcatg_descs) {
echo "<tr class=\"header\">\n
<td style=\"padding-left: 3em;\">" . $dept_category_desc . "</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
foreach($dept_subcatg_descs as $dept_subcatg_desc => $values) {
echo "<tr>\n
<td style=\"padding-left: 4em;\">" . $dept_subcatg_desc . "</td>\n";
$sum = $values['total_ty_yest_sales'];
echo "<td style=\"width:100px;\">".$sum."</td>\n";
$sum = $values['total_wo_dotcom_ty_yest_sales'];
echo "<td style=\"width:100px;\">".$sum."</td>\n";
$sum = $values['east_ty_yest_sales'];
echo "<td style=\"width:100px;\">".$sum."</td>\n";
}
}
}
}
}
?>
I want to replace the "SUM"s in the output with the sum of the values of next level of indention.
This is not the most efficient thing to do, but you can create a function that calculates the sum of an array recursively:
function array_sum_recursive($array)
{
$sum = 0;
array_walk_recursive($array, function($item) use (&$sum) {
$sum += $item;
});
return $sum;
}