The Real coding part.
Now that the files and database are ready lets move on to coding..
Lets start with the Model, we called our model 'shoutmodel.php' so now with the CodeIgniter conventions in mind we will create our model.
Obviously the model file will start and end with the php tags'' as its a php file, all our coding work goes inside these php tags.
Lets define our class-
we call our class Shoutmodel as the file name is shoutmodel.php.(I am not sure but i guess its a CodeIgniter convention to class the class name same as the file name with the first letter capital.)
That's what a basic CodeIgniter model looks like with a constructor. A constructor is a function with the function name same as the class name and is loaded automatically when the class is loaded unlike other functions.
Now we shall add our OWN FUNCTIONS to communicate with the database we created.
FIRST FUNCTION
We name our first function as 'getshout'.It will do just what it says, it will get shouts out of our database.
function getshout(){The line '$shouts = $this->db->get('shouts');' is a ACTIVE RECORD STATEMENT which works same as the sql statement 'SELECT * FROM shouts', yes it will get all information from our database i.e -'shouts'.
$shouts = $this->db->get('shouts');
if($shouts->num_rows() > 0){foreach ($shouts->result() as $shout){
$data[] = $shout;
}
return $data;
}
}
The line 'if($shouts->num_rows() > 0)' Means that the code inside the if block {} will be run only if there are more than 0 or atleast one row OR shout/comment/chat in our database table.
'{foreach ($shouts->result() as $shout){$data[] = $shout;}' Lets break this into 2 parts -
- 'foreach ($shouts->result() as $shout)' this is the php foreach statement
- '{$data[] = $shout;}'This is the foreach code block. which defines a variable $data and the '[ ]'means its an array. so the whole thing '$data[] = $shout', stores $shout data, that is, as in the first line $shouts retrieves data from database using ACTIVERECORD and then in the Second lines first part '$shouts->result() as $shout' the result of $shouts is set as $shout. Then this $shout variable data is stored in the $data variable as an array. Its a little complex, hope you understood.
NOW ADDING OUR SECOND FUNCTION -
We call our second function 'newshout', and again it does what it says. it adds new shouts to our database
this ones pretty simple
function newshout(){
$name=$_POST["name"];//sets the variable $name to user input in the form input 'name' (form to be created in the VIEW file in STEP 6)
$shout=$_POST["shout"];//sets the variable $shout to user input in the form input 'shout' (form to be created in the VIEW file in STEP 6)
$email=$_POST["email"];//sets the variable $email to user input in the form input 'email' (form to be created in the VIEW file in STEP 6)
$ins_shout = array(
'name'=>$name,//associates the $name variable with the database table field 'name'
'shout'=>$shout,//associates the $shout variable with the database table field 'shout'
'email'=>$email //associates the $email variable with the database table field 'email'
);
$this->db->insert('shouts',$ins_shout);//ActiveRecord to insert the information gathered. The first parameter of insert is the table name and the second parameter is the data to be inserted, in our case it is in array form but it can be in object form too.[if you don't know what a parameter of a function is then- in insert('shouts',$ins_shout) , insert is the function and inside the ( ) are the parameters separated by commas.]
}
That should explain everything..
in case you want to directly copy paste, here's the code again without comments
class Shoutmodel extends Model{
/*constructor function*/
function Shoutmodel(){
parent::Model();
}
function getshout(){
$shouts = $this->db->get('shouts');
if($shouts->num_rows() > 0){foreach ($shouts->result() as $shout){
$data[] = $shout;
}
return $data;
}
}
function newshout(){
$name=$_POST["name"];
$shout=$_POST["shout"];
$email=$_POST["email"];
$ins_shout = array(
'name'=>$name,
'shout'=>$shout,
'email'=>$email
);
$this->db->insert('shouts',$ins_shout);
}
}
?>
Well that's all for our Model file.. lets move to Step 5...
Leave a comment for help !
ssaas
ReplyDeletekl
ReplyDeleteffsfs
Deletesdasdasd
ReplyDelete