• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
adding data to the database

#4
(02-17-2015, 11:55 AM)Ben Wrote: Thanks for your quick reply RobertSF,

$autoload['libraries'] = array('database' );//param 'database' added
$autoload['model'] = array('site_model');

Can you tell me where  '$this->load->database' and '$this->load->model('site_model') ' should be placed? I am very new to this.

You're welcome, and I'm glad your application started to work. Since you are autoloading the necessary libraries and models, you don't need to use $this->load->database, etc. Autoloading does that for you.

But then why wasn't the application working? I suspect you set up your database to require data in all fields, so when you submitted the entry without a title, the database rejected it. Turning on errors should let you know next time when it happens.

But if you didn't use autoload, where would you place the $this->load statements? The answer is anywhere before you actually used the functionality loaded. You would have to use $this->load->database before you used $this->db->get('data'), for example. However, the place where you would generally place them is in the class constructor. The constructor is a special function that runs when you create a new instance of a class. Your controllers and models are classes.

Codeigniter creates instances of your controllers and models behind the scenes, so you don't have to. Otherwise, without Codeigniter, you would have to use code like $site_model = New Site_model to create an instance of Site_model and then use code like $site_model->add_record($data) to write a new record to your database.

Codeigniter does not automatically create constructor functions for you, but if you are autoloading libraries, models, etc., you don't have to use constructors. The purpose of the constructor is to initialize the object created from the class file with specific data or to set it up to start working. If autoloading does that for you, then there's no need to create constructors.[/font][/color]

But suppose you had a particular library that you used only with one controller, and all your other controllers didn't use it. In that case, it would make sense to load that library for that one controller through that controller's constructor. Why? Because if you autoload it, it autoloads for every controller, whether it needs it or not, and that's kind of a waste of time, memory, etc.

Consider spending some time going over the PHP documentation as well as looking up some PHP tutorials about classes, objects, and methods. Learning a little about how it's done from scratch, that is to say, without Codeigniter, will put you on firmer ground and give you a greater appreciation for Codeigniter. Pretty soon you won't be so new at this. Smile
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply


Messages In This Thread
adding data to the database - by Ben - 02-16-2015, 09:48 PM
RE: adding data to the database - by RobertSF - 02-16-2015, 11:07 PM
RE: adding data to the database - by Ben - 02-17-2015, 11:55 AM
RE: adding data to the database - by RobertSF - 02-17-2015, 02:39 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.