Jump to content

How to do this


I-AM-OBODO
Go to solution Solved by I-AM-OBODO,

Recommended Posts

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
Share on other sites

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;
Edited by QuickOldCar
Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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;

Edited by QuickOldCar
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.