Colbyd
Colbyd

Reputation: 371

Perform function in a while loop

I have this function that returns a bool(true) or bool(false) if the movement exists.

function movement_performed_today($class_id, $client_id){
$class_id = (int)$class_id;
$client_id = (int)$client_id;



$query = mysql_query("SELECT COUNT(`movement`) FROM `completed_movements` WHERE `class_id` = '$class_id' AND `client_id` = '$client_id' AND `date` = CURDATE()");

$movement_performed = mysql_fetch_row($query);
return ($movement_performed[0] > 0);

}

I have this while loop where I want to call this function and if it returns false perform the function completed_movement. I have tried many different ways but I just can't seem to get it to work. Any info will be much appreciated.

if (empty($_POST)=== false){
$i = 0;
while (isset($_POST["first_name"][$i])) {
    $movement_data = array(   
    'user_id'       => $session_user_id,   
    'class_id'      => $class_id,   
    'class_name'    => $class_name,
    'client_id'     => $_POST['client_id'][$i],    
    'first_name'    => $_POST['first_name'][$i],   
    'last_name'     => $_POST['last_name'][$i],   
    'nickname'      => $_POST['nickname'][$i],   
    'order'         => $_POST['order'][$i],   
    'movement'      => $_POST['movement'][$i],   
    'rep_set_sec'   => $_POST['rep_set_sec'][$i],   
    'rest'          => $_POST['rest'][$i],   
    'date'          => $today   
    );  

 //assign variable to the function
    $isPerformed = movement_performed_today($class_id, $_POST['client_id'][i]);

    //if return false perform this function
    if(! $isPerformed){ completed_movement($movement_data);}

   $i++; 
   } 

} // if empty   

What Im trying to do here is each time it loops through check to see if the function movement_performed_today is true or false. If returns false then insert this _POST data in to db. Not sure if Im doing this correctly because even though the function returns true it still calls the completed_movement function and post the info to db.

I believe the issue may lie here. Earlier this would return true or false but now just returns false even though the movement is present. This code:

$movement_performed = mysql_fetch_row($query);
//return ($movement_performed[0] > 0);
var_dump($movement_performed);

Returns this:

array(1) { [0]=> string(1) "0" } 
array(1) { [0]=> string(1) "0" }

If the movement is present shouldn't "0" be "1" or higher depending the number of times it matched up with the query.

Here is the working query:

function movement_performed_today($class_id, $client_id, $movement){
$class_id = (int)$class_id;
$client_id = (int)$client_id;



$query = mysql_query("SELECT COUNT(`movement`) FROM `completed_movements` WHERE `class_id` = '$class_id' AND `client_id` = '$client_id' AND `movement` = '$movement' AND `date` = CURDATE()");

$movement_performed = mysql_fetch_row($query);  
return ($movement_performed[0] > 0);

}

Here is the working while loop.

if (empty($_POST)=== false){
$i = 0;
while (isset($_POST["first_name"][$i])) {
    $movement_data = array(   
    'user_id'       => $session_user_id,   
    'class_id'      => $class_id,   
    'class_name'    => $class_name,
    'client_id'     => $_POST['client_id'][$i],    
    'first_name'    => $_POST['first_name'][$i],   
    'last_name'     => $_POST['last_name'][$i],   
    'nickname'      => $_POST['nickname'][$i],   
    'order'         => $_POST['order'][$i],   
    'movement'      => $_POST['movement'][$i],   
    'rep_set_sec'   => $_POST['rep_set_sec'][$i],   
    'rest'          => $_POST['rest'][$i],   
    'date'          => $today   
    );  

 //check not already performed today
    $isPerformed = movement_performed_today($class_id, $_POST['client_id'][$i], $_POST['movement'][$i]);

    //if not performed then do insert
    if (! $isPerformed){ completed_movement($movement_data);}

   $i++; 
   } 

} // if empty   

Thanks guys for all your help!

Upvotes: 1

Views: 184

Answers (2)

Stegrex
Stegrex

Reputation: 4024

Edit:

It sounds like you have trouble with your SQL query as well. Try:

$query = mysql_query("SELECT COUNT(`movement`) FROM `completed_movements` WHERE `class_id` = $class_id AND `client_id` = $client_id AND `date` = CURDATE()");

Since columns class_id and client_id appear to be integers, remove the single quotes around their values.

Edit: Corrected syntax, converted SQL result to integer first.

return ((int)$movement_performed[0] > 0 ? true : false);

If you use this return line, the function completed_movement() will return only booleans.

In this case, you want to be strict when doing comparison logic for calling the function.

You'll want to do this:

//if return false perform this function
if ($isPerformed === false) {
    completed_movement($movement_data);
}

Upvotes: 1

Steve Valliere
Steve Valliere

Reputation: 1892

I may be misreading this but it looks like you are setting $return to true or false, not actually returning the value. I believe you want to change:

$return = $movement_performed[0] > 0;

to

if($movement_performed[0] > 0) return true;
else return false;

Upvotes: 3

Related Questions