Welcome Guest, Not a member yet? Register   Sign In
Page URLs based on database results
#1

[eluser]Unknown[/eluser]
Hello all, I am currently building my first CI website and would like to know if its possible to generate pages dynamically based on database results.

For example;

I have a admin page which contains;
Code:
$group = $this->session->userdata('group');

  $query = $this->db->query('select username from users where group= "' . $group. '"');
  foreach ($query->result() as $row)
  {
   echo "<li>" . $row->username . "</li>";
  }

I then want to be able to change that list into hyperlinks and navigate to mysite.com/admin/$username and have it display content relevant to each user.

I'm sure this is possible, and more than likely I have missed the section in the user guide - would someone be able to point me in the right direction?

Thanks!
#2

[eluser]CroNiX[/eluser]
Probably a Route or _remap() would be easiest.
#3

[eluser]boltsabre[/eluser]
Sure, make sure you cleanse your DB variable (userdata('group')) first incase someone has hacked your session, or better use active records which will automatically cleanse your query for your, then when you echo it out do this:
Code:
foreach ($query->result() as $row)
  {
   echo "<li><a href='".base_url()."admin/view_user/".$row-&gt;username . "'>".$row->username."</a></li>";
  }
This will create your hyperlinks. You could echo this a little different and make use of the CI function anchor(), but you get the idea!

And then you in your "admin" controller, you would have a function called "view_user", which would access a "variable" called "$username, like this:
Code:
class Admin extends CI_Controller{
... // constructor goes here if you have one

   function view_user($username = null){
      if($username == null){
         echo "No username was passed to me"; //do whatever you want to do here
      }

      //else we have a username, now you can do whatever you want.
      //more then likely you'll want to load your model if you haven't already, and then call a funtion that
      //gets the user details by their username. Make sure it is unique!
   }
}
#4

[eluser]Unknown[/eluser]
[quote author="boltsabre" date="1361873710"]Sure, make sure you cleanse your DB variable (userdata('group')) first incase someone has hacked your session, or better use active records which will automatically cleanse your query for your, then when you echo it out do this:
Code:
foreach ($query->result() as $row)
  {
   echo "<li><a href='".base_url()."admin/view_user/".$row-&gt;username . "'>".$row->username."</a></li>";
  }
This will create your hyperlinks. You could echo this a little different and make use of the CI function anchor(), but you get the idea!

And then you in your "admin" controller, you would have a function called "view_user", which would access a "variable" called "$username, like this:
Code:
class Admin extends CI_Controller{
... // constructor goes here if you have one

   function view_user($username = null){
      if($username == null){
         echo "No username was passed to me"; //do whatever you want to do here
      }

      //else we have a username, now you can do whatever you want.
      //more then likely you'll want to load your model if you haven't already, and then call a funtion that
      //gets the user details by their username. Make sure it is unique!
   }
}
[/quote]

Thanks for that, boltsabre.

Is any additional routing required? I have implemented your suggestions as below;

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller{
// constructor goes here if you have one

function view_user($username = null){
  if($username == null){
   echo "No username was passed to me"; //do whatever you want to do here
  }

  $data['title']= 'User Administration';
  $this->load->view('header_view',$data);
  $this->load->view('user_view.php', $data);
  $this->load->view('footer_view',$data);
}
}
?&gt;

However the links are still returning 404 Smile




Theme © iAndrew 2016 - Forum software by © MyBB