Xtremer360 Posted March 25, 2012 Share Posted March 25, 2012 I'm trying to figure out what I'm doing wrong. For each recipient it runs each value which is an integer through the getUserByUserID function to verify that no values inside of the array are non users. But here's the part I'm not sure of and that's the if $recipient or $sender == FALSE. If ANY of the values are found to be non values then I want that if statement to activate. foreach ($this->input->post('recipient[]') AS $recipient) { $this->users->getUserByUserID($recipient); } $sender = $this->users->getUserByUserID($this->input->post('sender')); // Verifies users were returned from database if (($recipient == FALSE) || ($sender == FALSE)) { // Users was not found in the database $outputArray['message'] = 'Either the recipient or yourself could not be found in the database!'; } Quote Link to comment https://forums.phpfreaks.com/topic/259699-foreach-looping/ Share on other sites More sharing options...
scootstah Posted March 25, 2012 Share Posted March 25, 2012 $recipient is only going to be equal to the last iteration of the foreach loop. You need to do the check inside the foreach loop to check each item. Quote Link to comment https://forums.phpfreaks.com/topic/259699-foreach-looping/#findComment-1331000 Share on other sites More sharing options...
Xtremer360 Posted March 25, 2012 Author Share Posted March 25, 2012 So you are suggesting this: // Retrieves users from database with posted user ids foreach ($this->input->post('recipient[]') AS $recipient) { $this->users->getUserByUserID($recipient); if ($recipient == FALSE) { $outputArray['message'] = 'One or more of the recipients could not be found in the database!'; } } $sender = $this->users->getUserByUserID($this->input->post('sender')); if ($sender == FALSE) { $outputArray['message'] = 'You were for some reason not found in the database!'; } Quote Link to comment https://forums.phpfreaks.com/topic/259699-foreach-looping/#findComment-1331001 Share on other sites More sharing options...
scootstah Posted March 25, 2012 Share Posted March 25, 2012 Yes. Though I think your logic is off a little. I don't think $recipient is ever going to be false. Can you show me the getUserByUserId() method? Quote Link to comment https://forums.phpfreaks.com/topic/259699-foreach-looping/#findComment-1331011 Share on other sites More sharing options...
Xtremer360 Posted March 25, 2012 Author Share Posted March 25, 2012 I'm sorry it should have been NULL. /** * Get user record by user id * * @param int * @return object */ function getUserByUserId($userID) { $this->db->select('*'); $this->db->where('userID', $userID); $query = $this->db->get('users'); if ($query->num_rows() > 0) { $row = $query->row(); return $row; } else { return NULL; } } Quote Link to comment https://forums.phpfreaks.com/topic/259699-foreach-looping/#findComment-1331014 Share on other sites More sharing options...
scootstah Posted March 25, 2012 Share Posted March 25, 2012 Okay, then you need to do something like foreach ($this->input->post('recipient[]') AS $recipient) { if (!$this->users->getUserByUserID($recipient)) { $outputArray['message'] = 'One or more of the recipients could not be found in the database!'; } } There's a big problem with this though in that you will have a whole bunch of queries. A better way would be to pass an array to a method that checks for all of the recipients all at once. Quote Link to comment https://forums.phpfreaks.com/topic/259699-foreach-looping/#findComment-1331015 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.