syntax error on php file add to cart [duplicate] - php

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.

Related

Get each array item from foreach loop as table data

I'm trying a foreach loop on an array with 20 names. I should derive a table with 4 columns and 5 rows, with each cell(table data) having a unique name. My code is below with a snapshot of the output table. It hasn't worked just yet. How can I fix this?
<?php
$names = array("Patrick","Raymond","George","Hosea","Samuel","Alan","June","Edwin","Yvonne","John","Paul","Ruto","Uhuru","Raila","Kalonzo","Sonko","Joho","Wetangula","Mudavadi","Matiang'i");
echo "<table width='200' border='1' >";
foreach($names as $name){
echo "<tr>";
for($cols=1;$cols<5;$cols++){
echo "<td>".$name."</td>";
}
echo "<tr>";
}
echo "<table>";
?>
1st : Remove for loop
2nd : apply the limit using $i
Note 1 : Your looping single name 5 times .that should not .
Note 2 : for more detail read my comment lines .
<?php
$names = array("Patrick","Raymond","George","Hosea","Samuel","Alan","June","Edwin","Yvonne","John","Paul","Ruto","Uhuru","Raila","Kalonzo","Sonko","Joho","Wetangula","Mudavadi","Matiang'i");
echo "<table width='200' border='1' >";
$i=0;
foreach($names as $name){
if($i==0){ //open new tr if $i is 0
echo "<tr>";
}
echo "<td>".$name."</td>";
if($i==3){ //close the tr if the $i is reached the 3 .
echo "</tr>";
$i=-1; //why setting -1 means i'm incrementing after this so i set -1
}
$i++;
}
echo "<table>";
?>
Splitting the array into chunks of the wanted size may make for more readable code:
$names = array("Patrick","Raymond","George","Hosea","Samuel","Alan","June","Edwin","Yvonne","John","Paul","Ruto","Uhuru","Raila","Kalonzo","Sonko","Joho","Wetangula","Mudavadi","Matiang'i");
echo "<table width='200' border='1' >";
$names = array_chunk($names, 4);
foreach($names as $group){
echo "<tr>";
foreach($group as $name) {
echo "<td>".$name."</td>";
}
echo "</tr>";
}
echo "<table>";

Converting spotify json file to html

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>";
?>

Dynamic Table Continuous <td> Count

I am stuck in a weird problem, I want to show the continuous number till the last in the row and cols,
instead of that its showing form 1-50 and then again the row starts with 1 up-to 50.
<?php
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>$td</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Thanks
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
$style = "green";
if ($tr % 2 == 0) {
$style = "#ccc";
}
echo "<tr style='background-color:".$style."'>";
for($td=1;$td<=$cols;$td++)
{
echo "<td>$x</td>";
$x++;
}
echo "</tr>";
}
echo "</table>";
You are outputting $td which resets at every new tablerow.
Instead, you would like to output an incrementing $x, if I'm not mistaken.
<?php
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>" . $x . "</td>";
$x++;
}
echo "</tr>";
}
echo "</table>";
?>
In response to your comment on another answer: if you would want alternating row colors, you could use $tr and check wether it is even or uneven:
if($tr % 2 == 0)
{
// use color1
}
else
{
// use color2
}

Sorting distinct results into different tables

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>";
?>

php fun codeing

hey I make a simple php table using nested for loop ... and it will be like this...
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
but the problem is , i can not print this value using a loop inside the column .. so what will be the solution ??? please
my code :
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
echo "MY PROBLEM HERE...I cant print column numbers \n";
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
//echo "MY PROBLEM HERE...I cant print column numbers \n";
echo $col + ($row - 1) * 5;
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
To save several loops:
$rows = 3;
$cols = 5;
$table = '<table border="1">';
for($i=1;$i<=$rows;$i++){
$table .= '<tr><td>'.implode('</td><td>', range($cols*$i-$cols+1,$cols*$i)).'</td></tr>';
}
$table .= '</table>';
echo $table;
It's not $col + $row * 5 it has to be $row - 1
<?php
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
echo $col + ($row-1) * 5;
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
?>

Categories