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>
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.
I have a JSON feed which I am parsing via PHP. I am having issues getting some nested elements to echo which I would appreciate some assistance on.. I've looked at loads of related posts here but cant seem to get the logic to work on my specific JSON feed. Could someone advise what I am doing wrong?
JSON feed is here > https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json
The elements, I am struggling to parse are the "categories" parent and child nodes of "team", "location" and "commitment".
I was thinking this would work - but it does not...
<?php
$url = 'feed.json';
$data = file_get_contents($url);
$characters = json_decode($data, true);
?>
<table>
<tbody>
<tr>
<th>Job title</th>
<th>Team</th>
<th>Location</th>
<th>Commitment</th>
<th>DescriptionPlain</th>
<th>applyUrl</th>
</tr>
<?php foreach ($characters as $character) : ?>
<tr>
<td> <?php echo $character['text']; ?> </td>
<td> <?php echo $character['categories'][2]['team'] ?></td>
<td> <?php echo $character['categories'][2]->team ?></td>
<td> <?php echo $character['categories'][1]->location ?></td>
<td> <?php echo $character['categories'][0]->commitment ?></td>
<td> <?php echo $character['descriptionPlain']; ?> </td>
<td> <?php echo $character['applyUrl']; ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
Note, its just the categories children that fail to echo? Also noticed that if I use the full url in the $url variable it all fails? But from local it works??
Any ideas??? Thanks!
It should be:
<td> <?php echo $character['categories']["team"] ?></td>
<td> <?php echo $character['categories']["location"] ?></td>
<td> <?php echo $character['categories']["commitment"] ?></td>
instead. The numeric keys are not present on the array in the data. Also "categories" is not an object, so you cannot use the arrow (->) notation.
EDIT
You get an error because you are trying to access an object, actually you have an array, here is the solution hope it helps :
<?php
$url = 'https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json';
$data = file_get_contents($url);
$characters = json_decode($data, true);
$nb = count($characters);
?>
<table>
<tbody>
<tr>
<th>Job title</th>
<th>Team</th>
<th>Location</th>
<th>Commitment</th>
<th>DescriptionPlain</th>
<th>applyUrl</th>
</tr>
<?php while($nb > 0){
$nb--;
$nb_lists = count($characters[$nb]['lists']);
?>
<tr>
<?php
while($nb_lists > 0){
$nb_lists--;
?>
<td> <?php if(isset($characters[$nb]['lists'][$nb_lists]['text'])){ echo $characters[$nb]['lists'][$nb_lists]['text'];} ?> </td>
<?php } ?>
<td> <?php if(isset($characters[$nb]['categories']['team'])){echo $characters[$nb]['categories']['team'];} ?></td>
<td> <?php if(isset($characters[$nb]['categories']['team'])){echo $characters[$nb]['categories']['team'];} ?></td>
<td> <?php if(isset($characters[$nb]['categories']['location'])) {echo $characters[$nb]['categories']['location'];} ?></td>
<td> <?php if(isset($characters[$nb]['categories']['commitment'])){ echo $characters[$nb]['categories']['commitment'];} ?></td>
<td> <?php if(isset($characters[$nb]['descriptionPlain'])){echo $characters[$nb]['descriptionPlain']; }?> </td>
<td> <?php if(isset($characters[$nb]['applyUrl'])){echo $characters[$nb]['applyUrl'];} ?> </td>
</tr>
<?php } ?>
</tbody>
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>
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>
I want to print out my database into a table, but it isn't working properly.
Here's my code:
<?php
$title = 'Adminpaneel | Gemeente Loket Den Haag';
require 'header.php';
require 'connect.php';
$query = "SELECT * FROM aanvraag";
$result = mysqli_query($db, $query);
?>
<table border="2" >
<thead>
<tr>
<th>Voornaam</th>
<th>Achternaam</th>
<th>email</th>
<th>datum</th>
<th>adres</th>
<td>huisnummer</td>
<td>Postcode</td>
<td>Woonplaats</td>
<td>Product</td>
</tr>
</thead>
<tbody>
<?php
while( $row = mysqli_fetch_assoc($result) ){ ?>
<tr>
<td> <php echo $row['voornaam']; ?> </td>
<td> <php echo $row['achternaam']; ?> </td>
<td> <php echo $row['email']; ?> </td>
<td> <php echo $row['datum']; ?> </td>
<td> <php echo $row['adres']; ?> </td>
<td> <php echo $row['huisnr']; ?> </td>
<td> <php echo $row['postcode']; ?> </td>
<td> <php echo $row['woonplaats']; ?> </td>
<td> <php echo $row['product']; ?> </td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
require 'footer.aanvragen.php';
?>
The rows are created but the database values are missing. What seems to be the issue?
This is the table structure:
Try with this code for the html table:
<?php
while( $row = mysqli_fetch_assoc($result) ){ ?>
<tr>
<td> <?php echo $row['voornaam']; ?> </td>
<td> <?php echo $row['achternaam']; ?> </td>
<td> <?php echo $row['email']; ?> </td>
<td> <?php echo $row['datum']; ?> </td>
<td> <?php echo $row['adres']; ?> </td>
<td> <?php echo $row['huisnr']; ?> </td>
<td> <?php echo $row['postcode']; ?> </td>
<td> <?php echo $row['woonplaats']; ?> </td>
<td> <?php echo $row['product']; ?> </td>
</tr>
<?php } ?>
as suggested by Abhik Chakraborty
<td> <php echo $row['voornaam']; ?> </td>
One line of the output (but it is the same for all the lines):
PHP needs to start with
So the solution will be:
<td><?php echo $row['voornaam']; ?> </td>
But for all the rows...