I have a nested associative array that prints out users data in a table. Here is the code:
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Prenume</th>
<th>Nume de familie</th>
<th>Email</th>
<th>Telefon</th>
<th>Oras</th>
<th>Adresa</th>
</tr>
</thead>
<tbody>
<?php foreach ($user_data as $arr){ ?>
<tr>
<td>
row number nedded here
</td>
<?php foreach ($arr as $key => $value){ ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
<?php }?>
</tbody>
</table>
I need to display the row number instead at the most left, instead of "row number nedded here"
You could just add a variable as a counter and display that:
<tbody>
<?php $counter=0; foreach ($user_data as $arr){ ?>
<tr>
<td>
<?php echo ++$counter; ?>
</td>
<?php foreach ($arr as $key => $value){ ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
<?php }?>
</tbody>
You can do it like this, if you have not any key you have index number starting from 0.
<tbody>
<?php foreach ($user_data as $key=>$arr){ ?>
<tr>
<td>
<?php echo $key+1 ;?>
</td>
<td>
<?php echo $arr["prenume"];?>
</td>
<td>
<?php echo $arr["nume"];?>
</td>
<td>
<?php echo $arr["email"];?>
</td>
...............
</tr>
<?php }?>
</tbody>
Related
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 can i show data in two row separately as shown in image
I have tried this but i am not getting what i want. Also i don't want to use two loops separately.
<table class="tbl1">
</thead>
<tbody>
<tr>
<td>
<h1> date</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++;?>
<td>
<?php echo $value[ 'date']; ?>
<?php } ?>
</tr>
<tr>
<td>
<h1>Status</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++; ?>
<td>
<?php echo $value[ 'status']; ?>
</td>
<?php } ?>
</tr>
</tr>
</tbody>
</table>
Do a single loop and in that loop include 2 <tr>s for date and status.
<table class="tbl1">
<tbody>
<?php foreach ($student as $value) { ?>
<tr>
<td><h1> date</h1> </td>
<td><?php echo $value['date']; ?>
</tr>
<tr>
<td><h1>Status</h1></td>
<td><?php echo $value['status']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Also i don't see the purpose of using $i here and what about the rest of the column in front of date and status.
Or if you want only 2 rows and display all data column wise then you could do it like
<?php
$dateHTML = '';
$statusHTML = '';
foreach ($student as $value) {
$dateHTML .= '<td>'.$value['date'].'</td>';
$statusHTML .= '<td>'.$value['status'].'</td>';
}
?>
<table class="tbl1">
<tbody>
<tr>
<td><h1> date</h1> </td>
<?php echo $dateHTML;?>
</tr>
<tr>
<td><h1>Status</h1></td>
<?php echo $statusHTML;?>
</tr>
</tbody>
</table>
So, i've table like in picture
I want put looping row person on left side to singgle row select box on right side, like by color. And this is a code table right side.
<table>
<tr>
<td>No.</td>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<?php if(!empty($query)) { $number=1 ; foreach($query as $row) { ?>
<tr>
<td>
<?php echo $number++ ?>.</td>
<td>
<?php echo $row->column_a ?></td>
<td>
<?php echo $row->column_b ?></td>
<td>
<?php echo $row->column_c ?></td>
</tr>
<?php }} ?>
</table>
How to control looping iteration, to make this is happen?
Tanks a lot.
My friend just help to fix my problem,
<table>
<tr>
<td>No.</td>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<?php if(!empty($query)) { $number=1;$count=0; foreach($query as $row) { ?>
<?php if($count % 3 == 0){ ?>
<tr>
<td>
<?php echo $number++ ?>.</td>
<?php } ?>
<td>
<?php echo $row->column_a ?></td>
<td>
<?php echo $row->column_b ?></td>
<td>
<?php echo $row->column_c ?></td>
<?php $count++; if($count % 3 == 0){ ?>
</tr>
<?php }}} ?>
</table>
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>
I have a database which works by displaying first and last_names of all employees in the database, i can display it but when it displays, it's not formatted. I want to try and put the results in a table but I'm not sure how I would.
I thought I would have to echo json_encode(echo.<td>$posts</td>) or something like that
<?php foreach($query as $row): ?>
<tr>
<td>
<?php $arr = array(
'first_name' => $row->first_name,
'last_name' => $row->last_name,
); ?>
<?php $posts[] = $arr;?>
</tr>
<?php endforeach; ?>
<?php echo json_encode($posts);?>
This is how its displayed now
[{"first_name":"Georgi","last_name":"Facello"},
{"first_name":"Georgi","last_name":"Atchley"}]
Nothing is written between your <tr> and such .. you are just assigning to posts and then printing it out as JSON after the fact which makes no sense.
<?php foreach... ?>
<tr>
<td>
<?php echo $row->first_name ?>
</td>
<td>
<? php echo $row->last_name ?>
</td>
</tr>
<?php endforeach ?>
You're gonna have to build the table 'by hand'. Like this:
<table>
<?php foreach($query as $row): ?>
<tr>
<td>
<?php echo $row->first_name; ?>
</td>
<td>
<?php echo $row->last_name; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<table>
<thead>
<th> First Name </th>
<th> Last Name </th>
</thead>
<tbody>
<?php foreach($query as $row): ?>
<tr>
<td> <?php echo $row->first_name ?> </td>
<td> <?php echo $row->last_name ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>