How to send data/variable from one method to another in the same controller in CodeIgniter?

I have 2 functions/methods in the same controller in CodeIgniter like this:

public function _get_page_settings() { $page_settings = $this->page_model->get_page_settings(); $data['ps'] = $page_settings; } public function registration() { $this->_get_page_settings(); $this->load->view('page_registration', $data); }

The first method _get_page_settings() grabs some stuff from db and store it in a variable ps inside data array.

I want to use this method in many other methods inside this controller.

So, how to send data correctly to registration() because this code above is not working OK. It throw this error in my view: Message: Undefined variable: data on line 70 which is this line: $this->load->view('page_registration', $data);


I have finally figured it out. You can do it like this:

public function _get_page_settings() { $data['ps'] = $this->page_model->get_page_settings(); $this->ps = $data['ps']; } public function registration() { $this->_get_page_settings(); $this->load->view('page_registration', $this->ps); }

And to output it in the view e.g.:

<?php echo $this->ps->page_name; ?>


$data simply has different visibility scope, this is not CodeIgniter specific. You can solve this in two ways:

  • Make $data a class property ($this->data)
  • Make _get_page_settings() to return $data and assign it to a variable when calling it.
Category:php Time:2012-03-26 Views:0

Related post

  • Pass variables of a function to other function in a controller in codeigniter? 2011-11-20

    I have a controller that has the next functions: class controller { function __construct(){ } function myfunction(){ //here is my variable $variable="hello" } function myotherfunction(){ //in this function I need to get the value $variable $variable2

  • How to send data using webrequest class method? 2012-02-16

    In my application I send data to a PHP server. I got code from the msdn website. void SendPost() { var url = ""; // Create the web request object HttpWebRequest webRequest = (HttpWebRequest)W

  • Send data with HTTP delete method 2011-12-19

    Is it possible to send data with the HTTP delete method because when I'm trying to send data with GWT request builder, on the server, it not finding the data!! --------------Solutions------------- HTTP/1.1 does not define the meaning of the request p

  • Using a Class Variable vs Sending Local Variable to Functions/Methods 2011-04-04

    When is it good form to push a local variable to a function/method as a parameter, rather than using a class variable in place of the function/method variable. For instance, I can have a function: int DoSomething(int var) { if(var == -1) return 0; }

  • Access data from callback function at the class level in a controller in CodeIgniter 2011-10-11

    I have a callback function that I'm using to validate a submitted video URL based on YouTube or Vimeo APIs (depending on which URL they submitted). The callback function, as part of the validation, checks to validate their submission based on informa

  • Switching controllers and sending data in codeigniter 2011-08-08

    I have to devellop an internal web based application with codeigniter and I need to chain different forms (generate upon data choosen with previous form). Right now, I tried to use form validation in the same method of the controller but the chaining

  • Sending Data Between 2 Android Phones Via Wifi 2011-03-03

    How can i send data (variable or media) from 1 phone to another using wifi? And how would i know when it's done transferring? TY!!!! --------------Solutions------------- Have a look at the AllJoyn peer-to-peer library. It probably has all the feature

  • IOS4 - Send data using UDP socket 2011-05-02

    I've just begun developing with networking on the iPhone. I need to develop a simple app for talking to a Wifi router. The thing is, the wifi box communicates over UDP. I've dug around quite a bit, but all the sample code I've seen so far, have examp

  • A want a software visits a url from multiple proxies automatic and send data using POST Method 2011-03-03

    I want to suggest me a software visits a url from multiple proxies automatic and send data using POST Method ( The value of the POST Variables must be everytime different). For example the first time will be "Dim Ra" The second time will be "somethin

  • How to send data via post method to a php file in c++? 2011-07-06

    I am very new to c++. Recently I am trying to pass some data to a php file in c++ via the post method. I have tried a tutorial from MSDN send Function. The code is as follows: #ifndef UNICODE #define UNICODE #endif #define WIN32_LEAN_AND_MEAN #includ

  • how to use xmlhttprequest object's post method for sending data 2011-07-12

    I want to send data that is in a java script variable to the server.the variable is in a method that is executing when I click a button on the web is the code written in that method for sending data. var xmlhttp; if (window.XMLHttpRequest)

  • Sending Data Using Post Method Without Form 2011-07-20

    I want to send data to an API, data include simple variables username, pasword, email etc. Now problem is this that i want to send data to this using POST method, i searched this issue on google, every one is saying go for CURL. Now What is curl? is

  • Doest struts 2 accept "POST" method to send data from javascript to an Action server? 2011-12-16

    [I tried to simplifie the code to better understand the questions]When I tried to send data from a web side client to an ActionClass in Struts 2 like: jQuery("#bedata").click(function(){ //Function for button "bedata" var postData = "SOME DATA TO SEN

  • How do we send data via GET method? 2009-12-04

    I am creating a HTTPS connection and setting the request property as GET: _httpsConnection = (HttpsConnection), Connector.READ_WRITE); _httpsConnection.setRequestMethod(HttpsConnection.GET); But how do I send the GET parameters? Do

  • send data to JS file without using global variables 2011-03-29

    I need to send data in a HTML page to a script file that is loaded in that page. The simplest way i can think of is to use a global variable which is defined in the page and accessed in the script file. We all know global state is bad, so i started t

  • Does POST method send data as querystring? 2011-05-03

    I'd like to know if the POST method on HTTP send data as QueryString, or if it use a special structure to pass the data to the server. In fact when I analyze the communication with POST method from client to server (with Fiddler for example) I don't

  • how to get data to a servlet which is send using httpcommunicator with post method 2011-05-20

    I am trying to send data using httpcommunicator class. here is my code. public String postData(String address,String dataToBePosted) throws MalformedURLException,IOException,ProtocolException{ /** set up the http connection parameters */ HttpURLConne

  • JavaScript variable when sending data via jQuery $.post() function 2011-09-04

    I have a problem when I want to send input data via jQuery's $.post. I have this input: <input id="mydata" value='<?php echo $data; ?>' > and I want to send its data via jQuery's $.post() function. This $.post function runs, but it does't

  • Send data to server using jQuery $.ajax Post method 2011-10-26

    I use jQuery Ajax to send data to server. I set post type as POST but data always get send using GET method. How to send data using Post method? $.ajax({ type: "POST", dataType: 'jsonp', url: '', data: { 'CUrl'

Copyright (C), All Rights Reserved.

processed in 0.575 (s). 13 q(s)