Jump to content

Foreach looping


Xtremer360

Recommended Posts

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!';
            }

Link to comment
Share on other sites

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!';
            }
            

Link to comment
Share on other sites

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;      
        } 
            
    }

Link to comment
Share on other sites

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.

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.