Welcome Guest, Not a member yet? Register   Sign In
count of empty array
#1

[eluser]PHPraja[/eluser]
when i try to get the count of empty array which is posted from a form, I get the count as 1. Actually the count should be zero. Is nt it? Any one faced such problem?
#2

[eluser]Hermawan Haryanto[/eluser]
Are you sure that it's an array?
Take a look at this manual here: http://id.php.net/count
If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned.

Good luck,
Hermawan Haryanto
#3

[eluser]andreagam[/eluser]
Depending on the formulation, you could be getting a nested array, that is an array of arrays...
If so you could try counting the size of arrayname[0] to see if it's really empty.
#4

[eluser]gtech[/eluser]
you could always try print_r($array_or_obj_name); then you will know the contents.
#5

[eluser]PHPraja[/eluser]
Actually i got this count of empty array while validations using call back functions.

In my 'EmailToFriend' view i have a message box(text area), some list of dynamic check boxes(Friends list) and a textarea where the user can enter his friends emailid with a comma separated. Before submitting the form we should validate such that atleast one checkbox is checked or atleast one emailid is entered in the textarea. for this i used a CI validation as below using callback function.

Code:
$rules['emailslist']="callback_emailslist_check";
        $rules['message']="required";
        $this->validation->set_rules($rules);
        $fields['emailslist']    = " Email List ";
        $fields['message']    = " Message ";
        $this->validation->set_fields($fields);
        
        
        if ($this->validation->run() == FALSE)
            {   redirect(place/placeemail); }

// This is a call back function
   function emailslist_check($e)
        {
            if((count($this->input->post('chkEmails'))==1 ) && ($e==''))
            {
                //echo count($this->input->post('chkEmails')); die;
                $this->validation->set_message('emailslist_check', 'At least one recipient is required');
                return FALSE;
            } else {
            return TRUE;
            }
        }

In the above code 'emailslist' is the name of the textarea where the emailids should be entered and 'chkEmails' is the dynamic array (checkbox name ) This call back function does nt work. In the callback function can i check combination of two fields for validation? as i used above. I hope the above explanation is clear to express my doubt. any one plz help..
#6

[eluser]Seppo[/eluser]
Try this
if((is_array($this->input->post('chkEmails')) && count($this->input->post('chkEmails'))==1 ) && ($e==''))




Theme © iAndrew 2016 - Forum software by © MyBB