I need to convert JSON into a list using PHP, Tried code below but cannot make it work
$json=file_get_contents("http://feeds.mse.mk/service/FreeMSEFeeds.svc/ticker/JSON/8BA941D0-D6E6-44BD-8D8B-47FDB7A563FA");
$data = json_decode($json);
if (count($data->stand)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->stand as $idx => $stand) {
// Output a row
echo "<tr>";
echo "<td>$stand->AvgPrice</td>";
echo "<td>$stand->Description </td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
And I want to show list as here (not as a table):
http://prntscr.com/no1479
your all code is right but you can use stand class that is wrong your class is GetTickerJSONResult and so change the class stand to GetTickerJSONResult.
try this modified code..
<?PHP
$set =json_decode($json);
if (count($set->GetTickerJSONResult)) {
echo "<table>";
foreach ($set->GetTickerJSONResult as $idx => $stand) {
echo "<tr>";
echo "<td>$stand->AvgPrice</td>";
echo "<td>$stand->Description </td>";
echo "</tr>";
}
echo "</table>";
}
?>
It does not work because in $data->stand there is nothing.
Related
I have table with some data, and some other data I get with ajax.
I need to know how to display the response inside the table?
Code:
<?php $x = 1; foreach ($projects as $project){
echo "<tr class=\"parent\">";
echo "<td><i class=\"fa fa-chevron-down\"></td>";
echo "<td class=\"pid\">$project[pid]</td>";
echo "<td>$project[status]</td>";
echo "<td>$project[project_title]</td>";
echo "<td>$project[notes]</td>";
echo "<td>$project[responsible]</td>";
echo "<td>$project[start_date]</td>";
echo "<td>$project[completed_date]</td>";
echo "<td>$project[duration]</td>";
echo "<td>$project[completed]</td></tr>";
echo "<div class=\"cchild\" id=\"txtHint$x\">";
echo "</div>";
echo "</tbody>";
$x++;
}
The data should show inside the div element. As far as I know it's not allowed to put the div inside the table, but any other idea. How to get the data properly inside the table.
You first need to initialize a var with starting div and table tag and then in the for loop you can compute and append the tr td tags to the var. Once the loop is completed you can append the closing tags of div and table and the final output of the var will be table. Reference code:
$project = [1,2,3,4,5];
$div = "<div><table><thead></thead><tbody>";
foreach ($project as $p){
$div.="<tr><td>".$p."</td></tr>";
}
$div.="</tbody></table></div>";
You can add an empty tr with in table with specific class like
<?php $x = 1; foreach ($projects as $project){
echo "<tr class=\"parent\">";
echo "<td><i class=\"fa fa-chevron-down\"></td>";
echo "<td class=\"pid\">$project[pid]</td>";
echo "<td>$project[status]</td>";
echo "<td>$project[project_title]</td>";
echo "<td>$project[notes]</td>";
echo "<td>$project[responsible]</td>";
echo "<td>$project[start_date]</td>";
echo "<td>$project[completed_date]</td>";
echo "<td>$project[duration]</td>";
echo "<td>$project[completed]</td></tr>";
echo "<tr><td><div class="cchild"></div></td></tr>";
echo "</tbody>";
$x++;
}
Then replace the cchild div content with your ajax response.
<?php
$bookrec=array(
'book1'=>array('callno'=>123005,'price'=>number_format(1380,2),'desc'=>'Attack on Titan Anthology'),
'book2'=>array('callno'=>123006,'price'=>number_format(844,2),'desc'=>'Binge'),
'book3'=>array('callno'=>123004,'price'=>number_format(598,2),'desc'=>'A Work in Progress'),
'book4'=>array('callno'=>123003,'price'=>number_format(668,2),'desc'=>'The Amazing Book is Not on Fire: The World of Dan and Phil'),
'book5'=>array('callno'=>123002,'price'=>number_format(760,2),'desc'=>'Children of Eden: A Novel')
);
$sumbook=array();
echo "<table>";
echo "<tr><b>
<td>Call Number</td><td>Price</td><td>Book Title</td>
</b></tr>";
while(list($booknum,$rec)=each($bookrec)){
echo "<tr>";
foreach($rec as $data){
echo "<td>". $data."<br></td>";
foreach($data as $k => $val){
if(array_key_exists($val,$sumbook))
$sumbook[$val]['price']=$sumbook[$val]['price']+$data['price'];
else if($k == 'desc' && $k == 'callno')
$sumbook[$val] = $data;
}
}
}
echo "<br></tr>";
echo "</table>";
echo "TOTAL AMOUNT: Php ".$sumbook;?>
I am finishing this code for next week. Also, I am a student and just beginning in PHP. My problem is that I am trying to output the sum of ['price'] in the given array but it gives me this error:
Invalid argument supplied for foreach
I also tried various ways on solving this problem but I don't really get it. Please help. :) Thanks!!
<?php
$bookrec=array(
'book1'=>array('callno'=>123005,'price'=>number_format(1380,2),'desc'=>'Attack on Titan Anthology'),
'book2'=>array('callno'=>123006,'price'=>number_format(844,2),'desc'=>'Binge'),
'book3'=>array('callno'=>123004,'price'=>number_format(598,2),'desc'=>'A Work in Progress'),
'book4'=>array('callno'=>123003,'price'=>number_format(668,2),'desc'=>'The Amazing Book is Not on Fire: The World of Dan and Phil'),
'book5'=>array('callno'=>123002,'price'=>number_format(760,2),'desc'=>'Children of Eden: A Novel')
);
$sumbook=array();
echo "<table>";
echo "<tr><b>
<td>Call Number</td><td>Price</td><td>Book Title</td>
</b></tr>";
foreach($bookrec as $key1=>$rec )
{
echo "<tr>";
foreach($rec as $key2=>$data)
{
echo "<td>". $data."<br></td>";
if($key2="price")
{
$total+=$data;
}
}
echo "</tr><br>";
}
echo "<br></tr>";
echo "</table>";
echo "TOTAL AMOUNT: Php ".$sumbook;?>
I think you have lots of looping error just follow this code and also understand the how to iterate the array using foreach
$sumbook=array();
$total =0;
echo "<table>";
echo "<tr><b>
<td>Call Number</td><td>Price</td><td>Book Title</td>
</b></tr>";
foreach($bookrec as $key1=>$rec )
{
echo "<tr>";
foreach($rec as $key2=>$data)
{
echo "<td>". $data."<br></td>";
if($key2="price")
{
$total+=$data;
}
}
echo "</tr><br>";
}
echo "</table>";
echo "TOTAL AMOUNT: Php ".$total;
?>
Your first foreach loop had a wrong variable name with the one you initialized as your array.
foreach($bookrec as $data){
}
My output is in the form of a 2d array. I have uploaded a sample file and the output is displayed like a paragraph. I want it to be displayed as a table.
Code:
<?php
require("reader.php"); // php excel reader
$file="sample.xls";
$connection=new Spreadsheet_Excel_Reader(); // our main object
$connection->read($file);
$startrow=1;
$endrow=1000;
for($i=$startrow;$i<$endrow;$i++){ // we read row to row
for($j=1;$j<=30;$j++) {
// so we get [2][3] and [3][3]
echo $connection->sheets[0]["cells"][$i][$j];
echo "\n";
}
echo "\n";
}
?>
Try this:
echo "<table>";
for($i=$startrow;$i<$endrow;$i++){ // we read row to row
echo "<tr>";
for($j=1;$j<=30;$j++) {
echo "<td>";
echo $connection->sheets[0]["cells"][$i][$j];
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
I am trying to sort an associative array in ascending in order and then transfer it to a HTML table and I am currently stumped at an error. I looked for guidelines here on SO and followed instructions on some posts:
PHP display associative array in HTML table
But still no luck, here is my attempt:
<?php
function format($g){
array_multisort($g, SORT_ASC);
echo "<table>";
foreach($g as $key=>$row) {
echo "<tr>";
foreach($row as $key2=>$row2){
echo "<td>" . $row2 . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
$bib = array("Luke"=>"10",
"John"=>"30",
"Matt"=>"20",
"Mark"=>"40");
format($bib);
?>
My debugger is telling me there is an error at my for each loop but I don't see how it is wrong unless there is some syntax error that I am not seeing? The error is saying Invalid argument supplied for foreach()
Because your $bib is only single array but you use two foreach to loop this array
At 2nd loop, your $row variable is a string, you can't use foreach for this type
Can you try that for single array ?
<?php
function format($data) {
array_multisort($data, SORT_ASC);
echo "<table>";
foreach($data as $k => $v) {
echo "<tr>";
echo "<td>$k</td>";
echo "<td>$v</td>";
echo "</tr>";
}
echo "</table>";
}
$bib = array("Luke"=>"10",
"John"=>"30",
"Matt"=>"20",
"Mark"=>"40");
format($bib);
?>
$k is Luke John Matt and Mark,
$v is 10 30 20 and 40
You can see the foreach example here: http://php.net/manual/en/control-structures.foreach.php
Hope this helpful ^^
You can try this
<?php
function format($data){
array_multisort($data, SORT_ASC);
echo "<table>";
foreach($data as $key => $row) {
echo "<tr>";
echo "<td>" . $key . "</td>";
echo "<td>" . $row . "</td>";
echo "</tr>";
}
echo "</table>";
}
$bib = array(
"Luke"=>"10",
"John"=>"30",
"Matt"=>"20",
"Mark"=>"40"
);
format($bib);
?>
I have tried the following code:
$car_row = $car_xpath->query('//h3[#class="adtitlesnb"]');
$car_row2 = $car_xpath->query('//div[#class="snb_price_tag"]');
$i = 0;
echo "<table><thead><tr><td>Car Name</td><td>Price</td></tr></thead><tbody>";
foreach($car_row as $row){
echo "<tr><td>";
echo $row->nodeValue;
echo "</td><td>";
echo $car_row2->nodeValue;
echo "</td></tr>";
}
echo "</tbody></table>";
You can't iterate over one array with foreach and expect the other to follow.
foreach essentially resets the current index on an array, then loops through calling next on the array until there are no elements left. Using reset and next you can emulate this for your second array as follows:
$car_row = $car_xpath->query('//h3[#class="adtitlesnb"]');
$car_row2 = $car_xpath->query('//div[#class="snb_price_tag"]');
echo "<table><thead><tr><td>Car Name</td><td>Price</td></tr></thead><tbody>";
$row2 = reset($car_row2); // set the internal array pointer to the begining
foreach($car_row as $row) {
echo "<tr><td>";
echo $row->nodeValue;
echo "</td><td>";
echo $row2->nodeValue;
echo "</td></tr>";
$row2 = next($car_row2); // retrieve the next node from car_row2
}
echo "</tbody></table>";