Welcome Guest, Not a member yet? Register   Sign In
Session variable needs to change all DB queries.. any ideas ?
#5

[eluser]boltsabre[/eluser]
Well, that is a doozie of a pickle. I'm not great with CI/coding in general (well, lets just say I'm not a natural like some, but I can get stuff done with some playing around), so other people may have a better suggestion?

I'd consider re-organising your model and controller, seems like your trying to do too much in one function - make it more modular. First in your model I'd make one function for your first view (which lists all the categories (with the count of associated companies -> Animal and Pets (24)).

Code:
public function getCategories(){
   // sql statement here to get categories and their company count
   return $data
}

And a controller function to get this model data and pass it to a view
Code:
public function index(){  //because it's your main view, you can just use index, or name it something else if you want
   // load model, actually you have this in the constructor, so that's fine there.
   // call model function
   $data[categories] = $this->model->getCategories;
   $this->load->view('mainView', $data);
}

And in your view, just loop thru your $categories variable to generate your 'select' menu, using the category id as the value of each option. This way when your form gets posted to another controller you have the category id. Okay, so user selects a category, and they get another 'select' menu yes? with all the sub categories of the main category? Am I following this correctly????

Well, user selects main category, you need another controller to handle this
Code:
pucblic function processMainCategorySelection(){
   // do form validation here, basically check that you have a value from your main category select menu, and assign post value to a variable, $mainCatId
   // now pass this value to another model function
   $data['getSubCat'] = $this->modelName->getSubCategories($mainCatId);

   // pass sub cats to view and load
   $this->load->view('subCatView', $data);
}

And the model function to get the sub categories
Code:
public function getSubCategories($catId){
   // sql here, ie. $data "SELECT * FROM subCatTable WHERE mainCatId = ".$catId;
   return $data;
}

So you get the idea, breaking it down into many smaller functions. Not sure at what part the user must select the city, but where ever it is, just pass it from the form to your controller, validate it in your controller, and then pass it to your model to build your sql statement.

Look, its really late here, maybe I missed the point completely, and my code above is crap, it's all just pseudo so you get an idea of the logic of breaking it down into smaller chunks/functions, and it also helps to keep all your db logic in the model, rather than half in the controller like you have above. Hope it's of some help. I'll check back tomorrow after a good night sleep.


Messages In This Thread
Session variable needs to change all DB queries.. any ideas ? - by El Forum - 07-14-2011, 04:09 PM



Theme © iAndrew 2016 - Forum software by © MyBB