Welcome Guest, Not a member yet? Register   Sign In
Echo a single entry from a single DB returned array.
#1

[eluser]mflammia[/eluser]
Sorry if this is a simple but really struggling to find an answer.

Have the following array returned to the view from a single DB query:

Code:
<?PHP
        echo "<pre>";
print_r($person);
echo "<pre>";
?&gt;

Code:
Array
(
    [0] => stdClass Object
        (
            [id_person] => 95
            [first_name] => Abbey
            [second_name] => Nobody
            [dob] => 1973-09-16
            [email] =>
            [phone] =>
        )

)

I can get something like the first name to display by doing something like the following:

Code:
&lt;?php foreach($person as $row):?&gt;
&lt;?php echo $row->first_name ?&gt;
&lt;?php endforeach;?&gt;

Which seems a long winded / incorrect why to go about something that only has one record.

I've tried every combination of coding to just echo a single part of the array, with just a few like:

Code:
echo $person[0]['first_name'];
echo $person ['first_name'];
echo $person->firstname;

Without any luck.

Help much appreciated in advance
#2

[eluser]CroNiX[/eluser]
If you know you are only retrieving one record, use db::row() instead of db::result() and use db::row_array() instead of db::result_array(); Check the docs for database/generating query results for the options.
#3

[eluser]mflammia[/eluser]
Thank for replying and pointing me in the right direction - had looked there many times but it just did not make sense to me. Took me a while and learnt something new in the process, thanks.

For anyone else having the same problem this is what I did.

In my model

Code:
function get_person(){
            $query = $this->db->query('SELECT * FROM person WHERE id_person='.$this-&gt;uri-&gt;segment(3));
            return $query-&gt;row();
        }

In my controller

Code:
function get_records_edit(){
                //$this-&gt;load-&gt;model('site_model');
                $data['person'] = $this->site_model->get_person();
return $data;
        }

In my view i simpely do the following

Code:
&lt;?php echo $person->first_name ?&gt;
&lt;?php echo $person->second_name ?&gt;
#4

[eluser]jmadsen[/eluser]
good job - that's the idea.

In your model, I would add a check first on

Code:
if ($query->numrows() ==1)
{
   return $query->row();  // or ->row_array();
}
return false;

or you can write more generic and return a result(), a row() or false ...how ever you want to expand this (so get person could be for use with a where clause)

Also, I would take the $this->uri->segment(3)) out of the model and pass it in via a parameter instead, that can be validated

Eventually you may want to look into using MY_Model - lots of tutorials out there about that
#5

[eluser]Samus[/eluser]
[quote author="jmadsen" date="1333748548"]good job - that's the idea.

In your model, I would add a check first on

Code:
if ($query->numrows() ==1)
{
   return $query->row();  // or ->row_array();
}
return false;

or you can write more generic and return a result(), a row() or false ...how ever you want to expand this (so get person could be for use with a where clause)

Also, I would take the $this->uri->segment(3)) out of the model and pass it in via a parameter instead, that can be validated

Eventually you may want to look into using MY_Model - lots of tutorials out there about that[/quote]

Code:
if ($query->num_rows() ==1)
{
   return $query->row();  // or ->row_array();
}
return false;

Little typo. Smile




Theme © iAndrew 2016 - Forum software by © MyBB