Logic behind data flow from controller to view/model codeigniter - php

My question is, what method/technique/process is adopted to send data from controller to view/model and view to controller.
i want to understand the logic behind data flow in codeigniter.
Every type of help will be appreciated.
$this->load->view('sap_tab',$result);
$data = $this->input->post();

get data from controller to view
$data = array(
'title' => 'Client',
'bookings' => $this->db->get('bookings')->result_array()
);
$this->load->view('sap_tab',$data);
Now you can access $data in sap_tab.php as $title & $bookings
view to controller
There are two ways to do it
By URL parameter
Session

Related

Codeigniter large form insertion

I've a form with 120 fields to insert into the DB. The form is inserting fine and the approach I used is below:
I'm fetching all the fields from the view as below in the controller and passing the array($postdata) to the model file to insert.
**View**
$postdata = array(
'firstname' => $this->input->post('firstname'), //1st field
'lastname' => $this->input->post('lastname'), // 2nd field
'age' => $this->input->post('age'),
....
....
'test' => $this->input->post('test') // 120th field.
);
$this->Form_Model->insertdata($postdata);
**Model:**
function insertdata($data = array()) {
$sql_query = $this->db->insert('form_insert', $data);
redirect('Form');
}
My question is Is there any better way to insert. This approach feels bit repetitive.
If you simply want to get an array of all the data submitted, you can do it like this:
$postdata = $this->input->post();
This means, all the data submitted from the form will be there in this array.
And if you want to remove any particular element from this array, you can use unset().
Say for example, you may have named your submit button as "submit_btn" like this:
<input type="submit" name="submit_btn" />
then this value would be there in the above returned array. You can remove it like this:
$postdata = $this->input->post();
unset( $postdata['submit_btn']);
Btw, I have a couple of suggestions. The logic part is done in a Controller(you referred it by mistake as View). A View is simply for the displaying. And the Model is for the database communication.
Also, it would always be better to do some validations on the input that you received from the User through form submissions. We may don't even know what data they are sending!
And move that redirect() you used in the Model to the Controller from where you were trying to call that insertdata() method. In that Model, you just return a value (true or false or maybe something else) and do the business logic inside the Controller
You were kind of mixing up everything. That's why I thought to give you some pointers to help you.
Hope it helps :)

How did you bind datas to a view in Codeigniter ( likes View Composers in laravel)?

If you have data that you want to be bound to a view each time that view is
rendered,a view composer can help you ...
This task can be easily archived in laravel, but I am now using Codeigniter, there is no view composers things. What I have done now is, I create a custom view method, just like below
public function view($page,$params=null,$return=false)
{
// Every time I invoke this method, $nav will be passed to 'navigation' view.
$nav=[
'user' =>'Adam'
];
//return the views as view_partials instead of displayed
$view_partials = array(
'navigation' => $this->obj->load->view('partials/nav',$nav,true),
'page_content' => $this->obj->load->view($page,$params,true)
);
// load layout with the view_partials which contain bound data.
$this->obj->load->view($this->_layout,$view_partials,$return);
}
This method returns views as 'string', it can not works with json or complex page.... Thank you.
Ok, lets make this simple
$this->load->view(path_to_htmlpage, $bound_data, FALSE);
or
$this->load->view(path_to_htmlpage, $bound_data); // by default 3rd param is FALSE
this will render html page
If you want to get html page as a string, set 3rd parameter to TRUE
$html_string = $this->load->view(path_to_htmlpage, $bound_data, TRUE);
check https://ellislab.com/codeigniter/user-guide/general/views.html
public function view($page,$params=null,$return=false)
{
$nav['user']='adam';
$data['navigation']= $this->load->view("partials/nav",$nav,true);
$data['page_content'] = $this->load->view($page,$params,true)
$this->load->view('your_page',$data);
}
in yor view page ie.your_page you can use variables $navigation and $page_content to display the pages

Create a form for dynamic models - Yii

I have to create a form for a set of models, but unfortunately, I don't know how to do.
My first idea is to create a single form and a controller action which renders the view containing the form. But, this idea let me face an error. I create an action like this :
public function actionAddInfo($id){
$participant = Participant::model()->find('id_participant = ' . $id);
$info = InfoComp::model()->findAll('id_event = ' . $participant->id_event);
// here I must save the model if submitted
$this->render('addInfo', array('model' => $info));
}
In fact, the relationship in my models Participant, Evenement is below :
'idEvent' => array(self::BELONGS_TO, 'Evenement', 'id_event');
When accessing the variable $info in the view,
echo count($info);
I got the exception :
Undefined variable $info
This exception let me ask whether it is possible to proceed like that. I need your help. Else, can somebody suggest me another way to proceed ?
You are sending the variable with name model and you are trying to access it with name $info..
All you need to change is this:
$this->render('addInfo', array('info' => $info));

Codeigniter: Accessing a array in view

I am completely new in the codigniter i just started day befire Yesterday i am having a problem
Here is my snippet of my controller code
$allcalldetails_array = array(
'id' => $row->id,
'customer_id' => $row->customer_id
);
$this->session->set_userdata('logged',$allcalldetails_array);
I want to iterate the $allcalldetails_array in my views please tell me the way to do this
i tried iterating logged but could not get anything .
If i am printing the array in my views like print_r($allcalldetails_array); but this is also disappointing me .Please help me to get back on track .
Thanks
Its not necessary for session variables to be parsed via controller, access it on view directly:
$logged = $this->session->userdata('logged');
print_r($logged);
If you would like data to be send to your view as variables, you should add the corresponding data to your view load.
Controller:
$logged = $this->session->userdata('logged');
$this->load->view('viewfile', $logged);
View
print "logged id:".$id;
best regards.
Jonas
To get the data from session your need to do the following:
// controller logic
$logged = $data['logged_for_view'] = $this->session->userdata('logged'); // since 'logged' is the name you set it to
// more controller logic
$this->load->view('view_name', $data);
// in view
var_dump($logged_for_view); // this is the key of the $data variable you assigned for the view
Regarding the CodeIgniter's documentation :
Data is passed from the controller to the view by way of an array or
an object in the second parameter of the view loading function.
Then you could use something like :
// Controller side
$data['allcalldetails_array'] = array(
'id' => $row->id,
'customer_id' => $row->customer_id
);
$this->load->view('your_view', $data);
// View side
print_r($allcalldetails_array);
// Loop through your array
foreach ($allcalldetails_array as $detail){
// Do something with your $detail.
}

sending data from model to controller in codeigniter 1.7.2

I am receiving $fb_data[uid] i.e Facebook UID from my model and I want to put this variable's value into my controller where the signup process is taking place
passing fields in controller like this:
$sql_data = array('student_fname' => $student_fname, 'student_sname' => $student_sname, 'student_org' => $student_org, 'student_title' => $student_title, 'student_uid' => ?
I am confused how to insert the Facebook UID value here at my controller..
You need to have a function in your model which returns your $fb_data. In the controller you have (if not already done) to load the model
$this->load->model('model_name');
Then you can access your model function from the controller by calling
$this->model_name->function_name();
The section 'loading a model' in the CI documentation explains it again.
Do like this :
First create one method in controller :
Ex : In controller
function fbcontent() {
$this->load->model('your model name');
$content = $this->model_name->fbaction();
}
and In model
function fbaction(){
$sql_data = array('student_fname' => $student_fname, 'student_sname' => $student_sname, 'student_org' => $student_org, 'student_title' => $student_title, 'student_uid' => ?
return $sql_data;
}

Categories