Check Student grades using IF statement in a php while loop - php

I've two tables (examresults and gradings). I'm trying to give grades according to score value. here are my tables
examresults
ID
SUBJECT
SCORE
1
English
100
1
Math
80
1
Bios
40
gradings
GNAME
MAXSCORE
A
100
B
80
C
64
D
40
F
34
and here is what I've tried
my code
$counter = 0;
$stid=1;
if($sql=("SELECT * from `examresults` where `id`='$stid'"))
{
$stmt = $conn->prepare($sql);
// execute the query
$stmt->execute();
echo '<h3 class="text-center">Examination Results</h3>
';
echo'
<table class="table table-bordered">
<thead>
<tr style="background-color: #F2F2F2;">
<td class="text-bold">Sl No</td>
<td class="text-bold">Subject</td>
<td class="text-bold">Score</td>
<td class="text-bold">Grade</td>
</tr>
</thead>
';
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$score=$row['score'];
$check_for_grade = $conn->prepare("SELECT IF(maxscore>='$score',gname,IF(maxscore<='$score',gname,'')) FROM gradings");
$check_for_grade->execute();
$mypy = $check_for_grade->fetch();
$grade = $mypy[0];
echo'
<tbody>
<tr role="row" class="odd">
<td>'.++$Counter.'</td>
<td style="padding-left: 50px;">'.$row['subject'].'</td>
<td>'.$row['score'].'</td>
<td>'.$grade.'</td>
</tr>
';
}
}
echo'
</thead>
</tbody>
</table>
';
The problem is that, it gives me wrong info and I think problem exist in my IF statement, please any help.
Here is Output
ID
SUBJECT
SCORE
GRADE
1
English
100
A
1
Math
80
A
1
Bios
40
A
What I need is
ID
SUBJECT
SCORE
GRADE
1
English
100
A
1
Math
80
B
1
Bios
40
D

Related

Display a row with total amount of shift wise

I provide staff members to a site. They work shit wise Day and Night.
To make monthly Invoice I display the staff according to Shift wise.
Like
Day Shift
Emp Name Days Amount
A 30 1200
B 25 1000
Night Shift
C 27 1100
D 26 1050
I want to display a row with total Amount Shift Wise
I have this code
<?php
$temp='';
foreach ( $rows as $row)
{
///To Display Shift Row///////
if($temp!=$row->shift_type)
{
?>
<tr><td style="font-weight:bold"><?php echo $row->shift_type;?> Shift</td></tr>
<tr style=" border-top:3px solid #CCC">
<th style="text-align:center">Staff Name</th>
<th style="text-align:center"> Days</th>
<th style="text-align:center"> Amount</th>
</tr>
<?php $temp = $row->shift_type;
}
?>
////To Display Staff Members with Salry////
first_name.$row->last_name; ?>
<td align="center"><?php echo $row->week_days; ?></td>
<td align="center">$<?php echo $weekdaysamount= $row->week_days*$row->week_day_rate;
$totalweekdaysamount+=$weekdaysamount;?>
</td>
</tr>
<?php } ?>
This make the total of all rows . I want a row with total of Shift wise.
Should be like this
Day Shift
Emp Name Days Amount
A 30 1200
B 25 1000
Total 2200
Night Shift
C 27 1100
D 26 1050
Total 2150
<html>
<table style="width:100%">
<?php
require("stackoverflow_connect.php");
$extract=mysql_query("SELECT * FROM `employees` WHERE shift_type='Day'");
echo 'Day Shift';
echo '
<tr>
<td>Emp Name</td>
<td>Days</td>
<td>Amount</td>
</tr>
';
$total=0;
while($row=mysql_fetch_assoc($extract))
{
$total=$total+($row['week_day_rate']*$row['week_day']);
echo '
<tr>
<td>'.$row['name'].'</td>
<td>'.$row['week_day'].'</td>
<td>'.$row['week_day_rate']*$row['week_day'].'</td>
</tr>';
}
echo '<tr>
<td></td>
<td>Day Total</td>
<td>'.$total.'</td>
</tr>';
$extract_n=mysql_query("SELECT * FROM `employees` WHERE shift_type='Night'");
$total=0;
while($row_n=mysql_fetch_assoc($extract_n))
{
$total=$total+($row['week_day_rate']*$row['week_day']);
echo '
<tr>
<td>'.$row['name'].'</td>
<td>'.$row['week_day'].'</td>
<td>'.$row['week_day_rate']*$row['week_day'].'</td>
</tr>';
}
echo '<tr>
<td></td>
<td>Night Total</td>
<td>'.$total.'</td>
</tr>';
?>
</table>
<html>
You want to display the total shift wise,first order the result by the shift_type when fetching data.Then while calculating check the shift_type and if it is different from the previous record reset the value to zero.Something like below should work
$previous_shilft='';//Initally outside the foreach
if($previous_shilft!=$row->shift_type && !empty($previous_shilft)){
echo '<tr>
<td>Total</td>
<td></td>
<td>'.$totalweekdaysamount.'</td>
</tr>';//Display the row
}
if($previous_shilft!=$row->shift_type){
$totalweekdaysamount=0;
}
$totalweekdaysamount+=$weekdaysamount;
$previous_shilft=$row->shift_type;//For next loop check change the value

How to send each client ID data from Database to each different email

I m struggling to send Each ClientID Information from Database to their specific Email Address.
My training exercise was to use this SQL (See Below) that have 3 different tables that are as follow: Client, Sites, Job_Cards.
Now I have create a SQL that gives me the output that I need. But my problem is based at the fact that 1 clientID may have one or Many SiteID. one SiteID may have one and many Job_Card#.
When I create my PHP Script the Data Displays as follow: Link on a HTML page.
My Question is in 2 Parts:
How can I format the table to Display ONLY 1 ClientID or ClientName with the list of Job#, site name and Description of this single Client?
How may I send an Email to Each of the ClientName Without Duplicate ID. And What php script will be good to use to send Emails to Each Clients?
Here is my PHP Script and My SQL Statment:
$sqlSelect = "SELECT DISTINCT(cl.client_id),
client_name,
DATE(jb.date),
jb.job_number,
jb.repair,
st.site_name
FROM
job_cards jb
INNER JOIN
clients cl ON (cl.client_id = jb.client_id)
LEFT JOIN
sites st on (st.site_id = jb.site_id)
WHERE
jb.completed = 1
AND cl.client_id = jb.client_id
AND jb.date >= DATE_ADD(DATE(NOW()), INTERVAL - 30 DAY)
ORDER BY cl.client_name ASC";
//echo $sqlSelect;
$tresult = mysql_query($sqlSelect);
//die("ss");
//$dataCount = mysql_num_rows($result);
while($userData = mysql_fetch_assoc($tresult)){
if($i%2==0)
$classname = 'evenRow';
else if($i%2==1)
//extract($userData);
?>
</table>
</td>
</tr>
<tr>
<td align='center' height="30" style="font-size:14px;">
<b><?php echo $userData['client_name'];?></b>
</td>
<td align='center'></td>
<td>
<table width='100%' cellpadding= '1' border='0'>
<thead>
<tr>
<td style="font-size:13px; text-align:Left;"><b>Date</b></td>
<td style="font-size:13px; text-align:Left;"><b>Job #</b></td>
<td style="font-size:13px; text-align:left;"><b>Site name</b></td>
<td style="font-size:13px; text-align:left;"><b>Description</b></td>
</tr>
</thead>
<tr class='<?php if(isset($classname)) echo $classname;?>'>
<tr>
<td>
<?php echo mysql_real_escape_string ($userData['DATE(jb.date)']); ?>
</td>
<td>
<?php echo mysql_real_escape_string($userData['job_number']);?>
</td>
<td>
<?php echo mysql_real_escape_string($userData['site_name']);?>
</td>
<td>
<?php echo mysql_real_escape_string($userData['repair']);?>
</td>
</tr>
<?php $i++;
}
?>
</tr>
</table>
<table width='100%' cellpadding= '1' border='1'>
<tr>
</tr>
</table>
Please May Some 1 Help me. I tried What I could But Still I cannot send emails and the Table format doesn't display the way I want.
Thank.

Display each color separate with values

I am having trouble in printing values from database.
ITEM TABLE
ITEM | COLOR | MATERIAL | DIMENSIONS | CATEGORY | QUANTITY
- 01 33 05 111 12 1000.00
- 02 33 07 125 18 200.00
- 03 33 11 156 18 254.00
- 04 56 15 25 66 113.00
- 05 66 05 11 33 521.00
I am trying to print values in table(for each color print material dimension category)
So the output will be:
COLOR - > 33
MATERIAL | DIMENSION | CATEGORY | QUANTITY
05 111 12 1000.00
07 125 18 200.00
11 156 18 254.00
COLOR - > 56
MATERIAL | DIMENSION | CATEGORY | QUANTITY
15 25 66 113.00
COLOR - > 66
MATERIAL | DIMENSION | CATEGORY | QUANTITY
05 11 33 521.00
I am using query
$query = "SELECT a.itemnb, b.colorname, c.materialname, d.categoryname, sum(a.quantity) as quantity
FROM dbo_items a
JOIN dbo_color b
ON a.color=b.colorid
JOIN dbo_material c
on a.material=c.material
JOIN dbo_category
on a.category=d.categoryid
GROUP BY b.colorname, c.materialname, d.categoryname, ";
I am using PDO.
$q=$conn->query($query);
Now I can fetch all values in table, but that is not actually I want to make.
<table class="table table-bordered table-striped">
<thead>
<tr class="bg-primary">
<td data-field="color">COLOR</td>
<td data-field="material">MATERIAL</td>
<td data-field="dim">DIMENSIONS</td>
<td data-field="quantity">QUANTITY</td>
</tr>
</thead>
<tbody>
<?php while ($r = $m->fetch()){?>
<tr>
<td><?=$r['colorname']?></td>
<td><?=$r['materialname']?></td>
<td><?=$r['categoryname']?></td>
<td><?=$r['quantity ']?></td>
<?php } ?>
</tbody>
</table>
I want to print first color and then all material related to that color.
I am having trouble there, any help or advice is appreciated?
First remove the colorname field from group by clause in query and add this column with order by means add order by colorname in query.
And then change from HTML and php code with the following:
<table class="table table-bordered table-striped">
<thead>
<tr class="bg-primary">
<td data-field="color">COLOR</td>
<td data-field="material">MATERIAL</td>
<td data-field="dim">DIMENSIONS</td>
<td data-field="quantity">QUANTITY</td>
</tr>
</thead>
<tbody>
<?php
$tempColor = '';
while ($r = $m->fetch()){
if($tempColor != $r['colorname']) {
?>
<tr><td colspan="4">Color Name: <?=$r['colorname']?></td></tr>
<?php $tempColor = $r['colorname'];
} else {
?>
<tr>
<td><?=$r['colorname']?></td>
<td><?=$r['materialname']?></td>
<td><?=$r['categoryname']?></td>
<td><?=$r['quantity ']?></td>
<?php }
}
?>
</tbody>
</table>
You have to ORDER BY 'COLOR' and not to GROUP BY 'COLOR'
After that yout test if you are switching to a new color
<?php
<table class="table table-bordered table-striped">
<thead>
<tr class="bg-primary">
<td data-field="color">COLOR</td>
<td data-field="material">MATERIAL</td>
<td data-field="dim">DIMENSIONS</td>
<td data-field="quantity">QUANTITY</td>
</tr>
</thead>
<tbody>
<?php
$prevColor = '';
while ($r = $m->fetch()){?>
<tr>
<td><? print ($prevColor == $r['colorname'] ? '' : $r['colorname']) ?></td>
<td><? print $r['materialname']?></td>
<td><? print $r['categoryname']?></td>
<td><? print $r['quantity ']?></td>
<?php
$prevColor = $r['colorname'];}
?>
</tbody>
</table>
UPDATE
If you can not change your query, so you have to order by COLOR your resultset
Check if the current colorname is equal to the previous one:
<?php
$currcolor=array();
$i=0;
while ($r = $m->fetch()){
$currcolor[$i]=$r['colorname']; ?>
<tr>
<td>
<?php if($currcolor[$i] != $currcolor[$i-1]){
echo $currcolor[$i];
} ?>
</td>
<td><?php echo $r['materialname']; ?></td>
<td><?php echo $r['categoryname']; ?></td>
<td><?php echo $r['quantity ']; ?></td>
<?php
$i++;
} ?>
The easiest approach is to make one query for getting all colors, then you can make loop for result from first query and use another query for getting all information related to specific color.
Example:
$q = 'SELECT * FROM dbo_color';
$q=$conn->query($query);
while ($res = $q->fetch()){
$secondQ = 'SELECT * FROM relatedMaterials WHERE color = ' . $res->color;
}
And include in this query all html you need for creating table.
I hope this will help you.

Is it possible to <br> every 10 table rows in CSS?

My script basically loads data from my database and I have to <br> somewhere but it wont work because its a huge table. So I need to format this table so every 10 rows there is a <br>.
<table align="center" cellspacing="0" cellpadding="0">
<?php
$date = mysql_query("SELECT DISTINCT `date` FROM `matches`");
while($daterow = mysql_fetch_assoc($date)){
echo ' <tr>
<td style="width:60px; background-color:#000000;"><font color="#FFFFFF" style="font-size:11px">'.$daterow['date'].'</font></td>
</tr>
';
$query = mysql_query("SELECT hour, team1, team2, goalsteam1, goalsteam2, competition FROM `matches` WHERE `date`='". $daterow['date'] ."'");
while($row = mysql_fetch_array($query)){
$teamtest = mysql_query("SELECT teamname FROM `teams` WHERE `team`='".$row['team1']."'");
$teamtestrow = mysql_fetch_assoc($teamtest);
$hour = substr($row['hour'],0,5);
echo '
<tr class="teamtable">
<td style="width:60px; font-size:11px;">'.$hour.'</td>
<td style="width:145px; font-size:11px;">'.$teamrow['teamtest'].'</td>
<td style="width:15px; font-size:11px;">'.$row['goalsteam1'].'</td>
<td style="width:15px; font-size:11px;">'.$row['goalsteam2'].'</td>
<td style="width:145px; font-size:11px;">'.$row['team2'].'</td>
<td style="width:120; font-size:11px;">'.$row['competition'].'</td>
</tr>';
}
}
?>
</table>
Or just <br> before every date. When I tried it will just leave a huge blank space up but still no space between the date and matches.
Not too sure if CSS can handle this. And unclear on where you expect a break to occur based on the code you are sharing. But in general you would use the modulus operator in PHP would be a good start. Knowing that maybe this would work for you:
<table align="center" cellspacing="0" cellpadding="0">
<?php
$date = mysql_query("SELECT DISTINCT `date` FROM `matches`");
$counter = 0;
while($daterow = mysql_fetch_assoc($date)){
echo ' <tr>
<td style="width:60px; background-color:#000000;"><font color="#FFFFFF" style="font-size:11px">'.$daterow['date'].'</font></td>
</tr>
';
$query = mysql_query("SELECT hour, team1, team2, goalsteam1, goalsteam2, competition FROM `matches` WHERE `date`='". $daterow['date'] ."'");
while($row = mysql_fetch_array($query)){
$teamtest = mysql_query("SELECT teamname FROM `teams` WHERE `team`='".$row['team1']."'");
$teamtestrow = mysql_fetch_assoc($teamtest);
$hour = substr($row['hour'],0,5);
echo '
<tr class="teamtable">
<td style="width:60px; font-size:11px;">'.$hour.'</td>
<td style="width:145px; font-size:11px;">'.$teamrow['teamtest'].'</td>
<td style="width:15px; font-size:11px;">'.$row['goalsteam1'].'</td>
<td style="width:15px; font-size:11px;">'.$row['goalsteam2'].'</td>
<td style="width:145px; font-size:11px;">'.$row['team2'].'</td>
<td style="width:120; font-size:11px;">'.$row['competition'].'</td>
</tr>';
$counter++;
// Do a modulus check.
if ($counter % 10 == 0) {
echo "<tr><td colspan="6"> </td></tr>";
}
}
}
?>
</table>
The key chunk of code is this:
$counter++;
// Do a modulus check.
if ($counter % 10 == 0) {
echo "<tr><td colspan="6"> </td></tr>";
}
On each loop $counter is incremented by 1. And using the modulus operator in the if() statement we check for every 10th item & then do something. In this case insert a row with an in it.

php pagination with date and not serial number

I have database with 80 rows currently and it increases day by day as i am conducting online lectures for doctors on every tuesday and saturday. For Members of my site, recording of these lectures are available.
My database contains columns like lecture_start_date, lecture_end_date, display_time (+45 minutes to lecture end date-time)...
currently 80 lectures recording links are available on single page. so i created pagination for 10 lectures each. code is as follows..
$sql = "SELECT COUNT(lecture_start) FROM es_lecture_master";
$rs_result = mysql_query($sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 10);
for ($i=1; $i<=$total_pages; $i++) {
echo "<button><a class=\"page\" href='lec_history_p.php?page=".$i."'>".$i." </a></button>";
};
And
$current_date=date("Y-m-d H:i:s");
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 10;
$sql = "SELECT * FROM es_lecture_master where display_time <'".$current_date."' ORDER BY `lecture_start` DESC limit $start_from, 10";
$rs_result = mysql_query ($sql);
$ss=mysql_num_rows($rs_result);
$counts= sizeof($arrayreversed);
while($data=mysql_fetch_assoc($rs_result)) {
<div align="left">Lecture No :<div class="numberCircle" style="display:inline;"> <? echo $ss;?></div></div>
<table width="80%" style="text-align:left;" align="center" border="0" cellspacing="4" cellpadding="4" class="tabl">
<tr>
<td width="32%">E-Sambhasha Subject : </td>
<td ><?=stripslashes($data["lecture_subject"])?></td>
</tr>
<tr>
<td width="32%">Lecturer :</td>
<td><?=$data["lecturer_name"]?></td>
</tr>
<tr>
<td width="32%">IST Day & Date :</td>
<td><?=date("l ",strtotime($data['lecture_start']))?>,<?=date("d M Y",strtotime($data['lecture_start']));?>
</td>
</tr>
<tr>
<td width="32%" style="background-color:#d44302; color:#f4f4af;">IST Timing :</td>
<td><?=date("h:i:s A ",strtotime($data['lecture_start']))?> - <?=date("h:i:s A ",strtotime($data['lecture_end']))?></td>
</tr>
<tr>
<td width="32%">Session Recording :</td>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="tabl"><tr>
<td width="26%">
<button data-fancybox-href="<?php echo $data['recording_link'];?>" class="fancy fancybox.iframe more_info_btn" >Play Recording</button> </td>
</tr></table>
</td>
</tr>
</table>
</div>
With This Code I am getting result as following image.
But I don't want to show serial numbers for pagination but want to show date range of lectures conducted for pagination...e.g. button with label ="From 13 March 2014 To 7 Jan 2014" (here 13 march will be lecture start date of Lecture shown on Top on page 1) instead of Button "1", button with label = "From 06 Jan 2014 To 12 October 2014" Instead of Button with label "2"?
(database contains lecture_start_date and lecture_end_date columns)
Also in Lecture No., on every page it shows Lecture No. 10 to 1..instead of that how to show like on page 1 Lecture 80 to 70, on page 2 - lecture no. 70 to 60 and so on.. ?
see image -

Categories