How to show query results in single page header.php in codeigniter? - codeigniter

I have a lot of pages in my project on PHP Codeigniter. I have one header.php page .. In header.php I need to get the categories in mysql query ... how can I do this for every page ?.
I need to do a mysql query for each header.php on each page .. I want to once query for category.
thanks for help..

If you don't want to query DB every HTTP request, store first request query result in session variable or into local storage variable. MY_Controller pseudo code would be something like
if ( ! $this->session->userdata('header_categories') {
$header_categories = $this->Category_m->get_all();
$this->session->set_userdata('header_categories', $header_categories);
}
// otherwise data is already there and no need for DB query
In this session approach you could loop $this->session->userdata('header_catogories') in view file i.e. header.php
As you can see I mentioned MY_Controller class that should be present and every controller should extend that class. You need to set this part of code in constructor of MY_Controller.

Related

defining routes with parameters in laravel

in Laravel i want to do a page with a search box and a form (the route could be /products)
I want to retrieve information using search box typing id from a db and populate the form.
I request datas with the route for example /products/{id}
But in the controller i use the same function products($Request request) with if the id exists do something if no do other things, or there are two different functions?
Thx
Please go through the Laravel Resource Controllers.
To show list of products i.e. /products, create a index() method. To show a specific product i.e. /product/{id}, create show() method.
Probably is better use the same function, if id exist, return the page with the values filling the form, if not returns the same page but with a message showing that product doesn't exist

Accessing Auth'd user data throughout

I am looking to access the Auth'd users' data throughout my views. This information needs to come from a DB query so I can join in various other tables to get the data I need.
The view structure I am working with is as follows: (layout->dashboard). "Layout" being the generic html bits, parent. and "dashboard" being the page specific content.
My first attempt at passing data from the controller to the view outlined that I was only able to access the variable from the child view (dashboard) and not (layout) which I did presume beforehand. My question is, what is the best way to pass around user data, from a DB query, to any view I need it in.
In this one scenario, it is using a peice of data to retrieve the users avatar in the nav bar, found in "layout".
Many Thanks,
Laravel provides a quick way to scaffold all of the routes and views you need for authentication using one simple command:
php artisan make:auth
to make auth interface.
Check Laravel Doc:authentication
And then, try below...
Query DB like this...
//Controller
$users = DB::table('users')->get();
return view('layout', compact('users'));
After get the users collection, then send to view Blade(in the html file) like this...
//`Layout.blade.php` as View file like this..
<div class="container">
#foreach($users as $user)
{{$user->name}}
{{$user->"any you want"}}
#endforeach
</div>
#displaying-pagination-results
Enjoy coding~!! :)

October CMS overriding list query

In backend I have a list of records. And this list displays all records of this type. It is possible to override this query?
In controller I have Eloquent query and i grab some data from database,
and i set this data in $query_data variable. Howe push this data in list view?
unfortunately, I didn't get exactly your point of telling override query, if you mean that set some condition in fetching data from database in Eloquent you have to use where for example in the example below we have users which their gender is male:
$users=User::where('gender','male')->get();
for set more condition you can do like this:
$users=User::where([['gender','male'],['status',1]])->get();
and for showing the variable in your view you can use compact method:
return view('YOUR BLADE FILE NAME',compact('users'));
hope be helpful bro

Cakephp 3.6. How to call a helpers query from a controller?

I have on my homepage a small search form with a list of categories.
I use a helper to query the database to get all categories. This is fine and works great.
Now I want to update the categories select with an updated list of categories based on a performed search.
That means, I would need to call query inside the helper with a given $variable to run a new query and to return new data.
But this is my concern now.
I can't call a helper from within the PagesController, right?
So what is the right way to
- have a helper call to use on the fly in all views selecting DB Data?
- address an ajax call to a controller and to use the query from the helper?
Any advice will be highly appreciated!
Thanks!
Short answer:
You code needs to be moved in a Table class and not in a Helper class, if you need it both in the Controller and the View.
Long Answer:
The code you have created in your helper needs to be moved in the relevant Table class and that will fetch the data.
Then in both your view and the controller you will use the new method you created in your Table class.
For example in your CategoriesTable class you can add a method called:
public function fetchCategoryData(){
//TODO: Custom code that will fetch the category data like...
//return $this->find();
}
public function updateCategoyData($args){
//TODO: Custom code to update your category data
}
Now, in either the View or the Controller you can use this
$Categories = TableRegistry::get('Categories');
$Categories->fetchCategoryData();
//or
$Categories->updateCategoryData();

Magento: Where would I place that functionality?

I have a Magento form with a button and some fields. The button click calls a controller and in the controller I query the DB (using fetchAll) and create a csv file from the results.
Whats the best place to store the DB action and the CSV file creation? In a model maybe?
Thanks!
I would create a model, a resource model and a resource model collection for the data you need to export. In the collection I would define functionality that queries data from one or many (if necessary) tables . In the model itself I would create a function that fetches that collection and saves it into a CSV file. The controller action would contain only the model initialization and a call to the export function.

Resources