Sorting distinct results into different tables - php

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

Related

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

syntax error on php file add to cart [duplicate]

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.

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

query in php while loop firing but giving results of one variable

i have a timetable application which needs to generate timetables for all teachers in one go based on the semester. i am querying teacher id(s) and inside the while loop i am using the same code used to generate an individual teacher's timetable. according to mysql logs, query is firing for every teacher id but same timetable is being generated for all teachers.
i checked every line of code but its somehow not working.
I am so stuck on this, please help.
my php code:
<?php
include ("includes/functions.php");
$timespan = mysql_real_escape_string($_GET['timespan']);
$q11 = "select * from teachers order by teacher";
$r11 = mysql_query($q11) or die(mysql_error());
while($rows11 = mysql_fetch_array($r11)){
$teacher = $rows11['id'];
$array_day=array('Mon' => 0, 'Tue' => 1, 'Wed' => 2, 'Thu' => 3, 'Fri' => 4, 'Sat' => 5);
if($timespan=="BS"){
$qs="";
}
else if ($timespan=="MS"){
$qs=" and (partno='1' or partno='3' or partno='5' or partno='9') ";
}
else if ($timespan=="WS"){
$qs=" and (partno='2' or partno='4' or partno='6' or partno='9') ";
}
else if ($timespan=="1"){
$qs=" and partno='1' ";
}
else if ($timespan=="2"){
$qs=" and partno='2' ";
}
else if ($timespan=="3"){
$qs=" and partno='3' ";
}
else if ($timespan=="4"){
$qs=" and partno='4' ";
}
else if ($timespan=="5"){
$qs=" and partno='5' ";
}
else if ($timespan=="6"){
$qs=" and partno='6' ";
}
else if ($timespan=="7"){
$qs=" and partno='7' ";
}
else if ($timespan=="8"){
$qs=" and partno='8' ";
}
else if ($timespan=="10"){
$qs=" and partno='1' ";
}
else if ($timespan=="11"){
$qs=" and partno='2' ";
}
else if ($timespan=="9"){
$qs=" and partno='9' ";
}
if($qs==""){
$q1="SELECT tt.day as day, tt.period as period, tt.egstatus as egstat, tt.resp_id as id, tt.sub_group, tt.filter, c.shortname as c_s, p.shortname as p_s, tt.teacherid, tt.subjectid as subject, r.room as room, t.apd1 as apd1, t.apd2 as apd2, g.groupname as class, t.teacher as teacher, p.courseid as course, p.partno as part FROM course c, papers p, ttresponsibility tt, teachers t, groups g, rooms r where c.id=p.courseid and p.paperid = tt.subjectid and tt.teacherid= t.id and tt.room=r.room_id and tt.groupname=g.group_id and tt.teacherid = '$teacher' order by tt.day,tt.period,p.partno ASC";
}else{
$q1="SELECT tt.day as day, tt.period as period, tt.egstatus as egstat, tt.resp_id as id, tt.sub_group, tt.filter, c.shortname as c_s, p.shortname as p_s, tt.teacherid, tt.subjectid as subject, r.room as room, t.apd1 as apd1, t.apd2 as apd2, g.groupname as class, t.teacher as teacher, p.courseid as course, p.partno as part FROM course c, papers p, ttresponsibility tt, teachers t, groups g, rooms r where c.id=p.courseid and p.paperid = tt.subjectid and tt.teacherid= t.id and tt.room=r.room_id and tt.groupname=g.group_id and tt.teacherid = '$teacher'" .$qs. " order by tt.day,tt.period,p.partno ASC";
}
$r1 = mysql_query($q1) or die ("responsibility fetch error:" .mysql_error());
if(mysql_num_rows($r1) > 0){
$q2 = "select teacher,name2,type from teachers where id = '$teacher'";
$r2 = mysql_query($q2) or die("teacher fetch error:" .mysql_error());
$rows1 = mysql_fetch_array($r2);
//$num = mysql_num_rows($r1);
echo "<table width=100% style='border:0px ;font-family:arial;font-size:10px; border- collapse:collapse;cellspacing:0;padding:0px;'><tr><td width=50% align=left border=0 style='border:0px ;font-family:arial;font-size:10px; border-collapse:collapse;cellspacing:0;padding:0px;'>Dated: 31st July 2012</td><td style='border:0px ;font-family:arial;font-size:10px; border-collapse:collapse;cellspacing:0;padding:0px;' width=50% align=right border=0>Version 2.0</td></tr></table>";
echo "<center><font size=5><strong>Shri Ram College of Commerce</strong></font></center>";
echo "<br>";
echo "<table width=100%><tr><td align=left style='border:0px ;font-family:arial;font-size:10px; border-collapse:collapse;cellspacing:0;padding:0px;' ><font size=4><strong>Teacher: ";
if($rows1['type']==1){
echo "".$rows1['name2']."";
}else{
echo "".$rows1['teacher']."";
}
echo "</strong></h3></font></b></td>";
if ($timespan=="MS"){
echo "<td align=right style='border:0px ;font-family:arial;font-size:10px; border-collapse:collapse;cellspacing:0;padding:0px;'><font size=4><strong>Time Span: Monsoon Term</strong></font></br></td></tr></table>";
}
else if ($timespan=="WS"){
echo "<td align=right style='border:0px ;font-family:arial;font-size:10px; border- collapse:collapse;cellspacing:0;padding:0px;'><font size=4><strong>Time Span: Winter Term</strong></font></br></td></tr></table>";
}
echo "<br>";
echo "<table width='100%' border='1' class='tt'>";
echo "<tr class='heads'>";
echo "<td rowspan='2' style='padding:0;text-align:center; width:1%;'> <b>Period<br>Time</b></td>";
echo "<td style='padding:0;text-align:center; '><b>I</b></td>";
echo "<td style='padding:0;text-align:center; '><b>II</b></td>";
echo "<td style='padding:0;text-align:center; '><b>III</b></td>";
echo "<td style='padding:0;text-align:center; '><b>IV</b></td>";
echo "<td style='padding:0;text-align:center; '><b>V</b></td>";
echo "<td style='padding:0;text-align:center; width:1%;' ><b>Break</b></td>";
echo "<td style='padding:0;text-align:center; '><b>VI</b></td>";
echo "<td style='padding:0;text-align:center; '><b>VII</b></td>";
echo "<td style='padding:0;text-align:center; '><b>VIII</b></td>";
echo "<td style='padding:0;text-align:center; '><b>IX</b></td>";
echo "</tr>";
echo "<tr class='heads'>";
echo "<td style='padding:0;text-align:center; '><b>8:40 AM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>9:35 AM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>10:30 AM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>11:25 AM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>12:20 PM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>1:15 PM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>2:00 PM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>2:55 PM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>3:50 PM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>4:45 PM</b></td>";
echo "</tr>";
$k="ToFetchNextRow";
$l = 0;
$rowToProcess=0;
$toRepeat=1;
$num=mysql_num_rows($r1);
$num1=$num-1;
for($i=0;$i<=5;$i++){
$day = array_search($i, $array_day);
echo "<tr class='period'><td style='padding:0;text-align:center;'>
<b>".$day."</b></td>";
for($j=0;$j<=8;$j++){
if($i==0 and $j==5){
echo "<td style='xycell' rowspan=6 > </td>";
}
$noOfEntriesAddedInThisCell=0;
$toRepeat=1;
while($toRepeat==1){
$rows = mysql_fetch_array($r1);
$data[] = $rows; // creating multidimensional array
$dt = $data[$rowToProcess]['day'];
$period = $data[$rowToProcess]['period'];
$paper = $data[$rowToProcess]['p_s'];
$course = $data[$rowToProcess]['c_s'];
$room = $data[$rowToProcess]['room'];
$class = $data[$rowToProcess]['class'];
$id = $data[$rowToProcess]['id'];
$part = $data[$rowToProcess]['part'];
$subgroup = $data[$rowToProcess]['sub_group'];
$weekselector = $data[$rowToProcess]['filter'];
$egstatus = $data[$rowToProcess]['egstat'];
//process the row to be processed
//add the cell entry if it is of interest
if(($i+1) == $dt and ($j+1) == $period){
// check if the slot is busy
if($egstatus == 1){
echo "<td style='xycell' valign=center align=center class='tt'><b><div style='border- bottom:0px solid #bbb;padding:0;font-family:arial;font-size:11px'> <center>Contact<br>Hour</center></td>";
}
elseif($egstatus == 2){
echo "<td style='xycell' valign=center align=center><b><div style='border-bottom:0px solid #bbb;padding:0;font-family:arial;'><a class='slot' style='border-bottom:0px solid #ccc;' href='#' id='status".$dt."+".$period."' onclick='editSlot(".$dt.','.$period.','.$teacher.','.$id."); return false'>Busy</td>";
}
elseif($egstatus == 0){
$e = $i + 1;
$f = $j + 1;
if($noOfEntriesAddedInThisCell==0){
echo "<td valign=center align=center style='border:2px solid #000;padding:0;font-family:arial;'";
if($subgroup!=0){
echo "class='orangeBlock'";
}else{
echo "class='yellowBlock'";
}
echo "><b>";
}
echo "<table class='' style='border:0px solid #000;font-family:arial;border-collapse:collapse;cellspacing:0;padding:0px;'>";
echo "<tr>";
echo "<td style='border-collapse:collapse;border:0px ;font-size:11px; font-family: arial, arial,helvetica, sans-serif;' valign='top'><b> ".$course."-".$part." ".$class."";
if($subgroup!=0){
echo "".$subgroup." ";
}
echo "".$paper." ".$room."";
if($weekselector==1){
echo "(O)</td>";
}
if($weekselector==2){
echo "(E)</td>";
}
echo "</td></tr></table>";
}
$noOfEntriesAddedInThisCell = $noOfEntriesAddedInThisCell+1;
if($rowToProcess < $num1){
$rowToProcess=$rowToProcess+1;
}else{
$toRepeat=0;
}
}else{
if($noOfEntriesAddedInThisCell !=0){
//close the internal table cell
echo "</td>";
}else{
//fill the empty cell
$r = $i + 1;
$s = $j + 1;
echo "<td valign='top' class='tt'><br><br><br></td>";
}
$toRepeat=0;
}
}//while closing
}//jloop closing
}// closing i
echo "</tr>";
echo "</table>";
echo "<br><br><br><br>";
echo "<table width=100% style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;'>";
echo "<tr><td style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;' align=left style=font-size:11px> Convenor</td> <td style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;' align=center style=font-size:11px>Teacher Incharge</td><td style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;' align=right style=font-size:11px>Principal</td></tr>";
echo "<tr><td style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;' align=left style=font-size:11px style='border-collapse:collapse;border:0px solid #000;'> Vikas Madan</td><td style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;' align=center style=font-size:11px>Amit Sachdeva</td><td style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;' align=right style=font-size:11px>P. C. Jain</td></tr>";
echo "</table>";
}
else{
echo "";
}
}
?>
edited code snippet:
while($toRepeat==1){
while($rows = mysql_fetch_array($r1)){
$data[] = $rows; // creating multidimensional array
$dt = $data[$rowToProcess]['day'];
$period = $data[$rowToProcess]['period'];
$paper = $data[$rowToProcess]['p_s'];
$course = $data[$rowToProcess]['c_s'];
$room = $data[$rowToProcess]['room'];
$class = $data[$rowToProcess]['class'];
$id = $data[$rowToProcess]['id'];
$part = $data[$rowToProcess]['part'];
$subgroup = $data[$rowToProcess]['sub_group'];
$weekselector = $data[$rowToProcess]['filter'];
$egstatus = $data[$rowToProcess]['egstat'];
}
and the output afterwards.
You need to create another while loop while ($rows = mysql_fetch_array($r1)) { } You currently don't have this in a loop thus the repeating value.
Take this out of the loop:
$array_day=array('Mon' => 0, 'Tue' => 1, 'Wed' => 2, 'Thu' => 3, 'Fri' => 4, 'Sat' => 5);
I am not quite sure what you are asking. What do you mean it results of only one variable. Do you mean the first variable in the loop is the only one being returned correctly or on the second loop no variables are being returned correctly?

Multiple query data into single html table (PHP, Mysql) array not printing in correct position?

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.

Categories