Welcome Guest, Not a member yet? Register   Sign In
insertng an array to a database
#1

[eluser]russ_kern[/eluser]
I have a data entry form with a phone number field. I have broken the field into 3 parts to gather the Area code, first 3 digits and the last 4. I want to treat these as an array. The html for the feilds in my form look like:

Code:
<input id="enter" type="text" name="dphone[]" value="<?php echo set_value('dphone[]'); ?>" size="3"> -
    <input id="enter" type="text" name="dphone[]" value="<?php echo set_value('dphone[]'); ?>" size="3"> -
    <input id="enter" type="text" name="dphone[]" value="<?php echo set_value('dphone[]'); ?>" size="4">

My form validation looks like:

Code:
$this->form_validation->set_rules('dphone[]', 'Day Phone', 'trim|required|xss_clean|callback_phonecheck');

And the callback looks like:
Code:
function phonecheck($str)
    {
        
        $sql = "SELECT * FROM entries WHERE dphone = ?";
        $query = $this->db->query($sql, $str);
            if ($query->num_rows() > 0)
            {
                $this->form_validation->set_message('phonecheck', 'Only one entry is allowed per phone number');
                return FALSE;
            
            } else {
                return TRUE;
            
            }

    
    
    }

The line of code to insert the Complet phone number into the DB looks like:
Code:
$this->dphone = $this->input->post('dphone[]');

The regular form validations work correctly but on submission to the database it inserts a '0'

The way I want it to insert is: 1112223333

My callback isn't working correctly either, but this is probably due to the record insertion into the database... If I can get the data insertion working correctly I'll tackle that next.

I followed the userguide on this (I think) - I'm hoping someone can point out what I have done wrong. Hopefully this makes sense...

Thanks in advance.


Russ
#2

[eluser]adityamenon[/eluser]
It would be rather simple to to treat each input field as a string and concatenate before insert, than bother with arrays of text-input fields (I never saw *that* one before!).

html
Code:
<input id="enter" type="text" name="areaCode" value="<?php echo set_value('areaCode'); ?>" size="3"> -
<input id="enter" type="text" name="firstThreeDigits" value="<?php echo set_value('firstThreeDigits'); ?>" size="3"> -
<input id="enter" type="text" name="lastFourDigits" value="<?php echo set_value('lastFourDigits'); ?>" size="4">

form validation
Code:
$this->form_validation->set_rules('areaCode', 'Area Code', 'trim|required|xss_clean');
$this->form_validation->set_rules('firstThreeDigits', 'First Three Digits', 'trim|required|xss_clean');
$this->form_validation->set_rules('lastFourDigits', 'Last Four Digits', 'trim|required|xss_clean');

instead of using a callback, just exit with a simple if condition fail in case of errors
Code:
$str = $this->input->post('areaCode').$this->input->post('firstThreeDigits').$this->input->post('lastFourDigits');

function phonecheck($str)
    {
        
        $sql = "SELECT * FROM entries WHERE dphone = ?";
        $query = $this->db->query($sql, $str);
            if ($query->num_rows() > 0)
            {
                $this->form_validation->set_message('phonecheck', 'Only one entry is allowed per phone number');
                return FALSE;
            
            } else {
                return TRUE;
            
            }
    }

insert
Code:
$str = $this->input->post('areaCode').$this->input->post('firstThreeDigits').$this->input->post('lastFourDigits');

$this->dphone = $str;

I'm not sure if concatenating input class return values directly will work, just get your values each in a separate variables if it doesn't.
#3

[eluser]russ_kern[/eluser]
LOL.. well.. you are right... I would prefer to use your method (and it is the way I would normally do it) but the callback was throwing me on that ... from your example I see how it should work. I'll give that a run...

I tried method I posted because it seemed to me from the User Guide that it should work... I was apparently wrong Smile

Thanks for the quick response.

R
#4

[eluser]russ_kern[/eluser]
Perfect... Callback even worked great ....

I attached it to the last 4 digits so it was the last thing to be called.

Code:
$this->form_validation->set_rules('areaCode', 'Area Code', 'trim|required|xss_clean');
        $this->form_validation->set_rules('firstThreeDigits', 'First three digits', 'trim|required|xss_clean');
        $this->form_validation->set_rules('lastFourDigits', 'Last Four Digits', 'trim|required|xss_clean|callback_phonecheck');


And I set the callback up as:

Code:
function phonecheck($str)
    {
        $str = $this->input->post('areaCode').$this->input->post('firstThreeDigits').$this->input->post('lastFourDigits');
        
        $sql = "SELECT * FROM entries WHERE dphone = ?";
        $query = $this->db->query($sql, $str);
            if ($query->num_rows() > 0)
            {
                $this->form_validation->set_message('phonecheck', 'Only one entry is allowed per phone number');
                return FALSE;
            
            } else {
                
                $str = '+1'.$str;
                $sql = "SELECT * FROM entries WHERE cphone = ?";
                $query = $this->db->query($sql, $str);
                    if ($query->num_rows() > 0)
                {
                    $this->form_validation->set_message('phonecheck', 'Only one entry is allowed per phone number');
                    return FALSE;
            
                } else {
            
                    return TRUE;
                }

    
            }
        }



This callback has a little more code than the previous, but I thought I'd share the whole thing.

Thank you much.

R
#5

[eluser]adityamenon[/eluser]
I actually wanted to suggest that, but I didn't think of concatenating $str inside the callback, so I wasn't sure of the details... glad you sorted it out Smile




Theme © iAndrew 2016 - Forum software by © MyBB