How traverse model data from Controller in codeigniter - codeigniter

I am using codeigniter. From a model i have fetched some data in the controller using the following codes:
$data['english'] = $this->question_model->getRandomQuestions('english', 2);
So I know to access the data from view i would use the following code..
foreach($english as $eng){
echo $eng['columnName'];
}
But I want to travase the data in the controller doing some of my calculation. But how can I do that. I used the save above code in controller but it is then showing error.
The error message is as below :
Undefined variable: english
How can used those obtained data in controller using foreach loop. Any help would be appreciated...
thanx

Since the response is added in $data['english'] in your controller, then you would do:
foreach( $data['english'] as $i => $eng ) {
$data['english'][$i]['columnName'] = 'change with whatever';
}

Related

How to merge two queried results in Laravel Eloquent

I have a single form which I am using to create and edit information.
I am using Form::model to show all the data in the edit form which are queried from the database. Now I had to add another form part whose data is being stored in a different table. But I need to show those data during edit in the same form. I have tried to put two parameter in the form:model which did not work or I am doing it wrong.
{!! Form::model([$employee_data,$edu_info],['route'=>['employee.update',$employee_data->id],'method'=>'put','files'=> true]) !!}
Then I tried to merge the queried data in my controller like this:
public function edit($id)
{
$edit_info['title'] = 'Edit User';
$edit_info['country'] = Country::all();
$employee_basic = Employee::withTrashed()->where('id',$id)->first();
$employee_edu = EmployeeEdu::withTrashed()->where('employee_id',$id)->first();
$employee_all_data = $employee_basic->merge($employee_edu);
$employee_all_data->all();
$edit_info['employee_data'] = $employee_all_data;
return view('admin.employee.edit',$edit_info);
}
This did not work either. I get the following error:
Call to undefined method Illuminate\Database\Query\Builder::merge()
How can I achieve my intended result?
EDIT: I tried to use ->get() instead of ->first(), In this case I do not get the error but my merge does not work as when I dd($employee_all_data) it gives me only the value of $employee_edu.
Try this:
$employee_all_data = $employee_basic->toArray() + $employee_edu->toArray();

How to use custom pagination in laravel 5.2 using LengthAwarePaginator

How to use custom pagination in laravel 5.2 using Illuminate\Pagination\LengthAwarePaginator , i think this gives more flexibility than other class according to thier doc. But couldn't figure it out how to do that . So i need help on this . Kindly guide me .
Code in controller :
public function index(Request $request )
{
$data=array();
//**** fetch data starts
$countries_db = DB::table('location_country as lc');
$countries_db=$countries_db->select(DB::raw('lc.id,lc.country_name,lc.country_3_code,lc.country_2_code,lc.lat,lc.lng,lc.published'));
$pagi_country=$countries_db;
//**** fetch data ends
//***** pagination code starts
$pagi_country = $pagi_country->paginate(3);
$pagi_country->setPath('country/');
$data['pagi_country']=$pagi_country;
$data['useinPagiAr']=$useinPagiAr;
//***** pagination code ends
return view('admin.country.countrylist', $data);
}
Code in View
we will get data from this variable $pagi_country passing it to foreach loop , and printing data. To print the pagination link following code is needed
echo $pagi_country->appends($useinPagiAr)->render();

Codeigniter best approach to load data from model

Im trying to load data from a model if the data from a previously loaded model request is_numeric. I have written my code like this, though it doesn't work and i sort of get why. But i dont know how to get the content from the data['schedule_item'].
$this->load->model('schedule_model');
$this->load->model('partners_model');
$data['schedule_item'] = $this->schedule_model->getAll();
if(is_numeric($schedule_item['featured_partner1']) {
$data['featured_partner1'] = $this->schedule_model->get_partner($schedule_item['featured_partner1']);
}
You are getting the wrong variable you have assigned the model values on the index of $data array not to $schedule_item so it is undefined $schedule_item will be accessible in view only when all the indexes of $data is converted to the variables you should try to do this like
$this->load->model('schedule_model');
$this->load->model('partners_model');
$schedule_item=$this->schedule_model->getAll();
$data['schedule_item'] =$schedule_item;
if(is_numeric($schedule_item['featured_partner1']) {
$data['featured_partner1'] = $this->schedule_model->get_partner($schedule_item['featured_partner1']);
}
OR
$this->load->model('schedule_model');
$this->load->model('partners_model');
$data['schedule_item'] =$this->schedule_model->getAll();
if(is_numeric( $data['schedule_item']['featured_partner1']) {
$data['featured_partner1'] = $this->schedule_model->get_partner($schedule_item['featured_partner1']);
}

Codeigniter reusable sections

I have a table of data from a database that I want to display on various pages of my website. Ideally, i would like to just have an include or something that will go off and get the data, and return the html table. The html and data will be identical everytime I need to use this.
I wanted to know the best way of doing this
Thanks
EDIT
If this helps, something similar to a Django "inclusion" custom tag...for any django developers reading
you need to pass the variable $data to the view method.
This is your code:
function load_my_view(){
$this->load->model('my_table');
$data['my_results'] = $this->my_table->my_data();
$this->load->view('my_view');
}
Please change it to this in order to load the $data into the view:
function load_my_view(){
$this->load->model('my_table');
$data['my_results'] = $this->my_table->my_data();
$this->load->view('my_view',$data);
}
You should use a function in a model to fetch the data you need. Your controller calls the model function and sends the returned information to a view. You don't need to use traditional php includes with Codeigniter. I recommend a review of the user guide. It's very good and will tell you all the basic stuff you need to know to develop with CI. But to get you started, you watn to use Models, Views, and Controllers. Your url will tell CI what controller and function inside that controller to run. If your url is
http://www.example.com/my_controller/load_my_view
Then CI will do what is inside the load_my_view function in the my_controller controller. function load_my_view in turn instantiates a model "my_table" and runs a database query, returns information that the controller sends to the view. A basic example follows:
Your model
class my_table extends CI_Model{
function my_data(){
$this->db->select('column_1,column_2,column_3');
$this->db->from('my_table');
$query = $this->db->get();
if($query->num_rows()>0){
$result = $query->result();
}
else{
$result = false;
}
return $result;
}
}
Your controller
class my_controller extends CI_Controller{
function load_my_view(){
$this->load->model('my_table');
$data['my_results'] = $this->my_table->my_data();
$this->load->view('my_view');
}
}
Your View
<ul id = "my_db_results">
<?php foreach($my_results as $result):?>
<li><?php echo $result->column_1." : ".$result->column_2." ( ".$result->column_3." )";?></li>
<?php endforeach;?>
</ul>
It looks like a good oportunity to use cache: http://codeigniter.com/user_guide/libraries/caching.html
Ok, so here is one thing that worked for me, but its definitely not perfect.
I created a view in which made a call to the model getting the data then put the data into a table. This way, I only have to include this view to put the table anywhere.
I understand this completely ruins the point of having a MVC framework, but hopefully it demonstrates what I want to do...and it works

CodeIgniter problem retrieving and displaying data from DB

Here is my function. It is very simple.
function load_data() {
$query = $this->db->query('SELECT * FROM configurations WHERE username="' . $this->session->userdata('username') . '"');
return $query;
}
My controller has this line of code:
$data['query'] = $this->configurations->load_data();
In my view, I tried:
foreach($query->result_array() as $row) {
echo $row->first;
}
But I get an error that I am trying to get a property of a non-object. Isn't the query being returned from the model as an object?
You should use $query->result_array, row_array, result, or row otherwise your returning the object intead get the results. Check the CI manual.
You are returning the results as array and using $row as object!
Try:
foreach($query->result() as $row) {
Refer.
Try changing $this->load->model('login/configurations', '', TRUE); to $this->load->model('login/configurations', 'configurations', TRUE); and see if it works. If it does, it is related to how you're extending your model class. That is, it would be related to what name you give inside configurations.php.
Hope this helps.
Your undefined variable error tells me that your query might not be running correctly. To diagnose...enable the profiler to check your query.
From the documentation:
$this->output->enable_profiler();
Permits you to enable/disable the
Profiler, which will display benchmark
and other data at the bottom of your
pages for debugging and optimization
purposes.
To enable the profiler place the
following function anywhere within
your Controller functions:
$this->output->enable_profiler(TRUE);
When enabled a report will be
generated and inserted at the bottom
of your pages.
Your query will be shown at the end of
the page
Double check your query syntax to make sure it is running properly, and
your code in it's current state is returning an object of objects and arrays:
print_r($query)
CI_DB_mysql_result Object
(
[conn_id] => Resource id #29
[result_id] => Resource id #39
[result_array] => Array
(
)
[result_object] => Array
(
)
[current_row] => 0
[num_rows] => 3
[row_data] =>
)
you need to access the individual properties to get to the actual data.
$result=$query->result();
print_r($result);
should do it
Had this issue before - basic problem is that if the Query returns no result set then $query->result_array() == NULL
NULL is not a list and can't be processed in a foreach.
I have found that checking for this condition solves this issue.
From your question, you are getting the result as a pure array (result_array) but printing the data as object ($row->first).
result() or row() is returning in object but result_array is returning in array.
change your view part like,
foreach($query->result_array() as $row) {
echo $row['first'];
}
or if you want to use an object then,
foreach($query->result() as $row) {
echo $row->first;
}
Generating Query Results in Codeigniter

Resources