Creating table out from this array structure in php - php

Im having trouble creating a table out from this array:
http://pastebin.com/DXFjfhHJ
I started out with this:
<table style="width: 100%; text-align: center;">
<tr>
<td>Time</td>
<td>Aktivitet</td>
<td>Duration</td>
<td>Metabolisation</td>
</tr>
then i did:
foreach{$training as $time => $metabolisation}{
?>
<tr style="text-align: left;">
<td><?php echo $time; ?></td>
<td>Activity name</td>
<td>Duration</td>
<td><?php echo $metabolisation; ?></td>
</tr>
<?php
}
Which works almost.. It shows the right $time ( 03:00 etc. ), but nothing in $metabolisation. And I dont know how to call the Activity, it should be the arrays "name" variable. Same with duration, it should be the arrays "duration"
How can I do this?

Well the structure your using in your pastebin is not compatible with your foreach. You realy need to look deeper in your array structure like this.
<?php foreach( $training as $time => $data ): ?>
<tr style="text-align: left;">
<td><?php echo $time; ?></td>
<td>Total <?php echo $data['entries'][0]['name']; ?></td>
<td>Duration <?php echo $data['entries'][0]['duration']; ?></td>
<td><?php echo $data['entries'][0]['metabolisation']; ?></td>
</tr>
<?php endforeach; ?>

foreach($training as $time => $metabolisation){
foreach($metabolisation['entries'] as $entry) {
?>
<tr style="text-align: left;">
<td><?php echo $time; ?></td>
<td><?php echo $entry['name']; ?></td>
<td><?php echo $entry['duartion']; ?></td>
<td><?php echo $entry['metabolisation']; ?></td>
</tr>
<?php
}
}
?>
It probably doesn't output the data in an appropriate form, but you get the idea how to access it.

Related

Parsing XML from a URL address and converting into a table using PHP

I'm trying to take the following xml from this url http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml and make a table with the data. I've verified I can pull the data by using print_r($xml); but I can't get the data to dump into the table. This is what I've got so far, which is probably wrong. Can anyone help me out with the proper code to use?
<?php
$url = "http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml";
$xml = simplexml_load_file($url);
?>
<table>
<thead>
<tr>
<col><span style="font-weight:bold">Day</span></col>
<col><span style="font-weight:bold">Time(Eastern)</span></col>
<col><span style="font-weight:bold">Reservoir Elev. (behind dam)*</span</col>
<col><span style="font-weight:bold">Tailwater Elev. (below dam)*</span></col>
<col><span style="font-weight:bold">Avg Hourly Discharge*</span></col>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET->ROW as $obs) :?>
<tr>
<td><?php echo $obs->obs_day; ?></td>
<td><?php echo $obs->obs_hr; ?></td>
<td><?php echo $obs->upstream_elev; ?></td>
<td><?php echo $obs->downstream_elev; ?></td>
<td><?php echo $obs->avg_hourly_discharge; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As the XML has 2 <RESULTSET> elements, the RESULTSET->ROW was just picking the first one. So change that to RESULTSET[1]->ROW and you will get the data your after.
You also need to ensure that you use the same case for each element name...
<?php foreach ($xml->RESULTSET[1]->ROW as $obs) :?>
<tr>
<td><?php echo $obs->OBS_DAY; ?></td>
<td><?php echo $obs->OBS_HR; ?></td>
<td><?php echo $obs->UPSTREAM_ELEV; ?></td>
<td><?php echo $obs->DOWNSTREAM_ELEV; ?></td>
<td><?php echo $obs->AVG_HOURLY_DISCHARGE; ?></td>
</tr>
<?php endforeach; ?>

Change row color of a table based on mysqli value using materializecss

I'm using Materialize css on my project, i have a table that shows mysql field called "status" and in this table i want change the color of the row if i change the "status" like "1=blue, 2=red..." Someone here knows how i can make a function to do this? Thank you.
table extample:
table class="striped bordered responsive-table">
<thead>
<tr>
<th>ID</th>
<th>Cliente</th>
<th>Objeto</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php while($row_os = mysqli_fetch_assoc($result_user)){?>
<tr>
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
</tbody>
One simple way would be:
<?php
$colorMap = [
1 => 'blue',
2 => 'red',
// add more
];
while ($row_os = mysqli_fetch_assoc($result_user)) { ?>
<tr style="background:<?php echo $colorMap[$row_os['status']] ?>">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
Of course you could also add a class depending on status the same way and do the styling in CSS.
Simple as Doing this on the row , only if the status must contain a value of either 1 or 2
<?php
while($row_os = mysqli_fetch_assoc($result_user)){?>
<tr class="<?php echo $row_os["status"]==1?'blue':'red'?> lighten-2">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
or if status varies (then follow #colburton answers ) or this
<?php
while($row_os = mysqli_fetch_assoc($result_user)){
$color="";
switch($row_os["status"]){
case 1:
$color="blue";
break;
case 2:
$color="red";
break;
//and so on
}
?>
<tr class="<?php echo $color;?> lighten-2">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>

Display json content in php table

I am trying to display json content in php table but I'm getting error every time. I have some syntax error and cant figure out what should i change?
PS. Trying built it with Slim framework
Here is my code:
<div class="data-table-wrapper">
<?php
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
?>
<table class="data-table">
<thead>
<tr>
<td>Date</td>
<td>Time</td>
<td>Round</td>
<td>Circuit</td>
<td>Location</td>
</tr>
</thead>
<?PHP
foreach($myObject as $key=>$item);
?>
<tr>
<td><?PHP echo $item->Date; ?></td>
<td><?PHP echo $item->Time; ?></td>
<td><?PHP echo $item->Round; ?></td>
<td><?PHP echo $item->Circuit; ?></td>
<td><?PHP echo $item->Location; ?></td>
</tr>
<?PHP
}
?>
</table>
</div>
My error is:
Notice: Undefined property: stdClass::$Date in C:\xampp\htdocs\challenge\app\view\challenge.php on line 38
Notice: Undefined property: stdClass::$Time in C:\xampp\htdocs\challenge\app\view\challenge.php on line 39
Notice: Undefined property: stdClass::$Round in C:\xampp\htdocs\challenge\app\view\challenge.php on line 40
Notice: Undefined property: stdClass::$Circuit in C:\xampp\htdocs\challenge\app\view\challenge.php on line 41
I have just looked at your json format in comparison to the code. The path mapping to the json is incorrect. I have rectified that below;
Please review the following:
PHP code:
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
$myObjectMap = $myObject->MRData->RaceTable->Races;
For each format:
<?php foreach($myObjectMap as $key => $item): ?>
<tr>
<td><?PHP echo $item->date; ?></td>
<td><?PHP echo $item->time; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->Circuit->circuitId; ?></td>
<td><?PHP echo $item->Circuit->Location->country; ?></td>
</tr>
<?php endforeach; ?>
Full Code:
<html>
<head>
<title>PHP</title>
</head>
<body>
<?php
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
$myObjectMap = $myObject->MRData->RaceTable->Races;
?>
<table>
<thead>
<tr>
<td>Date</td>
<td>Time</td>
<td>Round</td>
<td>Circuit</td>
<td>Location</td>
</tr>
</thead>
<tbody>
<?php foreach($myObjectMap as $key => $item): ?>
<tr>
<td><?PHP echo $item->date; ?></td>
<td><?PHP echo $item->time; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->Circuit->circuitId; ?></td>
<td><?PHP echo $item->Circuit->Location->country; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
The problem is that decoded array looks differently. Dump your encoded array before the loop to understand the right data structure or use JSON beautify service. Also use the right upper/lowercase to address properties. Updated loop may look like this:
<div class="data-table-wrapper">
<?php
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
?>
<table class="data-table">
<thead>
<tr>
<td>Date</td>
<td>Time</td>
<td>Round</td>
<td>Circuit</td>
<td>Location</td>
</tr>
<?PHP
foreach($myObject->MRData->RaceTable->Races as $key=>$item){
?>
<tr>
<td><?PHP echo $item->date; ?></td>
<td><?PHP echo $item->time; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->Circuit->circuitName; ?></td>
<td><?PHP echo $item->Circuit->Location->locality; ?></td>
</tr>
<?PHP
}
?>
</table>
</div>
</div>

I got Error Notice: Array to string conversion in my code php

hi guys i got problem in my code
i don't know where come form this my code
<h1><?php echo $item["title"]; ?> </h1>
<table>
<tr>
<th>Catageory</th>
<td><?php echo $item["category"]; ?></td>
</tr>
<tr>
<th>Genre</th>
<td><?php echo $item["genre"]; ?></td>
</tr>
<tr>
<th>Format</th>
<td><?php echo $item["format"]; ?></td>
</tr>
<tr>
<th>Year</th>
<td><?php echo $item["year"]; ?></td>
</tr>
<?php
if(strtolower($item["category"]) == "books"){
?>
<tr>
<th>Authors</th>
<td><?php echo $item["authors"]; ?></td>
</tr>
<tr>
<th>Publisher</th>
<td><?php echo $item["publisher"]; ?></td>
</tr>
<tr>
<th>ISBN</th>
<td><?php echo $item["isbn"]; ?></td>
</tr>
<?php } ?>
And the error message,
and Iam sure all code are same i don't what's problem !!
As per your image, you are trying to print string on line 58, but you are getting array from your resultant query. So try to var_dump($yourvar) and check that are you getting required string or an array
You are trying to echo an array variable which is not right. Use the below debug code and you have to parse the array however you want.
Authors -> <?php echo "<pre>";print_r($item["authors"]);echo "<pre>"; ?>
my problem is solved
by replace
<td><?php echo $item["authors"]; ?></td>
to
<td><?php echo implode(",", $item["authors"]); ?></td>

merge two foreach in one layout

i got two arrays.
from results i want to make a table with foreach
but i dont know how to make it work in one table row..
this is what i got
<table>
<?php foreach ($appky as $appka) : ?>
<tr class="counter_apps" height="20px" >
<td width="40%"><?php echo $appka->name; ?></td>
<td width="20%"><?php echo $appka->all_items;?></td>
<td width="20%"><?php echo $appka->published; ?></td>
<td width="20%"><?php echo $appka->unpublished; ?></td>
</tr>
<?php endforeach; ?>
</table>
<table>
<?php foreach ($applications as $application) : ?>
<tr><td><?php echo $application->name; ?></td></tr>
<?php endforeach; ?>
</table>
so what i want is simply add to the first table another column with $application->name;
what i'm missing here??
thanks
<table>
<?php
$count = count($appky);
for($i=0; $i< $count; $i++) { ?>
<tr class="counter_apps" height="20px" >
<td width="40%"><?php echo $appky[$i]->name; ?></td>
<td width="20%"><?php echo $appky[$i]->all_items;?></td>
<td width="20%"><?php echo $appky[$i]->published; ?></td>
<td width="20%"><?php echo $appky[$i]->unpublished; ?></td>
<td width="20%"><?php echo $applications[$i]->name; ?></td>
</tr>
<?php } ?>
</table>
use for instead of foreach
<table>
<?php for($i=0 ; $i<count($appky) ; $i++ ) { ?>
<tr class="counter_apps" height="20px" >
<td width="40%"><?php echo $appka[$i]->name; ?></td>
<td width="20%"><?php echo $appka[$i]->all_items;?></td>
<td width="20%"><?php echo $appka[$i]->published; ?></td>
<td width="20%"><?php echo $appka[$i]->unpublished; ?></td>
<td width="20%"><?php echo isset($applications[$i]) ? $applications[$i]->name : '' ; ?></td>
</tr>
<?php } ?>
</table>
Assuming your two arrays aren't necessarily in order - If you have some data that matches between your $appka and $application you can try something like this below:
<table>
<?php foreach ($appky as $appka) : ?>
<tr class="counter_apps" height="20px" >
<td width="40%"><?php echo $appka->name; ?></td>
<td width="20%"><?php echo $appka->all_items;?></td>
<td width="20%"><?php echo $appka->published; ?></td>
<td width="20%"><?php echo $appka->unpublished; ?></td>
<?php foreach ($applications as $application) : ?>
<?php if ($appka->id == $application->id) { ?>
<td><?php echo $application->name; ?></td>
<?php } ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Otherwise you wont be able to spit out the correct application name. The above solution uses an id field, but you probably hove something else you can match on.

Categories