Welcome Guest, Not a member yet? Register   Sign In
I always do the same things, how do I make it a one time ? (function ? library ? class ? helper ?)
#11

[eluser]Dagobert Renouf[/eluser]
that's smart, thank you ;-).
#12

[eluser]Sumon[/eluser]
To reduce code i use one technique which might help you save your time. For that i use a common class (inside system\application\libraries)
Code:
class Custom_config extends Controller
{
  function Custom_config()
  {
     //parent::Controller();    //Strictly Prohibited.
  }
  function RestoreControl($_POST)
  {
    global $Data;
    foreach($_POST as $Key=>$Value)
    $Data[$Key] = $Value;
  }
}
here is my controller use these functions
Code:
class Photo extends Controller
{
  function CreateAlbum()
  Custom_config::InitializeTextBoxControl(array('album_title' , 'album_location' , 'album_description' , 'UploadMessage' ,  'Message'));
   //album_title, album_location, album_description, UploadMessage, Message = these are view text boxes name
  //Say here is your validation codes......
  //after validation for invalid case you need to restore your control values
  global $Data;
  Custom_config::RestoreControl($_POST); // repopulated common fields value
}
I think it might be helpful and please lets discuss further.
#13

[eluser]xwero[/eluser]
aaaahhhh a global, that is a no-no.
#14

[eluser]Référencement Google[/eluser]
globals with CI???
#15

[eluser]Sumon[/eluser]
Yes Global variable is supported in CI and i feel it's very much useful. Let me view you some line of codes... May be i can get some useful tips from you guys...
Code:
//THis is my custom config class (inside system/application/libraries
function DefaultConfigForCurMember()
{
  global $Data;
  $Data['PageTitle']="Shopno Dinga";
  $Data['BadWords'] = array('darn', 'shucks', 'golly', 'phooey', 'sucks', 'suck', 'fuck');
  $Data['BadWordReplace'] = "Beep!";
  $Data['LoggedIn']=($this->session->userdata('MemberId')=="")?0:1;
  ($this->session->userdata('TotalProfileViews')=="")?0:$this->session->userdata('TotalProfileViews');
  Custom_config::InitializeCalendar();
  
  $Data['mem_id'] = $this->session->userdata('MemberId');
  $Data['Tot']=0;
  $mm_id = $Data['mem_id'];
  if ($mm_id != '')
  {
    $Query=mysql_query("SELECT * FROM member_cv where member_cv.member_id = $mm_id");
    $Data['Tot']=mysql_num_rows($Query);
  }
}
In every controller constructor i call this function and automatically get few global values which are really useful for my project. For example page title which is assigned here. So that header template display at least project name.

So finally is global variable useful to use in CI?
#16

[eluser]xwero[/eluser]
Short answer no Smile

PageTitle, BadWords and BadWordReplace can be put in a custom config file.
LoggedIn and mem_id should stay in the session. If the MemberId is false the user isn't logged in.
The data from the database should be in a model.

Basically your are doing it wrong. The reason why globals are a no-no it because it's an error-prone way to use variables in different functions.




Theme © iAndrew 2016 - Forum software by © MyBB