Welcome Guest, Not a member yet? Register   Sign In
Using the URL to determine page content without making a function for each page
#5

[eluser]boltsabre[/eluser]
Just a couple of things, if you are creating/calling

Code:
public function get_job_info($job_title){..

In your controller, you may want to consider moving it into a model (technically all DB related stuff should be in your model to adhere to the MVC structure). That way if you ever need to call the function from a different controller you don't have to "copy and paste" your function (keeps things DRY and reduces the chances of bugs and keeps your files smaller and more readable).

If you are always sure it will be uri segment 3 then there is no need to assign it to a variable, inside your function you can just do:
Code:
$query = $this->db->query("SELECT * jobs WHERE title = $this->uri->segment(3)");
This just saves on creating one extra variable that has to be committed to server memory (and make sure you escape it like CronNix said, active records would be best for a simple SELECT like this).

Or if the uri segment changes you could just do it like this:
Code:
// lets say this is your controller and you are calling a model method, and that your model is already loaded
// by just passing your uri number in the function call you don't have to assign it to a variable
$job_title = $this->jobs_model->job_title_by_uri(3);

//And lets say this was your model function
public function job_title_by_uri($uri_seg_numb){
   if($this->uri->segment($uri_seg_numb)){
      //this is checking if the uri segment actually exists
      // you may need to use isset(), or if($this->uri->segment($uri_seg_numb) != null) or something else,
      // I'm at work and cannot check on my CI installation and have forgotten, sorry!

      //but anyway, in here goes your code, again escape it using active records
      $query = $this->db->query("SELECT * jobs WHERE title = $this->uri->segment($uri_seg_numb)"
      
      //check if a result was returned, if it was return that, else return false.
   }else{
      //uri segment doesn't exist, return false
      return false;
   }
}

Also, only SELECT * (select all) if you absolutely have to. If you only need a one column from your table, just select that, again this reduces server memory requirements and will increase the speed of your queries.


Messages In This Thread
Using the URL to determine page content without making a function for each page - by El Forum - 09-06-2012, 02:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB