I am trying to convert a spotify json file to a html table. However this json file is a bit complex. I am making a mistake somewhere, but can't find out what I am doing wrong. What is the proper code?
<?php
$spotifylist = "http://spotifycharts.com/api/?type=regional&country=nl&recurrence=daily&date=latest&limit=200";
$contents = file_get_contents($spotifylist);
$decoded = json_decode($contents,true);
$results = $decoded->entries[0]->items;
echo "<table class='chart'> <thead><tr class='row2'><th class='dw'></th><th class='song'>Artiest</th><th class='song'>Titel</th></tr></thead><tbody>";
foreach($results as $entry){
$artist = $entry->track->artists->name;
$name = $entry->track->name;
$x = $entry + 1;
$color = ($x%2 == 0)? 'row2': 'row1';
echo "<tr class='$color'>";
echo "<td class='dw'>". $x ."</td>";
echo "<td class='song'>". $artist ."</td>";
echo "<td class='song'>". $name ."</td>";
echo "</tr>";
}
echo "</tbody></table>";
?>
regards
Your code is a bit confused. I've cleaned it up a little bit in order that it can work, but I think you need to look at it more closely – it pulls very precise pieces of data out of the feed, which might not be safe.
Working code below:
<?php
$spotifylist = "http://spotifycharts.com/api/?type=regional&country=nl&recurrence=daily&date=latest&limit=200";
$contents = file_get_contents($spotifylist);
$decoded = json_decode($contents);
$results = $decoded->entries->items;
echo "<table class='chart'> <thead><tr class='row2'><th class='dw'></th><th class='song'>Artiest</th><th class='song'>Titel</th></tr></thead><tbody>";
$counter = 0;
foreach($results as $entry){
++$counter;
$artist = (string)$entry->track->artists[0]->name;
$name = $entry->track->album->name;
$color = ($counter % 2 == 0) ? 'row2': 'row1';
echo "<tr class='$color'>";
echo "<td class='dw'>$counter</td>";
echo "<td class='song'>". $artist ."</td>";
echo "<td class='song'>". $name ."</td>";
echo "</tr>";
}
echo "</tbody></table>";
?>
Related
This question already has answers here:
syntax error in case 'remove' when decrementing [closed]
(3 answers)
Closed 8 years ago.
I'm getting various errors (syntax) on the following php file. The current php file is about onlineshops product-to-cart adding function. I get various errors. The errors start from the first echo and are listed as syntax error, unexpected 'echo'.
If I remove it, I get a syntax error, unexpected ';' on the next line.
Any help will be great. Thanks.
/* display cart */
if ( isset( $_SESSION['cart'] ) )
(
echo "<table border=0 cellspacing=0 cellpadding=0 width='500'>";
$total = 0;
foreach($_SESSION['cart'] as $id => $x)
(
$result = mysql_query("SELECT * from onlineshop WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
$name = $myrow['name'];
$name = substr($name,0,40);
$price = $myrow['price'];
$line_cost = $price * $x;
$total = $total + $line_cost;
echo "<tr>";
echo "<td allign='left'> $name </td>";
echo "<td align = 'right'>X $x <a href='cart.php?id=".$id."&action=remove'> reduce </a></td>";
echo "<td align = 'right'>= $line_cost £";
echo "</tr>";
)
echo "<tr>";
echo "<td align ='right'><br>Total=</td>";
echo "<td align ='right'><b><br> $total £</b></td>";
echo "</tr>";
echo "</table>";
)
else
echo "Cart is empty";
You use wrong braces. Replace () with {} in if statement and foreach loop :
if (isset($_SESSION['cart'])){
echo "<table border=0 cellspacing=0 cellpadding=0 width='500'>";
$total = 0;
foreach($_SESSION['cart'] as $id => $x){
$result = mysql_query("SELECT * from onlineshop WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
$name = $myrow['name'];
$name = substr($name,0,40);
$price = $myrow['price'];
$line_cost = $price * $x;
$total = $total + $line_cost;
echo "<tr>";
echo "<td allign='left'> $name </td>";
echo "<td align = 'right'>X $x <a href='cart.php?id=".$id."&action=remove'> reduce </a></td>";
echo "<td align = 'right'>= $line_cost £";
echo "</tr>";
}
echo "<tr>";
echo "<td align ='right'><br>Total=</td>";
echo "<td align ='right'><b><br> $total £</b></td>";
echo "</tr>";
echo "</table>";
}else
echo "Cart is empty";
You should use {} braces instead of () for if's or loop's content block.
if (isset($_SESSION['cart'])) {
//....
}
instead of
if (isset($_SESSION['cart'])) (
//....
)
And the same to foreach loop.
I'm trying to parse the arrivals table from here [1] and put in into an array to be able to format it and put it into a table.
I did some research here and there, I've got some code from other questions, but I can't make the array and table look as I'd like.
Anyone can help me out?
<?php
require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');
$table = $html->find('table', 3);
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$rowData = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$rowData[] = $cell->innertext;
}
echo "<table>";
echo "<td>";
echo $rowData[0]. " ";
echo "</td>";
echo "<td>";
echo $rowData[1]. " ";
echo "</td>";
echo "<td>";
echo $rowData[2]. " ";
echo "</td>";
echo "<td>";
echo $rowData[3]. " ";
echo "</td>";
echo "<td>";
echo $rowData[4]. " ";
echo "</td>";
echo "<td>";
echo $rowData[5]. " ";
echo "</td>";
echo "<td>";
echo $rowData[6]. " ";
echo "</td>";
echo "<td>";
echo $rowData[7]. " ";
echo "</td>";
echo "<td>";
echo $rowData[8]. " ";
echo "</td>";
echo "</table>";
}
?>
Maybe try putting each row into an array and then each cell into another array. Hopefully, that will do what you want.
require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');
$table = $html->find('table', 3);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$flight = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$flight[] = $cell->plaintext;
}
$rowData[] = $flight;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
Note: this solution requires the simple_html_dom.php library. Get it here!
I have table that contains information on classes like CPR, First Aid, etc...I would like to sort and display this information into a table for each class type. So all of the CPR classes would be in a table then a new table for the first aid classes, etc...
The code below currently puts each class into it's own table...I need to group them together by className if possible.
Thanks in advance for any help.
Scott
<?php
$result = mysql_query("SELECT * FROM providerClasses WHERE clientId = '$clientId' ORDER BY className");
$i = 0; // Start count for BG color
while($row = mysql_fetch_array($result)) {
echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\" class=\"gridTable\" width=\"975\" style=\"margin-bottom:100px;\">";
echo "<tr class=\"gridHeader\">";
echo "<td width=\"88\" class=\"grid-header-cell\">Date</td>";
echo "<td width=\"161\" class=\"grid-header-cell\">Time</td>";
echo "<td width=\"143\" class=\"grid-header-cell\">Instructor</td>";
echo "<td width=\"179\" class=\"grid-header-cell\">Location</td>";
echo "<td width=\"8\" class=\"grid-header-cell\">Price</td>";
echo "<td width=\"210\" class=\"grid-header-cell\">Class Name</td>";
echo "</tr>";
$className = "$row[className]";
$date = "$row[date]";
$time = "$row[time]";
$instructor = "$row[instructor]";
$location = "$row[location]";
$price = "$row[price]";
$comments = "$row[comments]";
$i++;
// Determine background color of row
if ( $i & 1 ) { $style = "GridRow-Even"; //even
}
else { $style = "GridRow-Odd"; //odd
}
echo "<tr class=\"$style\">";
echo "<td> $date</td>";
echo "<td> $time</td>";
echo "<td> $instructor</td>";
echo "<td> $location</td>";
echo "<td> $price</td>";
echo "<td> $className</td>";
echo "</tr>";
} // end while
echo "</table>";
?>
Here is my code so far now i want to combine this what i can do ?
<table>
<tr>
<th>Imported Files</th>
<th>Report Files</th>
</tr>
<?php
$dir = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/u*.[cC][sS][vV]");
$dir2 = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/r*.[cC][sS][vV]");
foreach(glob($dir) as $file) {
echo "<tr>";
echo "<td>". $file."</td>";
}
foreach(glob($dir2) as $file) {
echo "<td>". $file."</td>";
}
echo "</tr>";
?>
</table>
I want to print like this
echo "<tr>";
echo "<td>". $file1."</td>";
echo "<td>". $file2."</td>";
}
echo "</tr>";
i.e in the same td what I can do Help me
update:-
I want to print this in to td
$dir = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/u*.[cC][sS][vV]");
$dir2 = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/r*.[cC][sS][vV]");
This one handles files lists of different sizes.
Plus, you can have more than two columns of "file lists" displayed in the same table.
// retrieve data
$dirs = array(glob($dir), glob($dir2));
// display
$max_files = max(array_map('count', $dirs));
$count_dirs = count($dirs);
echo '<table>';
for($i = 0; $i < $max_files; $i++)
{
echo '<tr>';
for($d = 0; $d < $count_dirs; $d++)
{
$strFile = (isset($dirs[$d][$i]) ? $dirs[$d][$i] : '[NO FILE]');
echo '<td>'.$strFile.'</td>';
}
echo '</tr>';
}
echo '</table>';
$line .= "<tr>";
foreach(glob($dir) as $file) {
$line .= "<td>". $file."</td>";
}
foreach(glob($dir2) as $file) {
$line .= "<td>". $file."</td>";
}
$line .= "</tr>";
echo $line;
If they should be combined and both are of the same size, perhaps this will be a solution (untested)
$dir1_files = glob($dir);
$dir2_files = glob($dir2);
for ($i =0; $i < count($dir1_files); $i++)
{
echo "<tr>";
printf("<td>%s</td>", $dir1_files[$i]);
printf("<td>%s</td>", $dir2_files[$i]);
echo "</tr>";
}
PHP 5.3+ has a MultipleIterator for this, combined with the GlobIterator you can have a solution like this:
$iterator = new MultipleIterator();
$iterator->attachIterator(new GlobIterator($dir);
$iterator->attachIterator(new GlobIterator($dir2);
foreach ($iterator as $current) {
echo "<tr>";
echo "<td>". $current[0] ."</td>";
echo "<td>". $current[1] ."</td>";
echo "</tr>";
}
If you have a different number of files and want to display all of them, you can use the MultipleIterator::MIT_NEED_ANY Flag:
$iterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
$iterator->attachIterator(new GlobIterator($dir);
$iterator->attachIterator(new GlobIterator($dir2);
foreach ($iterator as $current) {
echo "<tr>";
echo "<td>". (isset($current[0]) ? $current[0] : ' ') ."</td>";
echo "<td>". (isset($current[1]) ? $current[1] : ' ') ."</td>";
echo "</tr>";
}
I got code snippet from this site, which I used as shown below
$data = array();
while($row = mysql_fetch_assoc($num1)) {$data['row'][] = $row;}
while($row = mysql_fetch_assoc($num2)) {$data['row2'][] = $row;}
$count = count($data['row']);
echo "<table>" ;
echo "<tr>";
echo "<td width='300' bgcolor='#99CCF5' align='Left' style='padding-left:30px'><b>Country</b></td>" ;
echo "<td width='150' bgcolor='#99CCF5' align='center'><b>Mid Estimate 1</b></td>";
echo "<td width='150' bgcolor='#99CCF5' align='center'><b>Mid Estimate 2</b></td>";
echo "</tr>";
for($i=0;$i<=$count;$i++)
{
if(($i % 2) == 1)
{
echo "<tr>" ;
echo "<td align='center'>" . $data['row'][$i]['Country']."</td>";
echo "<td align='center'>" . $data['row'][$i]['MidEstimate']."</td>";
echo "<td align='center'>" . $data['row2'][$i]['MidEstimate']."</td>";
echo "</tr>" ;
}else
{
echo "<tr>" ;
echo "<td align='center'>" . $data['row'][$i]['Country'] ."</td>";
echo "<td align='center'>" . $data['row'][$i]['MidEstimate']."</td>";
echo "<td align='center'>" . $data['row2'][$i]['MidEstimate']."</td>";
echo "</tr>" ;
}
}
echo "</table>" ;
which gives a reult like below
image1 http://img4.pixa.us/8ba/19338641.jpg
where the correct result should be like this
image2 http://img4.pixa.us/c1d/19338642.jpg
that is if any value in a column is empty the next adjucent value gets that position. How can I make this correct? that is if any value is empty that column must be empty.
please help and thanks in advance.
You have to gather the data for each country. Your approach in the question messes up the listing since the keys for the array are not in sync. Let's sync your rows by 'Country':
$data = array();
while($row = mysql_fetch_assoc($num1))
{
$c = $row['Country'];
if (!isset($data[$c]))
{
$data[$c] = array('Country' => $c);
}
$data[$c]['MidEstimate1'] = $row['MidEstimate'];
}
while($row = mysql_fetch_assoc($num2))
{
$c = $row['Country'];
if (!isset($data[$c]))
{
$data[$c] = array('Country' => $c);
}
$data[$c]['MidEstimate2'] = $row['MidEstimate'];
}
Now you have a row in your array for every Country, with their data from each query.
$i = 0;
foreach ($data as $row)
{
echo ($i % 2) ? "<tr class='odd'>" : "<tr class='even'>" ;
echo "<td align='center'>" . $row['Country']."</td>";
echo "<td align='center'>" . $row['MidEstimate1']."</td>";
echo "<td align='center'>" . $row['MidEstimate2']."</td>";
echo "</tr>" ;
}
Note: this only works in 'Country' field is present in both SQL query.