Welcome Guest, Not a member yet? Register   Sign In
Making data available in ALL views
#11

(06-15-2021, 07:18 AM)SoccerGuy3 Wrote: Thanks everyone for the help. Just needed a push in the right direction. Somewhat new to CI and the community support is great.
Now that it is working/returning data, I wonder if there is a better way to return the data. In my view file I am using:
Code:
<?= siteInfo()['leads']; ?>

In my helper file:
Code:
<?php

if (! function_exists('siteinfo')) {

function siteinfo()
{
$leadModel = new \App\Models\LeadsModel();
$leads = $leadModel ->where('status != "Closed"')
->countAllResults();

$custModel = new \App\Models\CustomersModel();
$custs = $custModel ->where('status_customer != "Closed"')
->countAllResults();

$data = array(
'leads' => $leads,
'customers' => $custs,
);

return $data;
}
}
Any suggestions to make that more efficient or easier to access? Always looking to learn new ways!

Personally, I wouldn't use a helper for what you are doing. I would have the model/database instantiation in a controller that will display the view (or possibly a base controller). The $data would be passed into the view as normal.
Reply
#12

(06-15-2021, 12:48 PM)paulkd Wrote:
(06-15-2021, 07:18 AM)SoccerGuy3 Wrote: Thanks everyone for the help. Just needed a push in the right direction. Somewhat new to CI and the community support is great.
Now that it is working/returning data, I wonder if there is a better way to return the data. In my view file I am using:
Code:
<?= siteInfo()['leads']; ?>

In my helper file:
Code:
<?php

if (! function_exists('siteinfo')) {

function siteinfo()
{
$leadModel = new \App\Models\LeadsModel();
$leads = $leadModel ->where('status != "Closed"')
->countAllResults();

$custModel = new \App\Models\CustomersModel();
$custs = $custModel ->where('status_customer != "Closed"')
->countAllResults();

$data = array(
'leads' => $leads,
'customers' => $custs,
);

return $data;
}
}
Any suggestions to make that more efficient or easier to access? Always looking to learn new ways!

Personally, I wouldn't use a helper for what you are doing. I would have the model/database instantiation in a controller that will display the view (or possibly a base controller). The $data would be passed into the view as normal.

As stated back at the beginning, the helper made the most sense as I need it on every page and trying to put it into every controller just is a waste and hard to keep track of. I need the data available on every page load. Open to suggestions, but having to put it into every controller (every function?) is just not realistic for me.
Reply
#13

Another way that you can do it is by using View Cells.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#14

(06-15-2021, 08:53 PM)InsiteFX Wrote: Another way that you can do it is by using View Cells.

That's interesting. The docs are a little sparse, but if I understand correctly. I could create a new controller (or use the Base Controller) and put a function in it to pull a piece of data and return the rendered html directly. Can I return the HTML directly or do I have to create a view that will then be included in the original HTML?
Reply
#15

You can create class for such use can also be and Entity and then add a service which will return it as singleton. Much better solution that having it as helper method as the loading of data will happen only once. Then you can use it in view like this



PHP Code:
<?= service('siteinfo')->leads ?>
Reply
#16

in basecontroller i do this:

$this->renderer = \Config\Services::renderer();
$this->renderer->setData(['siteinfo'=>$siteinfo]);

so in views html file

<?=dd($siteinfo)?>
Reply
#17

Lots of options! I like many of the suggestions here. I’ll add one that hasn’t been brought up: Filters. I’ve been into injecting content that is used on many pages with an “after” Filter. Check out this example for building and inserting a custom menu on every page: https://github.com/tattersoftware/codeig...Filter.php

In your case if you always knew the place where it was going you could spend it to some div instead of using the placeholder text.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB