So, I have a PHP Script that would insert an attendance of the student. First it should check if the attendance with the same student ID and date already exist. If not, it inserts the attendance. I want to insert in the attendance status whether the student is present or late based on my php function compareTime's return value. But the problem is, the function wont return anything. That's why in the attendance data the status column is blank.
function checkAttendance($conn, $stud_id) {
$check = mysqli_query($conn, "SELECT * FROM tbl_attendance WHERE student_id = '$stud_id' AND date = CURDATE()") or die (mysqli_error());
if(mysqli_num_rows($check) > 0) {
return true;
}
else {
return false;
}
}
function compareTime($time,$grade) {
$ctime = strtotime($time);
if($grade == "H1" || $grade == "H2" || $grade == "H3" || $grade == "H4" || $grade == "S1" || $grade == "S2") {
if ($ctime > strtotime('05:00:00') && $ctime < strtotime('07:30:00')) return "P";
else if ($ctime > strtotime('07:30:00') && $ctime < strtotime('8:30:00')) return "L";
}
else {
if($ctime > strtotime('05:00:00') && $ctime < strtotime('07:15:00'))
return "P";
else if ($ctime > strtotime('07:15:00') && $ctime < strtotime('8:30:00'))
return "L";
}
}
function recordAttendance($conn, $sid, $glid, $scid) {
$sql = mysqli_query($conn, "INSERT INTO tbl_attendance(student_id, gradeLevel_id, section_id, date, arrival_time, status) VALUES ('".$sid."', '".$glid."', '".$scid."', CURDATE(), CURTIME(), '".compareTime(date('H:i:s'),$glid)."')") or die(mysqli_error());
}
$res = mysqli_query($connect, "SELECT * FROM tbl_student WHERE card_id = '$cardID'")or die(mysqli_error());
while($row = mysqli_fetch_array($res,MYSQLI_ASSOC)) {
if (checkAttendance($connect, $row['student_id']))
echo "Already Exist!";
else
recordAttendance($connect, $row['student_id'], $row['gradeLevel_id'], $row['section_id']);
}
It could have several reasons actually, but you'll need to debug to see which is the case.
First start debugging by outputting the values of compareTime to see if they actually work at all. If it doesn't, fix that first.
If that works you could try adding a 'hardcoded' value to your query to see if that gets inserted into your (test) data/table. If that doesn't produce a good result either it might be a case of wrong type in the database. Maybe it's expecting an integer or something else?
You could also extract the function call from the query and put it in a variable, it will provide a bit cleaner and more readable code, as well as it being easier to debug.
p.s.
Did you realise that, in the unlikely event of a time being exactly 7:30:00 or 7:15:00, a person falls between the cracks because you only check for greater or smaller than. Some should also be >= or <=
Might be better to rewrite it to something like this:
function compareTime($grade,$time = ''){
$time = (empty($time))?strtotime(date('H:i:s')):strtotime($time);
$time_5 = strtotime('05:00:00');
$time_715 = strtotime('07:15:00');
$time_730 = strtotime('07:30:00');
$time_830 = strtotime('08:30:00');
if ($grade == "H1" || $grade == "H2" || $grade == "H3" || $grade == "H4" || $grade == "S1" || $grade == "S2") {
if (
$time > $time_730
&& $time < $time_830
){
return "L";
} elseif (
$time > $time_5
&& $time <= $time_730
){
return "P";
}
} else {
if (
$time > $time_715
&& $time < $time_830
){
return "L";
} elseif (
$time > $time_5
&& $time <= $time_715
){
return "P";
}
}
}
Related
I have a for loop to cycle through and array, run a database query in relation to each element, then call a function that prints out something in relation to it. The array is 12 elements long but the for loop never gets past element 0. It doesn't error or fail it just doesn't do anything after the first element. I verified that by putting the echo $x; and echo $vendorsname[$x]; at the start of each loop cycle and sure enough it only ever echo's 0 out to the page.
$continuetill = count($vendorsname);
for ($x = 0; $x < $continuetill; $x++)
{
echo $x;
echo $vendorsname[$x];
$sql="SELECT low,mid,high,verlow,vermin,verhigh FROM vendors WHERE vendor = ".$x." ORDER BY id DESC LIMIT 1";
if ($result=mysqli_query($conn,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$low = $row[0];
$mid = $row[1];
$high = $row[2];
$verlow = $row[3];
$vermid = $row[4];
$verhigh = $row[5];
if(($low > $mid) && ($low > $high))
{
likely295Message($vendorsname[$x]);
}
elseif (($high > $low) && ($high > $mid) && ($high < 15))
{
possibly300Message($vendorsname[$x]);
}
elseif (($high > $low) && ($high > $mid) && ($high >= 15))
{
likely300Message($vendorsname[$x]);
}
elseif (($mid > $low) && ($mid > $high))
{
likely296Message($vendorsname[$x]);
}else
{
unknownMessage($vendorsname[$x]);
}
if(($verlow != 0) || ($vermid != 0) || ($verhigh != 0))
{
if(($verlow > $vermid) && ($verlow > $verhigh))
{
verified295Message($vendorsname[$x]);
changeBackgroundBack($vendorsname[$x]);
changeImage($vendorsname[$x]);
}
elseif (($verhigh > $verlow) && ($verhigh > $vermid))
{
verified300($vendorsname[$x]);
changeBackground($vendorsname[$x]);
changeImage($vendorsname[$x]);
}
elseif (($vermid > $verlow) && ($vermid > $verhigh))
{
verified296($vendorsname[$x]);
changeBackgroundBack($vendorsname[$x]);
changeImage($vendorsname[$x]);
}
}
}
mysqli_free_result($result);
}
}
Make sure you have error displaying turned on. Add at the beginning of your script:
ini_set('display_errors', 1);
to make sure you don't have any errors.
I want to check different conditions into if statement based on different scenario (Will get the $status value as 'Y' or 'N'). Please check the code below and let me know the issue here.
$fstrto = "10";
$cstrto = "7";
if($status == 'N')
{
$cond = "$fstrto <= $cstrto";
}
else
{
$cond = "$fstrto >= $cstrto";
}
if($cond)
{
echo "Success";
}
This code is not working as it takes the "$fstrto <= $cstrto" as variable.
Remove the quotes. Use intval/doubleval if the input is a string as in $fstrto = intval($fstrto);.
$fstrto = 10;
$cstrto = 7;
if($status == 'N')
{
$cond = $fstrto <= $cstrto;
}
else
{
$cond = $fstrto >= $cstrto;
}
if($cond)
{
echo "Success";
}
Why it works: $cond is being assigned the value of a boolean expression, the values of which can be true or false. if($cond) just checks whether $cond is true or false
what is need to do is when using string as a php code use
eval — Evaluate a string as PHP code
Use below code work like charm:
$fstrto = "10";
$cstrto = "7";
if($status == 'N')
{
$cond = "$fstrto <= $cstrto";
}
else
{
$cond = "$fstrto >= $cstrto";
}
if(eval("return $cond;"))
{
echo "Success";
}
IMPORTANT:
Use of eval is highly discouraged
NEVER EVER use eval with params by POST/GET without sanitize them
When is eval evil in php?
You "$fstrto <= $cstrto" is a string now a compare statement.
$fstrto = "10";
$cstrto = "7";
if( ($status == 'N' && $fstrto <= $cstrto) || ($status != 'N' && $fstrto >= $cstrto) )
{
echo "Success";
}
Potentially turn it into a function that funnels into a switch statement like so:
function evaluateCondition($status, $a, $b) {
switch ($status) {
case 'Y':
return $a >= $b;
break;
case 'N':
return $a <= $b;
break;
default:
// Error Log. Unknown Status.
}
}
Any future addition can be appended onto the switch statement as necessary, if it gets more convoluted have each case return a separate function() to improve readability.
In terms of the current version you could use it like so:
$result = evaluateCondition('Y', 5, 6);
var_dump($result); // bool(false)
Hope that helps.
$fstrto = "10";
$cstrto = "7";
$cond = false;
if($status == 'N')
{
if($fstrto <= $cstrto){
$cond = true;
}
}
else
{
if($fstrto >= $cstrto){
$cond = false;
}
}
if($cond)
{
echo "Success";
}
I want to change the students marks to GP so I have code like this
$mark=80;
if($mark>=84.50 && $mark<=100){$GP = 4;}
if($mark>=79.50 && $mark<=84.49){$GP = 3.67;}
if($mark>=74.50 && $mark<=79.49){$GP = 3.33;}
if($mark>=69.50 && $mark<=74.49){$GP = 3;}
if($mark>=64.50 && $mark<=69.49){$GP = 2.50;}
if($mark>=59.50 && $mark<=64.49){$GP = 2;}
if($mark>=54.50 && $mark<=59.49){$GP = 1.50;}
if($mark>=49.50 && $mark<=54.49){$GP = 1;}
else { $GP = 0; }
echo $GP;
Expected output here is 3.67 but here I am getting 0. Can anyone explain why I am getting 0?
Each if is evaluated independently. Regardless of what happens with the first couple of ifs, you reach the last one, and since $mark isn't between 49.50 and 54.49, the code enters the else block, and sets $GP to 0. If you replace all the ifs except the first one with else ifs you'll get the intended logic:
$mark=80;
if($mark>=84.50 && $mark<=100){$GP = 4;}
else if($mark>=79.50 && $mark<=84.49){$GP = 3.67;}
else if($mark>=74.50 && $mark<=79.49){$GP = 3.33;}
else if($mark>=69.50 && $mark<=74.49){$GP = 3;}
else if($mark>=64.50 && $mark<=69.49){$GP = 2.50;}
else if($mark>=59.50 && $mark<=64.49){$GP = 2;}
else if($mark>=54.50 && $mark<=59.49){$GP = 1.50;}
else if($mark>=49.50 && $mark<=54.49){$GP = 1;}
else { $GP = 0; }
echo $GP;
Your code fails because your else only applies to the last condition. That means all the previous if statements have come to nothing.
You should use elseif to make sure that the conditions are truly distinct:
$mark=80;
if($mark>=84.50 $GP = 4;
elseif($mark>=79.50) $GP = 3.67;
elseif($mark>=74.50) $GP = 3.33;
elseif($mark>=69.50) $GP = 3;
elseif($mark>=64.50) $GP = 2.50;
elseif($mark>=59.50) $GP = 2;
elseif($mark>=54.50) $GP = 1.50;
elseif($mark>=49.50) $GP = 1;
else $GP = 0;
echo $GP;
As you see, you can also dispense with the && additional condition. Finally, you don’t need braces ({}) around a simple statement.
<?php
$mark=80;
if($mark>=84.50 && $mark<=100){$GP = 4;}
elseif(abs($mark>=79.50 && $mark<=84.49)){$GP = 3.67;}
elseif($mark>=74.50 && $mark<=79.49){$GP = 3.33;}
elseif($mark>=69.50 && $mark<=74.49){$GP = 3;}
elseif($mark>=64.50 && $mark<=69.49){$GP = 2.50;}
elseif($mark>=59.50 && $mark<=64.49){$GP = 2;}
elseif($mark>=54.50 && $mark<=59.49){$GP = 1.50;}
elseif($mark>=49.50 && $mark<=54.49){$GP = 1;}
else { $GP = 0; }
echo $GP;
?>
Your comparision not in proper way check above code.
This seems simple but I don't know why it doe snot work.
I need to write an if statement that
first, checks if it is numeric
second, if it is not between 1 and 10, issue errorA
third, if it is not between 20 and 30, issue errorB
fourth, it is not a number, issue errorC
If is not numeric and satisfies all the ranges, added to the database.
anyways, I am not sure about the if and while combination to satisfy this....
So far I have,
if numeric and satisfies ranges, add to database
else, issue errorC
How can I filter for error A and B?
if ( isset [some code...]) {
$a = ...;
$b = ...);
$c = ...;
if (preg_match('/^\d+$/',$b) && preg_match('/^\d+$/',$c) &&
((1 <= $b && 10 >= $b)) && ((20 <= $c && 30 >= $c))) {
$sql = "INSERT [some code...]
mysql_query($sql);
$_SESSION['success'] = $_POST['success'];
header('Location: index.php') ;
return;
} else {
$_SESSION['error'] = $_POST['error'];
header('Location: index.php') ;
return;
}
}
if (preg_match('/^\d+$/',$b) && preg_match('/^\d+$/',$c)) {
if (($b >= 1 && $b <= 10) && ($c >= 20 && $c <= 30)) {
echo "OK";
} else {
echo "not in range";
}
} else {
echo "not a number";
}
im not sure this is a good question to post but here is my issue. I have an if statement that is getting way too long and i was wondering if there is some other kind of syntax to shorten it out:
if (($time1 <= $one_day)&&
($time2 <= $one_day)&&
($time3 <= $one_day)&&
($time4 <= $one_day)&&
($time5 <= $one_day)&&
($time1 != NULL)&&
($time2 != NULL)&&
($time3 != NULL)&&
($time4 != NULL)&&
($time5 != NULL)){
//do sometihng
}
this is one example but i have a similar one that goes up to ..&&($time15 <= $one_day).
the statement is pretty self explanatory, $time1, $time2, etc can come back empty so i have to check if they are NULL or not
any ideas?
thanks
You can put the common stuff in a function:
function validate_time($time, $one_day) {
return $time <= $one_day && $time != NULL;
}
if (validate_time($time1, $one_day) &&
validate_time($time2, $one_day) &&
validate_time($time3, $one_day) &&
validate_time($time4, $one_day) &&
validate_time($time5, $one_day)) {
// do something
}
You may want to refactor code and eliminate the need for copying & pasting those checks. Another way to get the job done:
while (true) {
foreach (array($time1, $time2, $time3, $time4, $time5) as $time) {
if ($time > $one_day || $time == NULL) {
break 2;
}
}
// do something
break;
}
The above could be put in a function as well which would make the while-loop and break keyword redundant. Replace break 2 by return then.
Using an array for you variables would help. The you can iterate over them and check.
Put the times in an array and have a for loop to do the checking.
Instead of using 15 similar but different variables, consider using an array.
If you must (or want to) keep the original variable names and not use an array here is the good solution (for $time1 to $time5):
$ok = true;
for ($i = 1; $i <= 5; $i++)
{
$var =& ${'time'.$i};
if ( ! ($var <= $one_day && $var != NULL))
{
$ok = false;
}
}
if ($ok)
{
//do something
}
You can set all the values into an Array and compare it using an For Loop.
A functionised version which should help with your reuse. This is similar to Lekensteyns code.
$times = array(
'time',
'time',
'time',
'time',
'time',
);
function validateTime($checks, $limit)
{
foreach($checks as $check) {
if($check == null || $check > $limit) {
return false;
}
}
return true;
}
if(validateTime($times,$one_day) == true) {
//codey code.
}