grocery Codeigniter loading view with template - php

i have recently downloaded grocery from their site and i am having a small problem in loading views with a template ..
as in controller the function of loading view look like this
function _example_output($output = null)
{
$this->load->view('groceryView.php',$output);
}
function index()
{
$this->_example_output((object)array('output' => '' , 'js_files' => array() ,'css_files' => array()));
}
what i want to try to implement is this
$data['main_content'] = 'groceryView';
$this->load->view('dashboardTemplate/template',$data);
i dont know if i do this how can i pass output ... as it gives me an error if i do this
$data['output'] = 'output';

Well you can load multiple views . You need to modify _example_output. If you have header and footer seperately you could load it like this.
function _example_output($output = null)
{
$this->load->view('header.php');
$this->load->view('groceryView.php',$output);
$this->load->view('footer.php');
}

Related

Retrieve data from database in codeigniter

I am new to Codeigniter so I'm having some difficulties I want to retrieve data from the database and load it in the view but I couldn't figure out how.
Here is my model:
public function viewClientWaterwell(){
$userid = get_client()->userid;
$waterwells = $this->db->select('*')
->from($this->table)
->where('ww_client_id', $userid)
->get()->result_array();
return $waterwells;
}
Here is my clientController:
public function viewClient()
{
$data['waterwells'] = $this->waterwell_model->viewClientWaterwell();
$this->data($data);
$this->view('waterwells/clients/viewClient');
$this->layout();
}
I want to pass some data in a view to the client side but I can't figure out how. I think there is some problem with my model but I cannot figure out what. I'm new to this and will appreciate your feedback. Thanks!
You just need to use second argument to pass data to your template or view file
Here is example to start
Inside your controller function
public function viewClient()
{
$data = array( 'myvar' => 'I love stackoverflow' );
$this->load->view('waterwells/clients/viewClient', $data);
}
And inside your template or view file you can access them by their name.
<?php echo $myvar; ?>

codeigniter uri segment merging?

I am a little confused. The following is my nav controller. i have been getting my page layout this way.
nav
Home
Contact
Test
Main Controller
public function index()
{
if($this->uri->segment(3))
{
$content = $this->uri->segment(3);
}
else
{
$content = 'home_v';
}
$data = ['title'=> 'home', 'main_content' => "$content"];
$this->load->view('tempelate',$data);
}
since, all those content in my nav is inside view, its not a problem but what if i have a folder inside a view and want to send it to that controller? am i here all clear asking the question? because i am myself confused.
I want to sent another uri->segment into the same function.
Cat
category here is the folder name. so now what i basically want to add into the function is .
$folderName = $this->uri->segment(4);
Didn't understood much from the question. But still if you have a folder inside the view folder, and want to pass the $data to the view inside that folder, you can simply do,
$this->load->view('foldername/viewname', $data);
EDIT:
public function index()
{
$content = 'home_v';
if($this->uri->segment(3))
{
$content = $this->uri->segment(3);
}
$foldername = '';
if($this->uri->segment(4))
{
$foldername = $this->uri->segment(4).'/';
}
$data = ['title'=> 'home', 'main_content' => "$content"];
$this->load->view($foldername.$content, $data);
}

cakephp load component on fly

I want to load component on fly in cakePHP
In this example, loading dynamically RequestHandler component for json response
public function xyz() {
$this->components[] ='RequestHandler';
$this->Components->load('RequestHandler');
$this->RequestHandler->initialize($this);
$abc = array("hello","world");
$this->set('abc', $abc);
$this->set('_serialize', array( 'abc'));
}
error generated on initialize function shows undefined.
AMENDMENT FOR MORE CLEAR PICTURE:
public function xyz() {
$this->components[] ='RequestHandler';
$this->RequestHandler = $this->Components->load('RequestHandler');
$this->RequestHandler->initialize($this);
$abc = array("hello","world");
$this->set('abc', $abc);
$this->set('_serialize', array( 'abc'));
}
I also tried
public function xyz() {
$this->RequestHandler = $this->Components->load('RequestHandler');
$abc = array("hello","world");
$this->set('abc', $abc);
$this->set('_serialize', array( 'abc'));
}
I can't use component like this #$this->RequestHandler->getTime(); because cakephp automatic handle json respone.
When I hit just above code using http://disecake.localhost/resources/xyz.json
{"code":500,"url":"/resources/xyz.json","name":"View file "
/var/www/disecake/app/View/Resources/xyz.ctp" is missing."}
When I use
public $components = array( 'RequestHandler');
in my cotroller than output
{"abc":["hello","world"]}
I think now question is more clear.
There is literally a section in the CakePHP book called "Loading Components on the fly".
CakePHP 2.x:
http://book.cakephp.org/2.0/en/controllers/components.html#loading-components-on-the-fly
CakePHP 3.x:
http://book.cakephp.org/3.0/en/controllers/components.html#loading-components-on-the-fly
(and it's done different than how you're attempting)

Laravel : Variable layout for a single view

I wanted to know if there's a way to use variable layout for a single view in laravel.
I have a view of the login section. I want to show the login view in a lightbox by calling it via AJAX. I was thinking of using a different layout for the login view when it is called through ajax.
Something like this :
if($_GET["from"] == "ajaxLink") {
// use layout1
} else {
// use layout2
}
This obviously doesnt work. :)
Is there any way i can do this??
Thanks.
2 ways.
1. Blade layout
Controller:
$layout = Request::ajax() ? 'layout1' : 'layout2';
$data = array('layout' => $layout);
return View::make('index', $data);
View:
#layout($layout)
//rest of the code....
2. Controller layout
public function action_index()
{
$this->layout = Request::ajax() ? 'layout1' :'layout2';
$this->layout->nest('content', 'index');
}

How to Retrieve Image Files from Database Using Yii Framework?

Our Yii Framework application has the following defined as part of the UserProfileImages model:
public function getProfileImages($param, $user_id) {
if(isset($param['select']) && $param['select']=='all'){
$profile_images = UserProfileImages::model()->findAllByAttributes( array( 'user_id'=>$user_id) );
} else {
$profile_images = UserProfileImages::model()->findByAttributes( array( 'user_id'=>$user_id) );
}
return $profile_images;
}
How would I wire up the above snippet to a widget in my view to return all the images for a given user?
Bonus Question: Which image rotator do you suggest to render the above?
In your view file, add something like this, assuming that your controller specified $user_id:
$this->widget('UserProfileImagesWidget', array(
"userProfileImages" => UserProfileImages::getProfileImages(
array("select" => "all"),
$user_id
),
"user_id" => $user_id
));
Depending on your MVC philosophy, you could also retrieve the userProfileImages data in the controller and pass that data to your view.
Define a widget like this:
class UserProfileImagesWidget extends CWidget {
public $user_id;
public $userProfileImages = array();
public function run() {
$this->render("userProfileImage");
}
}
Finally, in the userProfileImages.php view file, you can do something like this:
if(!empty($this->userProfileImages)) {
// Your display magic
// You can access $this->user_id
}
As a side note: You might want to change the order of your parameters in getProfileImages. If $user_id is the first parameter, you can leave out $params completely in case you don't want to specify any.

Categories