Welcome Guest, Not a member yet? Register   Sign In
I'm doing something simple wrong...
#1

[eluser]NateL[/eluser]
my controller - called client, is passed a string of text and assigned to the variable $client:

Code:
function index($client)
{
$data['client'] = $this->clients_model->get_this_client($client);
var_dump($data);
$this->load->view('client', $data);
}

It loads the clients_model method called get_this_client.

get_this_client simply queries the database and returns the result.

But, my var_dump spits out this info:


Code:
array
  'client' =>
    array
      0 =>
        object(stdClass)[14]
          public 'id' => string '1' (length=1)
          public 'userid' => string '3' (length=1)
          public 'clientname' => string 'Test Client' (length=11)
          public 'clientdir' => string 'testclient' (length=10)

Now - in my view - client - I am passing it the variable $data. I SHOULD be able to just plug in this code:

Code:
<?=$clientname?> // echos 'Test Client'

but - I have to use this code:

Code:
<?=$data[0]->clientname?> // echos 'Test Client'
which isn't right... Why am I getting that extra array in there?

If it helps any, here is Clients_model.php
Code:
function get_this_client($client)
        {
            $this->db->select('*')->
                    from('clients')->
                    where('clientdir', $client);
            
            $cdata = $this->db->get();
                        
            if ($cdata->num_rows() > 0){
                return $cdata->result();
            }
                
        }
#2

[eluser]Dam1an[/eluser]
You need the [0] part becuase you return a result set, where you want to use row (or row_array)
Change the model to
Code:
function get_this_client($client)
{
    $this->db->select('*')->
            from('clients')->
            where('clientdir', $client);
    
    $cdata = $this->db->get();
                
    if ($cdata->num_rows() > 0){
        return $cdata->row();
    }
        
}
#3

[eluser]jcavard[/eluser]
This is because your model returns the result(). If you are expecting only one row, you should change you model to ($cdata->row() instead of $cdata->result())
Code:
function get_this_client($client)
        {
            $this->db->select('*')->
                    from('clients')->
                    where('clientdir', $client);
            
            $cdata = $this->db->get();
                        
            if ($cdata->num_rows() > 0){
                return $cdata->row();
            }
                
        }

This way, you get rid of the [0] index. However, if you expect more than one row, don't change your controller, just loop through the record in your view that way:
Code:
foreach($data as $row)
{
    echo $row->clientname;
}

hope this help
#4

[eluser]jcavard[/eluser]
[quote author="Dam1an" date="1249598010"]You need the [0] part becuase you return a result set, where you want to use row (or row_array)
Change the model to
Code:
function get_this_client($client)
{
    $this->db->select('*')->
            from('clients')->
            where('clientdir', $client);
    
    $cdata = $this->db->get();
                
    if ($cdata->num_rows() > 0){
        return $cdata->row();
    }
        
}
[/quote]
damit. It took me 6 minutes more than you to explain the same thing.
COFFEE!!!!!!!
#5

[eluser]Dam1an[/eluser]
[quote author="jcavard" date="1249598789"][/quote]
damit. It took me 6 minutes more than you to explain the same thing.
COFFEE!!!!!!![/quote]

Haha
And you mean you need coffee or you've had too much and it's fried your brain?




Theme © iAndrew 2016 - Forum software by © MyBB