Welcome Guest, Not a member yet? Register   Sign In
Update or insert if record exits in table or not
#1

[eluser]mflammia[/eluser]
Hi,

Not sure if I am going about this correctly but within my model I have the following:

Code:
function get_person(){
            $query = $this->db->query('SELECT * FROM person WHERE id_person='.$this->uri->segment(3));
            if ($query->num_rows() ==1){
                return $query->row();
            }
            return false;
        }
        
        function get_address(){
            $query = $this->db->query('SELECT * FROM address WHERE id_person='.$this->uri->segment(3));
            if ($query->num_rows() == 1){
                return $query->row();
            }
            return false;
        }

Within my controller I have:

Code:
function get_records_edit(){
                if ($this->site_model->get_person()){
                    $data['person'] = $this->site_model->get_person();
                    $data['personSet']= 1;
                }else{
                    $data['personSet']= 0;;
                }
                if ($this->site_model->get_address()){
                    $data['address'] = $this->site_model->get_address();
                    $data['addressSet']= 1;
                }else{
                    $data['addressSet']= 0;
                }
}

and in my view I have something like:

Code:
<div>
        <label>Postcode</label>
        &lt;input name="postcode" type="text" value="&lt;?php if ($addressSet == 1){echo $address-&gt;postcode;} ?&gt;" size="40" maxlength="30" /&gt;
        <span>Enter a postcode in the format WD244LH</span>
    </div>

The person and address are different tables and the idea is two fold in that I test the data in the table exits, if so, output it in the value.

And also that I want to be able to do an edit from this view, so I need to tell my controller / model as to whether to update or insert a record depending on whether date already exits or not.

I was thinking of going about this by creating an update function that uses the

Code:
$data['personSet']
$data['addressSet']

values created within the controller get_records_edit function.

I'm stuck because I don't not only know how to call these values from another function / method within the controller when the update function is called or whether this is the right way to go about it?

Thanks in advance for any pointers / help.
#2

[eluser]Denzel[/eluser]
Check out this article. I don't think CodeIgniter supports this kind of query out of the box with Active Records, but you could definitely build the query yourself.




Theme © iAndrew 2016 - Forum software by © MyBB