HTML Table structure not displaying data in right order - php

I have a table that displays data from JSON. I am able to display the data in the table but it does not render the table very well.
The structure of the table is distorted. It does not display the data in the order in which I want it to display.
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Number</th>
<th>Amount</th>
</tr>
</thead>
<?php
$url = 'xxxxxxxxxxxxxxxxxxxxxx'; // path to your JSON file
//$url = 'data.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data);
$i = 0;
?>
<tbody>
<tr>
<?php
foreach ($characters as $character)
{
?>
<td>
<?php echo $character->name; ?>
</td>
<td>
<?php echo $character->phoneNumber; ?>
</td>
<td>
<?php echo $character->amount; ?>
</td>
<?php }
?>
</tr>
</tbody>
</table>
I expect three columns only. With the data showing in the right order

Try
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Number</th>
<th>Amount</th>
</tr>
</thead>
<?php
$url = 'xxxxxxxxxxxxxxxxxxxxxx'; // path to your JSON file
//$url = 'data.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data);
$i = 0;
?>
<tbody>
<?php
foreach ($characters as $character)
{
?>
<tr>
<td>
<?php echo $character->name; ?>
</td>
<td>
<?php echo $character->phoneNumber; ?>
</td>
<td>
<?php echo $character->amount; ?>
</td>
</tr>
<?php }
?>
</tbody>
</table>

Related

How to highlight larger value in each table row?

Below is the base code I am trying for a product comparison.
How can I highlight the larger/small value ?
For example
Consider the price as an example.
attached result screenshot
attached excel example with highlight on value
$results = $mysqli->query('SELECT * FROM filterr where pid in ('.$pid1.''.$pid2.''.$pid3.''.$pid4.') ');
?>
// SETP FOUR : Displaying table values through HTML Table
<table>
<thead>
<tr>
<th>Details</th>
<th>Model 1</th>
<th>Model 2</th>
<th>Model 3</th>
<th>Model 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>Manufacturer</td>
<?php foreach ($results as $result){ ?>
<td> <?php echo $result['product_brand']; ?> </td>
<?php } ?>
<tr>
<tr>
<td>Price</td>
<?php foreach ($results as $result){ ?>
<td> <?php echo $result['product_price']; ?> </td>
<?php } ?>
<tr>
<tr>
<td>Rating</td>
<?php foreach ($results as $result){ ?>
<td> <?php echo $result['score']; ?> </td>
<?php } ?>
<tr>
<tr>
<td>Battery</td>
<?php foreach ($results as $result){ ?>
<td> <?php echo $result['battery']; ?> </td>
<?php } ?>
<tr>
<tr>
<td>Storage</td>
<?php foreach ($results as $result){ ?>
<td> <?php echo $result['product_storage']; ?> </td>
<?php } ?>
<tr>
</tbody>
</table>
you can use simple if like :
.green{background-color:green;}
<?php if( $result['product_price'] > 5000 ){ //higher then ?>
<td class='green'>
<?php }else{ ?>
<td>
<?php } ?>
You can use same method for lower with another class.

How to set different styles for different rows in html table created from php array?

I'm struggling with creating a html table from php array, while applying different css classes on different rows according to the array.
I have this code in data.php:
<?php
$data = array(
array('Title 1'=>'text11', 'Title 2'=>'text12'),
array('Title 1'=>'text21', 'Title 2'=>'text22'),
array('Title 1'=>'text31', 'Title 2'=>'text32'),
array('Title 1'=>'text41', 'Title 2'=>'text42', 'special'=>'style1'),
array('Title 1'=>'text51', 'Title 2'=>'text52', 'special'=>'style2'),
);
?>
I want to create a html table from this array, and if the array contains 'special'=>'style', it would set that style to that particular row. This is my code so far:
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if ($row == 'class1') {
$class='class="style1"';
} elseif ($row == 'class1') {
$class='class="style2"';
} else {
$class='';
}?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
And this is the desired output:
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>text11</td><td>text12</td>
</tr>
<tr>
<td>text21</td><td>text22</td>
</tr>
<tr>
<td>text31</td><td>text32</td>
</tr>
<tr class="style1">
<td>text41</td><td>text42</td>
</tr>
<tr class="style2">
<td>text51</td><td>text52</td>
</tr>
</tbody>
</table>
Your problem is this line:
<?php foreach ($data as $key=>$row):
You're forgetting that you're looping over a multidimensional array (i.e. an array of arrays) so that $row is an array here. On the next line:
if ($row == 'class1')
You're looking for a comparison between a string and $row which is an array. This will never work! As Daniel points out in his answer, you need to look at the contents of the array.
Don't forget you'll need to remove the special element from the array before displaying it. Personally, I'd condense the code a bit, though mixing PHP and HTML like this is never a good idea.
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): $class = $row["special"] ?? ""; unset($row["special"]);?>
<tr class="<?=$class?>">
<td><?=implode("</td><td>", $row)?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I'm not sure I understood everything but you can try this
<?php foreach ($data as $key => $row):
$class = '';
if(isset($row['special'])){
$class = 'class="'.$row['special'].'"';
unset($row['special']);
}
?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
If I understand your question correctly, this should do the job.
The result is as you desire, see: Online PHP shell
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if (isset($row["special"])) {
$class = " class='" . $row["special"]. "'";
unset($row["special"]);
} else {
$class='';
}?>
<tr<?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

PHP TCPDF heredoc

I got this in my heredoc in my TCPDF.
I basically want to create a dynamic pdf with the data of my database.
$html = <<<EOD
<table border="1">
<thead>
<tr>
<th>firstname</th>
<th>lastname<th>
</tr>
</thead>
<tbody>
<tr>
<th></th>
<th></th>
</tr>
</tbody>
</table>
EOD;
I want to make this dynamic like this, to create the data from my database dynamically.
<?php
foreach($result_set as $result) {
?>
<tr>
<td>
<?php echo $result['firstname']; ?>
</td>
<td>
<?php echo $result['lastname']; ?>
</td>
</tr>
<?php
}
?>
I tried this so far but I cannot find a suitable solution:
$html = <<<EOD
<table border="1">
<thead>
<tr>
<th>Vorname</th>
<th>Nachname</th>
<th>Von</th>
<th>Bis</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?php echo $result['firstname']; ?>
</td>
<td>
<?php echo $result['lastname']; ?>
</td>
</tr>
</tbody>
</table>
EOD;
can anyone help please.
Ok I somehow did it, but still if there is any better solution feel free to post:)
Code:
$loopHereFirstname = '';
$loopHereLastname = '';
foreach($result_set_random_01 as $result_dish_usr_01) {
$tr_start = '<tr>';
$tr_end = '</tr>';
$td_start = '<td>';
$td_end = '</td>';
$loopHereFirstname .= $result_dish_usr_01['firstname']."\n";
$loopHereLastname .= $result_dish_usr_01['lastname']."\n";
}
$html = <<<EOD
<table border="1">
<thead>
<tr>
<th>firstname</th>
<th>lastname</th>
</tr>
</thead>
<tbody>
$tr_start
$td_start
$loopHereFirstname
$td_end
$td_start
$loopHereLastname
$td_end
$tr_end
</tbody>
</table>
EOD;
You've to break your code into two part if you want to use a loop with <<
<?php
$html =<<<EOD
<table border="1">
<thead>
<tr>
<th>Vorname</th>
<th>Nachname</th>
<th>Von</th>
<th>Bis</th>
</tr>
</thead>
<tbody>
EOD;
foreach($result_set as $result) {
$html.=<<<EOD
<tr>
<td>
{$result['firstname']}
</td>
<td>
{$result['lastname']}
</td>
</tr>
EOD;
}
$html .= '</tbody></table>';

foreach loop not generating the right html table

I'm trying to get this output from returned data fetched from mysql:
<table>
<thead>
<tr>
<th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Features</td>
<td>LCD</td><td>Yes</td><td>No</td>
<td>Auto Pilot</td><td>No</td><td>No</td>
<td>Fast Generation</td><td>Yes</td><td>No</td>
<td>Dual Cores</td><td>No</td><td>Yes</td>
</tr>
</tbody>
</table>
But I have trouble getting the following code to achieve that output with one foreach loop. It uses in_array to check whether each value from $featured_tests exists in the returned data.
$featured_tests = array("LCD","Auto Pilot","Fast Generation","Dual Cores");
$table_head ="<table><thead><tr><th></th>";
$table_colspan1 = "</tr></thead><tbody><tr><td colspan='5'>Features</td></tr>";
$table_end ="</tr></tbody></table>";
foreach($rows as $row)
{
$special_features = explode(",",$row->special_features);
$header_image .= "<th><img src='".$row->image_url."'></th>";
foreach($featured_tests as $featured_test)
{
$featured .= "<tr><td>".$featured_test."</td>";
if(in_array($featured_test,$special_features))
{
$featured .= "<td>Yes</td>";
}
else
{
$featured .= "<td>No</td>";
}
}
}
$table_html = $table_head.$header_image.$table_colspan1.$featured.$table_end;
But the result I'm getting is a mess. Each value in $featured_tests is iterating over and over again for each product and thus results in a very long table. Can anyone help me correct my code to get the ideal output?
Here's the result:
<table>
<thead>
<tr>
<th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Features</td>
</tr>
<tr>
<td>LCD</td><td>Yes</td>
</tr>
<tr>
<td>Auto Pilot</td>No<td></td>
</tr>
<tr>
<td>Fast Generation</td><td>Yes</td>
</tr>
<tr>
<td>Dual Cores</td>No<td>
</tr>
<tr>
<td>LCD</td><td>No</td>
</tr>
<tr>
<td>Auto Pilot</td>No<td></td>
</tr>
<tr>
<td>Fast Generation</td><td>No</td>
</tr>
<tr>
<td>Dual Cores</td>Yes<td>
</tr>
</tbody>
</table>
you are creating new rows <tr> inside on most deep foreach...
take this example to make what you want:
<table>
<thead>
<th>Col 1</th><th>Col 2</th>
</thead>
<tbody>
<?php foreach($large_array as $less_array): ?>
<tr>
<?php foreach($less_array as $row): ?>
<!-- <td> contents etc</td>-->
<?php endforeach?>
</tr>
<?php endforeach;?>
</tbody>
</table>

Putting html and PHP code inside a variable

I have this code that produces a table exactly how I want it. I want to put this whole code assigned to a PHP variable e.g.: $table=the posted code. I tried concatenating and heredoc but couldn't get it to output my table as it is when doing and echo $table;.
Any input is appreciated
<table id=patients>
<tr>
<th>Pt. username</th>
<th>Pt. number</th>
<th>Full Name</th>
<th>Added on</th>
</tr>
<?php $x=1;
foreach ($users as $patient) {
?> <tr <?php if ($x % 2 == 0) {echo "class='alt'"; } ?>>
<td> <?php echo $patient['username'];?></td>
<td> <?php echo $patient['id'];?></td>
<td> <?php echo $patient['name'];?></td>
<td> <?php echo $patient['joined'];?></td>
</tr>
<?php
$x++;
} ?>
</table>
Just use output buffering to put the output into the internal buffer and then capture it.
<?php
ob_start();
?>
<table id=patients>
<tr>
<th>Pt. username</th>
<th>Pt. number</th>
<th>Full Name</th>
<th>Added on</th>
</tr>
<?php $x=1;
foreach ($users as $patient) {
?> <tr <?php if ($x % 2 == 0) {echo "class='alt'"; } ?>>
<td> <?php echo $patient['username'];?></td>
<td> <?php echo $patient['id'];?></td>
<td> <?php echo $patient['name'];?></td>
<td> <?php echo $patient['joined'];?></td>
</tr>
<?php
$x++;
} ?>
</table>
<?php
$table = ob_get_clean();
?>

Categories