PHP issue: using if statement - php

Hello I have a problem in one of my php projects. The problem states that I need to provide the top students for each year of classes as well as the best overall student. However i am having trouble with the if statments; I am unable to post the name of each student where the name should be it displays their score for the year.
pic of database:
http://imagizer.imageshack.us/a/img843/9674/7r2t.png
here is my code:
<body>
<form>
<?php
$username = "amar";
$password = "amar";
$hostname = "localhost";
$database = "study";
$set = 100;
$met = 0;
$mysqli = new mysqli($hostname, $username, $password, $database)
or die("Unable to connect to MySQL");
$query = "select Name, year_1, year_2, year_3, year_4, Final, Final_Grade from toppers";
$result = mysqli_query($mysqli, $query);
if (!$result) {
$message = 'Invalid query: ' . mysqli_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
echo "<table border=1 cellpadding=5>";
echo "<tr><td>Student Name</td>";
echo "<td>2011</td>";
echo "<td>2012</td>";
echo "<td>2013</td>";
echo "<td>2014</td>";
echo "<td>Final Exam</td>";
echo "<td>Grade</td></tr>";
while ($row = mysqli_fetch_array($result)) {
$grader = "$_POST[result]";
$x = $row['year_1'] + $row['year_2'] + $row['year_3'] + $row['year_4'] + $row['Final'];
$grader = $x / 5;
$row[Final_Grade] = $grader;
if ($grader <= 100 and $grader >= 89) {
$grade = "A";
} elseif ($grader <= 90 and $grader >= 79) {
$grade = "B";
} elseif ($grader <= 80 and $grader >= 69) {
$grade = "C";
} elseif ($grader <= 70 and $grader >= 59) {
$grade = "D";
} else {
$grade = "F";
}
if ('$row[Final]' > $set) {
$year1 = $grade;
$year2011 = $row;
}
if ('$row[Final]' < $set) {
$year2 = $grade;
$year2012 = $row;
}
if ('$row[Final]' < $set) {
$year3 = $grade;
$year2013 = $row;
}
if ('$row[Final]' < $set) {
$year4 = $grade;
$year2014 = $row;
}
if ('$row[Final]' < $set) {
$final = $grade;
$finalscore = $row;
}
if ('$row[Final]' < $set) {
$overall = $grade;
$overall = $row;
}
echo "<tr><td>";
echo "$row[Name]";
echo "</td><td>";
echo "$row[year_1]";
echo "</td><td>";
echo "$row[year_2]";
echo "</td><td>";
echo "$row[year_3]";
echo "</td><td>";
echo "$row[year_4]";
echo "</td><td>";
echo "$row[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
}
echo "<tr><td colspan=7>Topper for 2011: </td></tr>";
echo "$year2011[Name]";
echo "</td><td>";
echo "$year2011[year_1]";
echo "</td><td>";
echo "$year2011[year_2]";
echo "</td><td>";
echo "$year2011[year_3]";
echo "</td><td>";
echo "$year2011[year_4]";
echo "</td><td>";
echo "$year2011[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "<tr><td colspan=7>Topper for 2012: </td></tr>";
echo "$year2012[Name]";
echo "</td><td>";
echo "$year2012[year_1]";
echo "</td><td>";
echo "$year2012[year_2]";
echo "</td><td>";
echo "$year2012[year_3]";
echo "</td><td>";
echo "$year2012[year_4]";
echo "</td><td>";
echo "$year2012[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "<tr><td colspan=7>Topper for 2013: </td></tr>";
echo "$year2013[Name]";
echo "</td><td>";
echo "$year2013[year_1]";
echo "</td><td>";
echo "$year2013[year_2]";
echo "</td><td>";
echo "$year2013[year_3]";
echo "</td><td>";
echo "$year2013[year_4]";
echo "</td><td>";
echo "$year2013[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "<tr><td colspan=7>Topper for 2014: </td></tr>";
echo "$finalscore[Name]";
echo "</td><td>";
echo "$finalscore[year_1]";
echo "</td><td>";
echo "$finalscore[year_2]";
echo "</td><td>";
echo "$finalscore[year_3]";
echo "</td><td>";
echo "$finalscore[year_4]";
echo "</td><td>";
echo "$finalscore[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "<tr><td colspan=7>Topper overall: </td></tr>";
echo "$overall[Name]";
echo "</td><td>";
echo "$overall[year_1]";
echo "</td><td>";
echo "$overall[year_2]";
echo "</td><td>";
echo "$overall[year_3]";
echo "</td><td>";
echo "$overall[year_4]";
echo "</td><td>";
echo "$overall[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "</table>";

Everything in single quotes in PHP gets interpreted literally as a string. When you do '$row[Final]' PHP interprets that as "dollar sign, open bracket, the word 'Final', close bracket."
In order to have PHP interpret what you're saying as a reference to a variable, you have to reference only your index as a string, leaving the rest outside your quotes.
$row["Final"]; tells PHP, "check the variable $row and see what's stored at it's index represented by the string Final."
PHP: Strings - Manual

It looks like you need to store the highest result for each group you want to display later (ie, highest for each year), rather than just one single highest store in $set.
Not 100% sure on what you want, but to give you an idea something like this:-
<body>
<form>
<?php
$username = "amar";
$password = "amar";
$hostname = "localhost";
$database = "study";
$set = array('2011'=>0, '2012'=>0, '2013'=>0, '2014'=>0, 'final'=>0, 'Final_Grade'=>0);
$mysqli = new mysqli($hostname, $username, $password, $database)
or die("Unable to connect to MySQL");
$query = "select Name, year_1, year_2, year_3, year_4, Final, Final_Grade from toppers";
$result = mysqli_query($mysqli, $query);
if (!$result) {
$message = 'Invalid query: ' . mysqli_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
echo "<table border=1 cellpadding=5>";
echo "<tr><td>Student Name</td>";
echo "<td>2011</td>";
echo "<td>2012</td>";
echo "<td>2013</td>";
echo "<td>2014</td>";
echo "<td>Final Exam</td>";
echo "<td>Grade</td></tr>";
while ($row = mysqli_fetch_array($result))
{
$grader = $_POST['result'];
$x = $row['year_1'] + $row['year_2'] + $row['year_3'] + $row['year_4'] + $row['Final'];
$grader = $x / 5;
$row['Final_Grade'] = $grader;
switch (true)
{
case ($grader <= 100 and $grader >= 89) :
$grade = "A";
break;
case ($grader <= 90 and $grader >= 79) :
$grade = "B";
break;
case ($grader <= 80 and $grader >= 69) :
$grade = "C";
break;
case ($grader <= 70 and $grader >= 59) :
$grade = "D";
break;
default :
$grade = "F";
break;
}
if ($row['Final'] > $set['2011'])
{
$year2011 = $row;
$set['2011'] = $row['Final'];
}
if ($row['Final'] < $set['2012'])
{
$year2011 = $row;
$set['2011'] = $row['Final'];
}
if ($row['Final'] < $set['2013'])
{
$year2011 = $row;
$set['2011'] = $row['Final'];
}
if ($row['Final'] < $set['2014'])
{
$year2011 = $row;
$set['2011'] = $row['Final'];
}
if ($row['Final'] < $set['final'])
{
$finalscore = $row;
$set['final'] = $row['Final'];
}
if ($row['Final_Grade'] < $set['Final_Grade'])
{
$overall = $row;
$set['Final_Grade'] = $row['Final_Grade'];
}
echo "<tr><td>";
echo "$row[Name]";
echo "</td><td>";
echo "$row[year_1]";
echo "</td><td>";
echo "$row[year_2]";
echo "</td><td>";
echo "$row[year_3]";
echo "</td><td>";
echo "$row[year_4]";
echo "</td><td>";
echo "$row[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
}
echo "<tr><td colspan=7>Topper for 2011: </td></tr>";
echo "<tr><td>".implode("</td><td>", $year2011)."</td></tr>";
echo "<tr><td colspan=7>Topper for 2012: </td></tr>";
echo "<tr><td>".implode("</td><td>", $year2012)."</td></tr>";
echo "<tr><td colspan=7>Topper for 2013: </td></tr>";
echo "<tr><td>".implode("</td><td>", $year2013)."</td></tr>";
echo "<tr><td colspan=7>Topper for 2014: </td></tr>";
echo "<tr><td>".implode("</td><td>", $year2014)."</td></tr>";
echo "<tr><td colspan=7>Topper overall: </td></tr>";
echo "<tr><td>".implode("</td><td>", $overall)."</td></tr>";
echo "</table>";

Related

PHP: How can I replace specific data?

I need a little script so when I get my data output from my Mysql table and the data I receive is -1 then it should display Expired if it is anything other than -1 it should just display the data.
This is what I currently have. I have tried some stuff but my PHP and MySQL is 0.
$sql = "SELECT ban_id, timestamp, perp_steamid, perp_name, admin_steamid, admin_name, bantime, timeleft, reason FROM nomercyg_CTBan_Log";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>". $row["ban_id"]. "</td><td>". date('m/d/Y h:i:s', $row["timestamp"]). "</td><td>". $row["perp_name"]. "</td><td>". $row["admin_name"]."</td><td>". $row["bantime"]." min". "</td><td>". $row["timeleft"]." min"."</td><td>". $row["reason"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
If I understood you right, you can just add an if statement inside your while loop to decide if you should display 'Expired' or the data you caught itself.
while($row = $result->fetch_assoc()) {
if($row['band_id] == -1) {
echo '<tr><td>Expired</td></tr>; //Or add as many columns as you need here
} else {
echo "<tr><td>". $row["ban_id"]. "</td><td>". date('m/d/Y h:i:s', $row["timestamp"]). "</td><td>". $row["perp_name"]. "</td><td>". $row["admin_name"]."</td><td>". $row["bantime"]." min". "</td><td>". $row["timeleft"]." min"."</td><td>". $row["reason"]. "</td></tr>";
}
}
Maybe like this way but i think its not the best solution
$sql = "SELECT ban_id, timestamp, perp_steamid, perp_name, admin_steamid, admin_name, bantime, timeleft, reason FROM nomercyg_CTBan_Log";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//echo . . . . . . . .. .. . ... . ;
echo "<tr><td>";
if ($row["ban_id"] <=0){
$row["ban_id"] = 'EXPIRED!!!';
echo $row["ban_id"];
}else {
echo $row["ban_id"];
}
echo "</td><td>";
if(date('m/d/Y h:i:s', $row["timestamp"]) <=0){
$row["ban_id"] = 'EXPIRED!!!';
echo date('m/d/Y h:i:s', $row["timestamp"]);
}else {
echo date('m/d/Y h:i:s', $row["timestamp"]);
}
echo "</td><td>";
if ($row["perp_name"]<=0){
$row["perp_name"] = 'EXPIRED!!!';
echo $row["perp_name"];
}else{
echo $row["perp_name"];
}
echo "</td><td>";
if ($row["admin_name"]<=0){
$row["admin_name"] = 'EXPIRED!!!';
echo $row["admin_name"];
}else{
echo $row["admin_name"];
}
echo "</td><td>";
if ($row["bantime"]<=0){
$row["bantime"] = 'EXPIRED!!!';
echo $row["bantime"];
}else{
echo $row["bantime"];
}
echo " min";
echo "</td><td>";
if ($row["timeleft"] <=0){
$row["timeleft"] = 'EXPIRED!!!';
echo $row["timeleft"];
}else{
echo $row["timeleft"];
}
echo " min";
echo "</td><td>";
if ($row["reason"]<=0){
$row["reason"] = 'EXPIRED!!!';
echo $row["reason"];
}else{
echo $row["reason"];
}
echo "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}

PHP - Adding a separator in a Loop

I asked this question yesterday but no one replied after I tried to get further help. I've been trying to figure out the problem in the code I was given in my previous post but it's not giving me the results I want. Right now what it does is it writes a separator after each row but what I really want is if the name of the manager from the previous row is different then write a separator and keep going on with loop.
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($rows['Manager'] !== $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
}
You're mixing $row and $rows, you would be okay doing this:
$team = "";
while ($row = mysqli_fetch_array($results)) {
if($team && ($row['Manager'] != $team)){ // skip first separator
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
$team = $row['Manager'];
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
Am I missing something, or can you simply display the data, then assign it to the variable further in the loop?
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($row['Manager'] !== $team){
echo "<tr><td colspan=\"3\">".$team."</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
$team = $row[0]['Manager'];
}
}
What is $rowS['Manager'] ? $rowS is unknown. use $row['Manager'] like:
if($row['Manager'] !== $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($rows['Manager'] !== $team) {
$team = $row['Manager'];
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
}
Just move the value allocation $team = $rows['Manager']; in the if condition.
while ($row = mysqli_fetch_array($results)) {
$team = "";
foreach($row as $rows){
if($rows['Manager'] != $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
$team = $rows['Manager'];
}
echo "<tr>";
echo "<td>".$rows['Manager']."</td>";
echo "<td>".$rows['Employee_ID']."</td>";
echo "<td>".$rows['Name']."</td>";
echo "</tr>";
}
}

Displayind data in a codeigniter view using a table

Hi am having problem in sorting the correct <td> to display subject results for students. i have 11 subjects offered in the school and senior students take 8 subjects because 4 are electives at this level. so when fetching results for the senior students and displaying when the student does not take that subject it still echoes the result in the wrong field.my code below does not distinguish e.g if the <td> is physics or biology.
i want it to echo a (-) where a student does not take the subject.thanks in advance
<table border='1'>
<tr><th>Position</th><th>Students</th><th>English</th><th>Kiswahili</th><th>Maths</th><th>Biology</th><th>Physics</th><th>Chemistry</th><th>History</th><th>Geography</th><th>CRE</th><th>Agriculture</th><th>Business</th><th>Total Marks</th><th>Mean Grade</th><th>Aggregate Points</th><th>Action</th>
</tr>
<?php
//loop to display the names of students
$i = 1;
foreach ($overallresults as $result) {
echo "<tr>";
echo "<td>";
echo $i++;
echo "</td>";
$admNo = $result->admNo;
$total_marks = $result->total_marks;
$agp = $result->aggregate_points;
$mean_grade = $result->mean_grade;
$agp = $result->aggregate_points;
$result_id = $result->result_id;
$fname = "";
foreach ($students as $student) {
// print_r($student);
$admNo1 = $student->admNo;
if ($admNo == $admNo1) {
// print_r($student);
$fname = $student->firstName;
$mname = $student->middleName;
$lname = $student->lastName;
//}
// }
//echo "<tr>";
echo "<td>";
echo $fname . " " . $mname . " " . $lname;
echo "</td>";
}
}
foreach ($subjectresults as $subresult) {
// print_r($result);
$score = "0";
$admNo3 = $subresult->admNo;
$subCode = $subresult->subCode;
$score = $subresult->score;
if ($admNo == $admNo3) {
if ($subCode == '232') {
$score = $score;
}
if ($subCode == '101') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '102') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '121') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '231') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '232') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '233') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '311') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '312') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '313') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '443') {
echo "<td>";
if (!$score) {
echo 0;
} else {
echo $score;
}
echo "</td>";
}
if ($subCode == '565') {
echo "<td>";
echo $score;
echo "</td>";
}
}
}
?>
<?php
if (isset($term)) {
$term = $term;
}
if (isset($form)) {
$form = $form;
}
if (isset($year)) {
$year = $year;
}
if (isset($examCategory)) {
$examCategory = $examCategory;
}
//if ($admNo == $admNo1) {
// print_r($student);
//}
// }
echo "<td>";
echo $total_marks;
echo "</td>";
echo "<td>";
echo $mean_grade;
echo "</td>";
echo "<td>";
echo $agp;
echo "</td>";
echo "<td>";
echo "</td>";
//}
}
?>
</table>
<?php
}
?>
</div>
the above code works but displays the subject done by students in the wrong table data field.the subjects are identified using subject codes like english is 101 while kiswahili is 102,maths is 121
I can see that the subjects are static. I would rather you have a data structure for Results, with proper relation's based on student id, course id. then you can loop all the Results and fetch the relevant subjects names and student names. eg;
<?php
$results = array();
$subjects = array(101=>"English", 102=>"Kiswahili",103=>"Physics",104=>"Chemistry");
$students = array("tom","dick","ally");
$result1 = array(
'studentId'=>1,
'score'=>array(
101=>20,
102=>30,
103=>30,
104=>45
),);
$result2 = array(
'studentId'=>2,
'score'=>array(
101=>34,
102=>54,
103=>77
),);
$results[] = $result1;
$results[] = $result2;
echo "<table border='1'>";
echo "<tr>";
echo "<th>#</th><th>Student</th>";
for($i = 101; $i < 105; $i++){
echo "<th>".$subjects[$i]."</th>";
}
echo "</tr>";
$count = 1;
foreach($results as $result){
echo "<tr>";
echo "<td>".$count."</td>";
echo "<td>".$students[$result['studentId']]."</td>";
foreach($subjects as $key => $value){
$marks = $result['score'][$key]!=null?$result['score'][$key]:'-';
echo "<td>".$marks."</td>";
}
echo "</tr>";
$count++;
}
echo "</table>";
?>
Ofcourse you will have to write Helper functions for fetching student names and calculating the mean scores.

how to select two columns from two tables in mysql

I am trying to execute this query but i got error " Undefined index:
lname".I want to count row from one column(fname) from table a and
select column(lname) from other table b. so please help me.
$result = mysql_query("SELECT COUNT(fname),lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
If you still get an error you can try to fetch both separately:
$result = mysql_query("SELECT COUNT(fname) FROM a");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
$result1 = mysql_query("SELECT lname FROM b");
while ($row = mysql_fetch_array($result1))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td></tr>";
}
You need to use an alias. Use this:
$result = mysql_query("SELECT COUNT(fname) AS countfname,lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['countfname'];
echo "</td></tr>";
}
Try this code:
$result = mysql_query("SELECT COUNT(a.fname) as fname,b.lname as lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}

Php undefine variable issue

I've a html form which is insert data to mysql database and then get those data with following php code (From supplier_jv table)
<?php
include("include/address2.php");
include("include/menu.php");
$uname_ad = $_SESSION['uname_ad'];
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM supplier_jv WHERE jv_id = '$id'");
$num = mysql_num_rows($sql);
if($num == 0)
{
echo "<p><font color=red>Accounts is emtpy</font></p>";
}
else
{
$re_name = mysql_fetch_array($sql);
echo "<center><h2>";
echo "<strong>Accounts of </strong>";
echo $re_name['jv_name'];
echo "</h2></center>";
echo "<center>";
echo "<table>";
echo "<table border='0' cellpadding='5' cellspacing='5' width='1000'>";
echo "<tr/>";
echo "<td><strong>Date</strong></td>";
echo "<td><strong>Particular</strong></td>";
echo "<td><strong>Folio(C)</strong></td>";
echo "<td><strong>Folio(J)</strong></td>";
echo "<td><strong>Debit</strong></td>";
echo "<td><strong>Credit</strong></td>";
echo "<td><strong>Balance</strong></td>";
echo "</tr>";
while($re= mysql_fetch_array($sql))
{
$day = $re['day'];
$month $re['month'];
$year = $re['year'];
$parti = $re['particulars'];
$folio = $re['folio'];
$folio2 = $re['folio2'];
$debit = $re['debit'];
$credit = $re['credit'];
$balance = $re['balance'];
$b = $debit - $credit;
$total_debit = mysql_query("SELECT SUM(debit) FROM supplier_jv");
$re_t = mysql_fetch_array($total_debit);
$t_d = $re_t['SUM(debit)'];
$total_credit = mysql_query("SELECT SUM(credit) FROM supplier_jv");
$re_t2 = mysql_fetch_array($total_credit);
$t_c = $re_t2['SUM(credit)'];
$b = $t_d - $t_c;
echo "<tr>";
echo "<td>$day/$month/$year</td>";
echo "<td>$parti</td>";
echo "<td>$folio</td>";
echo "<td>$folio2</td>";
echo "<td>";
echo number_format($debit);
echo "</td>";
echo "<td>";
echo number_format($credit);
echo "</td>";
echo "<td></td>";
echo "</tr>";
}
echo "<tr bgcolor='#f3f3f3'>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td><strong>Total Balance-</strong></td>;
echo "<td><strong>";
echo number_format($b);
echo "</strong></td>";
echo "</tr>";
echo "</table>";
echo "</center>";
}
?>
Well, After insert data then it's show:
Notice: Undefined variable: b in E:\xampp\htdocs\Accounts\admin\content
\supplier_account.php on line 108
But if i insert data in second time then it's OK!!.
Any idea or solution.
Thanks
shibbir.
You need to use isset to see if it is set, not null and also avoid the Notice: Undefined variable error:
if (isset($b))
{
// your code here
}
Where you have:
$b = $t_d - $t_c;
Make sure that there is some value coming up for $t_d and $t_c

Categories