Welcome Guest, Not a member yet? Register   Sign In
Problem extracting correct information from database
#1

[eluser]jbads[/eluser]
Hi, I am developing a function/page controller that would like to take a parameter from the url and use that parameter (in this case username) to extract data to populate a profile for the user.

Overall, the controller is in it's infancy, and I'm learning codeigniter as I go, therefore only the basic operations have been coded.

When I load this function through the browser eg. localhost/site/index.php/class/function/parameter it is only extracting the last row in the database, not the row specified in the url. If I enter a url with no /parameter/ it still returns the last entry in the database.

Here is my function thus far, could you please advise me what is wrong with my sql

Quote: function viewprofile(){
//retrieve username from URL
$url_username = $this->uri->segment(3);


if(!isset($url_username)){

redirect('core');

}

$data['segment_id'] = $url_username;

//get info from database
//build where clause in sql statement
$where = $this->db->where('username', $url_username);



//run query
$query = $this->db->query('SELECT * FROM table $where;');

$data['segment_id'] = $query->num_rows();



//check that there was a result
if ($query->num_rows() > 0){
//put results into usable variables
foreach($query->result_array() as $row){
$data['username'] = $row['username'];
$data['firstname'] = $row['firstname'];
$data['lastname'] = $row['lastname'];
$data['emailaddress'] = $row['emailaddress'];
}

}else{
redirect('core');
}


$page_header['title'] = "View Profile";

$this->load->view('index_header_view', $page_header);
$this->load->view('index_menu_view');
$this->load->view('profile/view_profile_body_view', $data);
$this->load->view('index_footer_view');

}


Any other advise in regards to this code would be greatly appreciated,
Thanks, Jake
#2

[eluser]wiredesignz[/eluser]
Code:
function viewprofile(){
    
    //retrieve username from URL
    if(!$url_username = $this->uri->segment(3))
    {
        redirect(’core’);
    }

    $data[’segment_id’] = $url_username;

    //get info from database
    //build where clause in sql statement
    $this->db->where(’username’, $url_username);

    //run query
    $query = $this->db->get('table');
    
    //check that there was a result
    if($data[’segment_id’] = $query->num_rows())
    {
        //put results into usable variables
        foreach($query->result_array() as $row)
        {
            $data[’username’] = $row[’username’];
            $data[’firstname’] = $row[’firstname’];
            $data[’lastname’] = $row[’lastname’];
            $data[’emailaddress’] = $row[’emailaddress’];
        }
    }
    else
    {

...

}
Totally untested, but the basics are there. Good Luck.
#3

[eluser]jbads[/eluser]
Hey thanks for your help, there were errors with that code but you helped me figure out the use of my $where variable.

Cheers.
#4

[eluser]wiredesignz[/eluser]
I'm not surprised there are errors, I said it was untested. Tongue




Theme © iAndrew 2016 - Forum software by © MyBB