Welcome Guest, Not a member yet? Register   Sign In
Need help returning single row from database
#1

[eluser]blinger2008[/eluser]
Hi, I am having trouble returning just a single row from the database after a user clicks on a person's name. I want it to go to the page with all his information on it. I can get it to go to that page, but it is returning all the rows. If you could help me, I'd appreciate it.

Model:
Code:
function get_records(){
        
        $query = $this->db->get('people');
        return $query->result();
        }

Controller
Code:
function person(){
        $data = array();
        
        if($query = $this->people_model->get_records()){
            $data['records'] = $query;
            }
        //$data["people"]=$this->people_model->get_person($personID);
        $this->masterpage->addContentPage('head/default', 'head');
        $this->masterpage->addContentPage('content/person', 'content', $data);
        $this->masterpage->show();
        
        }

View
Code:
<?php if(isset($records)) : foreach($records as $row) :?>

<a href="&lt;?php echo site_url('index.php/people/person'); ?&gt;">&lt;?php echo $row->firstName; ?&gt; &lt;?php echo $row->lastName; ?&gt;</a><br/>
&lt;?php endforeach; ?&gt;

&lt;?php else : ?&gt;
<h3>No records were returned</h3>
&lt;?php endif; ?&gt;
#2

[eluser]JHackamack[/eluser]
What you have is a good example of how to list out all people in your database, try this for individual people: (replace $this->db->where('id') with your id column name
Code:
function get_record($id){
        $this->db->where('id',$id)
        $query = $this->db->get('people');
        return $query->row();
        }

Code:
function person(){
        $data = array();
        
        if($this->uri->segment(3)) {
        $person=$this->people_model->get_records($this->uri->segment(3));
print_r($person);
die();
} else {
if($query = $this->people_model->get_records()){
            $data['records'] = $query;
            }
}
        $this->masterpage->addContentPage('head/default', 'head');
        $this->masterpage->addContentPage('content/person', 'content', $data);
        $this->masterpage->show();
        
        }
Code:
&lt;?php if(isset($records)) : foreach($records as $row) :?&gt;

<a >id); >&lt;?php echo $row->firstName; ?&gt; &lt;?php echo $row->lastName; ?&gt;</a><br/>
&lt;?php endforeach; ?&gt;

&lt;?php else : ?&gt;
<h3>No records were returned</h3>
&lt;?php endif; ?&gt;

This is all a very basic idea to get you heading in the right direction. Let me know if you have any problems.
#3

[eluser]JHackamack[/eluser]
That href should be:
site_url('index.php/people/person'.$row->id)
#4

[eluser]blinger2008[/eluser]
I put that in there, and it just gave me an array of all the people.
#5

[eluser]JHackamack[/eluser]
You should see an array of all the people, but clicking on them should give you the individual person's information
#6

[eluser]blinger2008[/eluser]
Yeah, the first page gives me the list of names, and when i click on one of the names, a giant array comes up of everyones info.

Code:
Array ( [0] => stdClass Object ( [id] => 1 [firstName] => J Gilbert [lastName] => Kaufman [title] => [photo] => images/image.jpg [bio] => This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. [resume] => This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. ) [1] => stdClass Object ( [id] => 2 [firstName] => Mark [lastName] => Keister [title] => P.E. [photo] => images/image.jpg [bio] => This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. This is his bio. [resume] => This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. This is his resume. ) )
#7

[eluser]blinger2008[/eluser]
alright i got it but it still spits out as an array, not my person view page.
#8

[eluser]JHackamack[/eluser]
Doh, that was a mistake on my part:]
Code:
$person=$this->people_model->get_records($this->uri->segment(3));
should be
Code:
$person=$this->people_model->get_record($this->uri->segment(3));
#9

[eluser]JHackamack[/eluser]
Because i had the print_r and then die its spitting out the Array. Just replace that with your view call and you should be good.
#10

[eluser]blinger2008[/eluser]
Yeah I figured out that part but it shows as an array.




Theme © iAndrew 2016 - Forum software by © MyBB