I-AM-OBODO Posted November 26, 2014 Share Posted November 26, 2014 Hi, I have a point that is assigned to an individual based on their duration in the program. I used datediff to calculate the date difference and the result I assigned to a variable. Now I want to do this: if day = 0-5, point =6 what I did was if($diff == 0 OR 1 OR 2 OR 3 OR 4 OR 5){ $point =6; }elseif($diff == 6 OR 7 OR 8 OR 9 OR 10){ $point =4; } else $point =1; } echo $point; Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/ Share on other sites More sharing options...
QuickOldCar Posted November 26, 2014 Share Posted November 26, 2014 You can tackle this the way you are if ($diff == 0 || $diff == 1 || $diff == 2 || $diff == 3 || $diff == 4 || $diff == 5) { $point = 6; } elseif ($diff == 6 || $diff == 7 || $diff == 8 || $diff == 9 || $diff == 10) { $point = 4; } else { $point = 1; } echo $point; Make a switch switch ($diff) { case 0: $point = 6; break; case 1: $point = 6; break; case 2: $point = 6; break; case 3: $point = 6; break; case 4: $point = 6; break; case 5: $point = 6; break; case 6: $point = 4; break; case 7: $point = 4; break; case 8: $point = 4; break; case 9: $point = 4; break; case 10: $point = 4; break; default: $point = 1; } echo $point; Do a double array checking with in_array() $six_array = range(0, 5); $four_array = range(6, 10); if (in_array($diff, $six_array)) { $point = 6; } elseif (in_array($diff, $four_array)) { $point = 4; } else { $point = 1; } echo $point; Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497707 Share on other sites More sharing options...
Barand Posted November 26, 2014 Share Posted November 26, 2014 You can use "fall through" in a switch statement switch ($diff) { case 0: case 1: case 2: case 3: case 4: case 5: $point = 6; break; case 6: case 7: case 8: case 9: case 10: $point = 4; break; default: $point = 1; } echo $point; Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497717 Share on other sites More sharing options...
QuickOldCar Posted November 26, 2014 Share Posted November 26, 2014 Good point Barand. Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497718 Share on other sites More sharing options...
I-AM-OBODO Posted November 26, 2014 Author Share Posted November 26, 2014 Thanks @all Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497732 Share on other sites More sharing options...
I-AM-OBODO Posted November 26, 2014 Author Share Posted November 26, 2014 but how can I sum all the values from $point? Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497772 Share on other sites More sharing options...
Barand Posted November 26, 2014 Share Posted November 26, 2014 Define a total variable, say $total_points then accumulate the points values into that. $total_points += $points; Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497780 Share on other sites More sharing options...
I-AM-OBODO Posted November 26, 2014 Author Share Posted November 26, 2014 I did so but It's saying undefined variable: total_point and yet brings the result of only one row without adding the column Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497783 Share on other sites More sharing options...
Barand Posted November 26, 2014 Share Posted November 26, 2014 pseudocode version: $total_points = 0; // DEFINE the variable begin loop $points = whatever; // get points value $total_points += $points; // accumulate points total endloop echo $total_points; Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497785 Share on other sites More sharing options...
I-AM-OBODO Posted November 27, 2014 Author Share Posted November 27, 2014 still wouldn't work. could it be cos the result value is not in the database? cos diff is d result of a datediff value. I.e $stmt=$pdo->query("select datediff(due, paid) as diff where name =:name"); $stmt->execute() while ($row = $stmt->fetch (PDO::FETCH_ASSOC)) { $diff=$row['diff'] ; } switch ($diff) { case 0: case 1: case 2: case 3: case 4: case 5: $point = 6; break; case 6: case 7: case 8: case 9: case 10: $point = 4; break; default: $point = 1; } echo $point; $total_points = 0; // DEFINE the variable begin loop $points = $point; // get points value $total_points += $points; // accumulate points total endloop echo $total_points; in the above code let's say I have 6 as $diff on the first row and on the second row I have $diff as 4, the $total_points ought be 10 but its giving me 6 Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497807 Share on other sites More sharing options...
I-AM-OBODO Posted November 27, 2014 Author Share Posted November 27, 2014 still wouldn't work. could it be cos the result value is not in the database? cos diff is d result of a datediff value. I.e $stmt=$pdo->query("select datediff(due, paid) as diff where name =:name"); $stmt->execute() while ($row = $stmt->fetch (PDO::FETCH_ASSOC)) { $diff=$row['diff'] ; } switch ($diff) { case 0: case 1: case 2: case 3: case 4: case 5: $point = 6; break; case 6: case 7: case 8: case 9: case 10: $point = 4; break; default: $point = 1; } echo $point; $total_points = 0; // DEFINE the variable begin loop $points = $point; // get points value $total_points += $points; // accumulate points total endloop echo $total_points; in the above code let's say I have 6 as $diff on the first row and on the second row I have $diff as 4, the $total_points ought be 10 but its giving me 6 Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497808 Share on other sites More sharing options...
QuickOldCar Posted November 27, 2014 Share Posted November 27, 2014 Because you need to have everything in your while loop or make them an array and do later on. $stmt=$pdo->query("select datediff(due, paid) as diff where name =:name"); $stmt->execute() $total_points = 0; while ($row = $stmt->fetch (PDO::FETCH_ASSOC)) { $diff=$row['diff'] ; switch ($diff) { case 0: case 1: case 2: case 3: case 4: case 5: $point = 6; break; case 6: case 7: case 8: case 9: case 10: $point = 4; break; default: $point = 1; } $total_points += $point; } echo $total_points; Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497809 Share on other sites More sharing options...
Barand Posted November 27, 2014 Share Posted November 27, 2014 I suggest you read the pseudocode again (reply #9) Setting the total to zero is done before the loop begins. Inside the loop calculate the value for the points accumulate into the total After the loop echo the accumulated total Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497811 Share on other sites More sharing options...
I-AM-OBODO Posted November 28, 2014 Author Share Posted November 28, 2014 It's all working now. Thanks @QuickOldCar and @Barand Link to comment https://forums.phpfreaks.com/topic/292720-how-to-do-this/#findComment-1497907 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.