Welcome Guest, Not a member yet? Register   Sign In
Get URL Data?
#1

[eluser]RandyCram[/eluser]
Hello,

So I am working on a rather large project and It isn't my first project with CI but my largest so far and I seem to be stuck here.

I need to get data from the url and then use that data to get info from a database.

Example:
Mysite.com/users/profile/2

I want to get that 2 data from the url. the users and profile part are defined using the controller no problem there. I just don't know how to get that 2 information.

using it in the database is no problem I just put a query in the controller to use the '$row->id;" to get the data to use in my view.

Whats the best method of doing this with CodeIgniter?

Thanks,
Randy
#2

[eluser]bobbob[/eluser]
Normally you would have your users controller with a function in it called profile which takes one parameter namely the 2.


users.php should have this in it:
Code:
function profile($id)
{
//do what you need $id repesents the 2
}
called be Mysite.com/users/profile/2
#3

[eluser]Dennis Rasmussen[/eluser]
/controller/method/param1/param2/...
All parameters can be retrieved in your method as bobbob wrote above.
If you want to make sure that the variable $id is numeric only, you can do a check manually in your method or you can specify a route requiring a number in the 3rd segment of your URL.

And as a last note, you can read more about getting segment data here:
http://ellislab.com/codeigniter/user-gui...s/uri.html
#4

[eluser]RandyCram[/eluser]
Alright, i got that working and I seem to be getting an error now with my database query..

Error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/users.php

Line Number: 59

My Code:
Code:
function profile($view_id) {
        
        $query = $this->db->query('SELECT * FROM profile_info WHERE user_id = "$view_id"');
        $row = $query->row();
        
            $data['main_content'] = 'view_profile';
            $data['profile_id'] = $row->profile_id;
            $this->load->view('includes/template', $data);
    }

Line #59 in my code snippet is the query.
#5

[eluser]umefarooq[/eluser]
hi if your user_id is integer type then don't put double quotes around $view_id

Code:
$query = $this->db->query("SELECT * FROM profile_info WHERE user_id = $view_id");

try the above code hope will work for you.
#6

[eluser]RandyCram[/eluser]
umefarooq,

I have tried that and I get an error like this:

Quote:A Database Error Occurred

Error Number: 1054

Unknown column '$view_id' in 'where clause'

SELECT * FROM profile_info WHERE user_id = $view_id
#7

[eluser]umefarooq[/eluser]
check are you getting proper value in $view_id just echo it or not if not then try

Code:
$this->uri->segment(3);

just put you segment number
#8

[eluser]RandyCram[/eluser]
Yup, I'm getting the right one.

If i put in the echo i get the number that I have on there.
#9

[eluser]umefarooq[/eluser]
then try this active record query
Code:
$this->db->select('*')->from('profile_info')->where(array('user_id'=>$view_id));
  $row =  $this->db->get()->row();

  to check either your query is right or wrong
  echo $this->db->last_query();

will work
#10

[eluser]RandyCram[/eluser]
That worked.

Thanks for your help everybody.




Theme © iAndrew 2016 - Forum software by © MyBB