I'm trying to code the system in that things are separated using "userId". That means, products or services are separated on the basis that user who add them in the system.
For that I'm trying to pass the userId from user controller to service and product controller. But I got an error that undefined variable userID.
$data['id'] = $this->session->userdata('user_id');
pass this in create() function of department controller.
i'm loading user model in __construct() function also.
Thanks in advance.
You can do it using two ways.
1) By Helper - simple create a custom helper file and create a function inside helper file and inside function add get userid code and then using function name you can access this userid anywhere in whole project.
2) Using By Require Controller Class -- if you want to access user id in another controller so i will explain with you by example.
Ist Controller - Test.php
here i defined a variable as a public and this variable contains userid value.
2nd Controller - Test2.php
here we will require Test.php file
require('Test.php file path');
and then create object and then we can access Test.php file variables and methods inside Test2.php file.
But i will suggest you first way it`s very easy way and multiple advantages.
Hope it will work for you.
Related
Two or more loaded controllers with the same name login models are not working.
E.g I have two models from same name one is login_model and other is in admin -> login_model.php then I want to load both in a single controller.
How can I do that?
try this
$this->load->model('admin/login_model' , 'myinterest');
The second parameter (optional) is used to call the method.
ex:
$this->myinterest->get_users();
You can do this like this:
$this->load->model("login_model","login_model");
$this->load->model("admin/login_model","admin_login_model");
You need to place admin model in another folder, say in admin folder
so your code will be like below
$this->load->model('login_model');
$this->load->model('admin/login_model');
This will call both model in same controller
You can use this as well:
$this->load->model("admin/login_model","login_model");
Trying to list a bunch of addresses with gmap.
Code in controller has this initialization steps:
$this->load->library('GMap');
$this->gmap->GoogleMapAPI();
$this->gmap->setMapType('map');
Was using the addMarkerByAddress in the same function as below and it was working:
$this->gmap->addMarkerByAddress($address,$ordername, $ordertitle, $tooltip, $icon_leaf);
When I try the same code in the view file it fails. I understand how to pass data to the views for display purposes (using the $data[] variable) but how do I get the gmap instance from the main file so it can be referenced in the view?
You can pass the object through
$data["gmap"] -> $this->gmap
Or you can create a Helper with the function/s you need, when you load the helper in the controller, you can access it without problem in the view.
Your view must not access controller's methods/variables/objects.
I wanted the functionalities of view files to run in controller file also.
For example, I wanted $this->escapeHtml() which runs in view file alone to run in controller through some means like $this->...->escapeHtml()
Is this possible? Kindly help.
You need to get the ViewHelperManager and extract the EscapeHtml helper. This is a one example how to do it from the controller:
$viewHelperManager = $this->getServiceLocator()->get('ViewHelperManager');
$escapeHtml = $viewHelperManager->get('escapeHtml'); // $escapeHtml can be called as function because of its __invoke method
$escapedVal = $escapeHtml('string');
Note that it is recommended to escape and display the output in the view scripts and not in the controller.
I'm really beginner to codeigniter I'm working on CI since last 2 weeks. During this period I have created many views.php files, some controllers.php files and some models.php files
Now I want start a new website project.
What should I do. Should I delete all files of my controllers, views and models, etc., and download another codeigniter and start from the beginning?
You should check the documentation of codeigniter for help but just to give you a quick start ill explain how to create your first codeigniter project.
Installation
1 Download the codeigniter framework from http://ellislab.com/codeigniter
2 upload it in root directory of your website or local apache server directory.
Creating your codeigniter project.
In codeigniter your controller will handle the url requests and load appropriate model and views. So the first step is to create your controller.
1 Creating your controller: go to Applications->controllers and there you will find a built in controller called welcome.php.
This controller loads a view welcome_message.php which is inside Application->views.
You can use this controller or create your own.
To create your own controller create a new php file myfirstcontroller.php and extend a class with same name from CI_Controller.
Note that the name of the file and your class name should be the same. the index function is the default function that will be called when you make a request to the controller
class myfirstcontroller extends CI_Controller {
public function index(){
$this->load->view("myfirstview");
}
}
so when you request this controller through yoursite/index.php/myfirstcontroller
it will load a view called myfirstview.php which will reside inside applications->views.
Go ahead and create this file in applications ->views.
2 To pass data from controller to view you will send an array to the view
class myfirstcontroller extends CI_Controller {
public function index(){
$data['name']="My first application.";
$this->load->view("myfirstview",$data);
}
}
3 You can access this variable in view
echo $name
and it will output your variable
3 you use models you have to create a file inside applications->models and call it from controller and it will return the result in the form of array.
You can look at the documentation for further help.
Hope this helped you to get started with codeigniter.
The user guide is inside your download library.
You can also view it in http://ellislab.com/codeigniter/user-guide/
Good luck!!!
Here is Phil Sturgeon's article on how to do multiple site on one CI instance, in here he explains 2 ways of doing it and describes pros and cons.
http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter
But in his latest articles he has told what happened to modular separation.
http://philsturgeon.co.uk/blog/2010/03/modular-separation-codeigniter-2
Can you tell me how to use a controller for home page because i'm trying to put a model's data in home.ctp (homepage view) with
<?php $this->user->find() ?>but it returns
Notice (8): Undefined property:
View::$user [APP\views\pages\home.ctp,
line 1]
You should check out the cookbook; it has some solid CakePHP tutorials, at http://book.cakephp.org/
You haven't really provided alot of information, but my guess is your Controller uses a model 'User', and you're putting $this->user->find() in your view, when it should be in your controller. In your controller's action you'll want/need to do something like this...
Users_Controller extends AppController {
function index() {
$arrayOfUsers = $this->User->find(...);
$this->set('users', $arrayOfUsers);
}
}
You can then - in your View - access 'users' like so:
pre($users);
... since you used the Controller method set() to send a variable $users to the view.
All you really need to do is create a new controller if that's the direction you want to go. If this is the only statement you have that requires data access, it might be worth faking it in only this method of the PagesController. For example, one of my projects' homepages is 99% static save for a list of featured events. Rather than move everything out to a new controller or even loading my Event model for the entire PagesController (where it's not needed), I just applied this solution in PagesController::home():
$attractions = ClassRegistry::init ( 'Attraction' )->featured ( 10 );
Works great. If your page is more dynamic than mine, though, it may well be worth routing your homepage through a different controller (one that is more closely related to the data being displayed).
The default controller for the home page i.e home.ctp view, is located in /cake/libs/controller/pages_controller.php. This controller can be overriden by placing a copy in the controllers directory of your application as /app/controllers/pages_controller.php. You can then modify that controller as deem fit i.e. use models, inject variables to be used in the home page etc