Welcome Guest, Not a member yet? Register   Sign In
Accessing and changing data in controller
#1

[eluser]cleansugar[/eluser]
controller:
Code:
function detail($name)
    {
        $data['result'] = $this->hello_model->getDetail($name);
        foreach ($data['result'] as $row)
           {
               if ($row->sex == 0){
                  $row->sex = 'female';
              }else{
                  $row->sex = 'male';
              };
}
    foreach ($data['result'] as $row)
        {
              if ($row->bigo != null){
                  $this->load->view('bigodetail_view',$data);
              }else{
                  $this->load->view('detail_view',$data);
              };
           }
model:
Code:
function getDetail($name)
    {
        $this->db->where('name', $name);
        $query = $this->db->get('table1');
        
        if ($query->num_rows() == 0)
        {
            //show_error('Database is empty!');
        }else{
            return $query->result();
        }
    }

if $row->sex 0 female, 1->male.
Did I do a right programming? changing $data['result'] is not thought as right.

If I want to make and add to this obeject another sex1 property(sex1 is string like 'male' or 'female'), what can I do?

And In controller, foreach is required to read $data['result']?
Any other way like $data['result']['sex'][n] does not exist?

Please help...
#2

[eluser]Cro_Crx[/eluser]
Quote:if $row->sex 0 female, 1->male.
Did I do a right programming? changing $data['result'] is not thought as right.

If you're storing female's as 0 and 1 for male then yeah. But why not store 'male' and 'female' values in an enum database attribute?

Quote:And In controller, foreach is required to read $data['result']?
Any other way like $data['result']['sex'][n] does not exist?

Well no, it wouldn't exist. If you want to see what's inside of $data to get a feel for it, try print_r($data).

But basically, $data['result'] is an array containing objects. So you would access it like this $data['result'][n]->sex

Using foreach as you're doing already is the most efficient way of accessing the array. using for() is actually far slower in PHP.




Theme © iAndrew 2016 - Forum software by © MyBB