Page 1 of 1

If statement help

Posted: Sun Jun 12, 2005 1:28 am
by nhan
i have this code

Code: Select all

if  ($t2 > $data1 && $t2 < $data2){
print $shift2;
}
elseif ($t2 > $data3 && $t2 < $data4){
print $shift3;
}
elseif ($t2 > $data5 && $t2 < $data6){
print $shift1;
} 
elseif ($t2 > $data7 && $t2 < $data8){
print $shift4;
}
else{
print $none;
}
is there a way i can declare the print $shift# into a variable?
the code is working perfectly with the print but when i do this..

Code: Select all

if  (($t2>$data1) && ($t2<$data2)){
$shift = 'B-Shift';
}
it does not display the word "B-Shift" when i add $shift into my mysql query....

can someone help me.. thanks!

--nahn

d11wtq | C'mon, 49 posts and still using

Code: Select all

tags. We use

Code: Select all

tags for PHP code [/color]

Posted: Sun Jun 12, 2005 1:46 am
by hongco
first of all, your if condition is not correct.

your $t2 will fall in else case at all time.

Posted: Sun Jun 12, 2005 2:34 am
by anjanesh

Code: Select all

<?php
$data1 = 0;
$data2 = 50;
$data3 = 100;
$data4 = 150;
$data5 = 250;
$data6 = 300;
$data7 = 500;
$data8 = 1000;

$shift2 = "a";
$shift3 = "b";
$shift4 = "c";
$shift5 = "d";
$shift6 = "e";
$shift7 = "f";
$shift8 = "g";

$t2 = "228";

for ($i=2; $i<8; $i++)
 {
        $data_prev = "data".($i-1);
        $data_curr = "data".$i;
        $shift_var = "shift".$i;
        if ($t2 > $$data_prev && $t2 < $$data_curr)
         {
                echo $$shift_var;
         }
 }
?>
Arrays are better though.

Posted: Sun Jun 12, 2005 3:38 am
by nhan
im sorry, heres the whole script, t2 is actually a time variable...
the conditions are met if the "print $shift#" is used after the if..elseif statements. But when i used "$shift=$shift#" and used "$shift" as a part of the query it does not work. :(

i am really in a mess here.. thanks!

Code: Select all

$shift1 = 'A-Shift';
$shift2 = 'B-Shift';
$shift3 = 'C-Shift';
$shift4 = 'D-Shift';
$none = 'Floating';
$data1 = 630; 
$data2 = 730;
$data3 = 1330;
$data4 = 1500;
$data5 = 2130;
$data6 = 2300;
$data7 = 731;
$data8 = 900;

if  ($t2 > $data1 && $t2 < $data2){print $shift2;}
elseif ($t2 > $data3 && $t2 < $data4){print $shift3;}
elseif ($t2 > $data5 && $t2 < $data6){print $shift1;} 
elseif ($t2 > $data7 && $t2 < $data8){print $shift4;}
else{print $none;}

$db = mysql_connect('XX', 'XX', 'XX') or die("Couldn't connect to the database."); 
mysql_select_db('XX') or die("Couldn't select the database"); 
$query = "INSERT INTO timein (userName,ip,dayin,timein,time1,day1,shift) VALUES ('$name','$ip','$day','$Today','$time','$d','$shift')";
$result = mysql_query($query) or die('Error, insert query failed');
mysql_close();
header("Location:XX.php?sid=$id");
d11wtq | :roll:

Posted: Thu Jun 16, 2005 2:34 am
by nhan
is my code really wrong??? thanks!

Posted: Thu Jun 16, 2005 3:11 am
by phpScott
this should work.

Code: Select all

$shift1 = 'A-Shift';
$shift2 = 'B-Shift';
$shift3 = 'C-Shift';
$shift4 = 'D-Shift';
$none = 'Floating';
$data1 = 630; 
$data2 = 730;
$data3 = 1330;
$data4 = 1500;
$data5 = 2130;
$data6 = 2300;
$data7 = 731;
$data8 = 900;
 
if  ($t2 > $data1 && $t2 < $data2){$shift = $shift2;}
elseif ($t2 > $data3 && $t2 < $data4){$shift = $shift3;}
elseif ($t2 > $data5 && $t2 < $data6){$shift = $shift1;} 
elseif ($t2 > $data7 && $t2 < $data8){ $shift =$shift4;}
else{$shift = $none;}
 
$db = mysql_connect('XX', 'XX', 'XX') or die("Couldn't connect to the database."); 
mysql_select_db('XX') or die("Couldn't select the database"); 
$query = "INSERT INTO timein (userName,ip,dayin,timein,time1,day1,shift) VALUES ('$name','$ip','$day','$Today','$time','$d','$shift')";
$result = mysql_query($query) or die('Error, insert query failed');
mysql_close();

echo "sql is $sql";
// just temporarly commint out the header redirect for testing
//header("Location:XX.php?sid=$id");

Posted: Thu Jun 16, 2005 3:34 am
by nhan
theres nothing wrong with the header, my only concern is in the condition statement if else....

just want to declare the value of $shift into a variable... when i use the print command it prints the abcd shift, but when i assign it to a variable it does not reflect when i use it in the query....

thanks!

Posted: Thu Jun 16, 2005 5:44 am
by phpScott
I only commented out the header so that you wouldn't get any additional error messages about warning header already sent.

what does sql print out when you run the script i suggested

Posted: Thu Jun 16, 2005 7:21 am
by nhan
it just prints "sql is" then blank....
:(

Posted: Thu Jun 16, 2005 7:30 am
by malcolmboston
amendment..

Code: Select all

$query = "INSERT INTO timein (userName,ip,dayin,timein,time1,day1,shift) VALUES ('$name','$ip','$day','$Today','$time','$d','$shift')";
$result = mysql_query($query) or die('Error, insert query failed');
mysql_close();
echo "sql is {$query}";

Posted: Thu Jun 16, 2005 7:31 am
by artexercise
where is the $sql variable populated? Are you trying to print the INSERT statement to the screen? "sql is $query" instead of "sql is $sql".

Posted: Thu Jun 16, 2005 7:34 am
by nhan
nope i just want to print the value of $shift, wether it is on a-shift, b-shift, c-shift or d-shift.....