Thursday 15 August 2013

php - CodeIgniter - What kind of variable can work during controller is called? -


How does the controller work first I want to ask? Assume I have a controller:

  & lt ?? Php if (define ('basepay')) (exit 'no direct script entry is allowed'); Class Home CI_Controller {Public Static $ groupId; Public Function __ Composition () {} Public Function Index () {$ this- & gt; Load-> Model ('product_model'); $ Data ['new_products'] = $ this- & gt; Product_model- & gt; Get_new_products (); $ Data ['featured_products'] = $ this- & gt; Product_model- & gt; Get_featured_products (); $ Data ['sale_products'] = $ this- & gt; Product_model- & gt; Get_sale_products (); $ Data ['template'] = 'index'; $ Data ['title'] = 'home'; $ This- & gt; Load-> Views ('master', $ data); } Public Function Group ($ groupId) {$ this- & gt; Load-> Model ('group_model'); $ This- & gt; Load-> Model ('product_model'); $ GroupId = array_pop (explosion ('-', $ groupId)); Auto: $ groupId = $ groupId; $ Data ['groupName'] = $ this- & gt; Group_model- & gt; Get_group_name ($ groupId); $ Data ['allGroupWithQuantity'] = $ this- & gt; Group_model- & gt; Get_group_with_quantity (); $ Data ['product'] = $ this- & gt; Product_model- & gt; Get_products_by_group ($ groupId); $ Data ['template'] = 'group'; $ Data ['title'] = $ data ['group name'] - & gt; group name; $ This- & gt; Load-> Views ('master', $ data); } Public function tests () {echo seft: $ groupId; }} / * End of the file home.php * / / * location: ./application/modules/front/controllers/home.php * /   

when I enter http: / Type / localhost: 8080 / ci / and http: // localhost: 8080 / ci / television> and then http: // localhost: 8080 / ci / test / < , I get white screen if the first time is called the controller (use the method in the controller), and the second time, I think the controller does not need to reload, so by group method , I set the value for $ groupId Not to test method I can easily get to, but I can not when I call on the test method, the controller is loaded again.

Secondly, I want to ask, how to pass the $ group through another method? Remember! $ GroupId does not store in controller, I get the URL.

You need to call your controller in this way:

 < Code> http: // localhost: 8080 / ci / home / group / 12   

Where 12 is your grouped and home name Your Controller is in a group function to store your $ groupId Use session:

  $ this-> CI- & gt; Session-> Set_userdata ('group id', $ group id); Use this line of code in your $ groupId exam to load:  
  $ groupId = $ this-> CI- & gt; Session-> Userdata ('group');   

There is another solution (as requested), although this is not the best practice, to create a static class named a group:

  Lt ;? Php square group {public fixed $ groupId = 12; ?} & Gt;   

Then your home controller will look like this:

  include_once 'group.php'; Class Home CI_Controller {Public Function __ Construct () {} Public Function Index () {$ this- & gt; Load-> The model ('product_model') is expanded; $ Data ['new_products'] = $ this- & gt; Product_model- & gt; Get_new_products (); $ Data ['featured_products'] = $ this- & gt; Product_model- & gt; Get_featured_products (); $ Data ['sale_products'] = $ this- & gt; Product_model- & gt; Get_sale_products (); $ Data ['template'] = 'index'; $ Data ['title'] = 'home'; $ This- & gt; Load-> Views ('master', $ data); } Public Function Group ($ groupId) {$ this- & gt; Load-> Model ('group_model'); $ This- & gt; Load-> Model ('product_model'); $ GroupId = array_pop (explosion ('-', $ groupId)); Group :: $ groupId = $ groupId; $ Data ['groupName'] = $ this- & gt; Group_model- & gt; Get_group_name ($ groupId); $ Data ['allGroupWithQuantity'] = $ this- & gt; Group_model- & gt; Get_group_with_quantity (); $ Data ['product'] = $ this- & gt; Product_model- & gt; Get_products_by_group ($ groupId); $ Data ['template'] = 'group'; $ Data ['title'] = $ data ['group name'] - & gt; group name; $ This- & gt; Load-> Views ('master', $ data); } Public function tests () {echo group :: $ groupId; }}? & Gt; Check this site for more information on controllers:  



No comments:

Post a Comment