How to send post value to two separate functions - php

I have a form and a submit action routed to BaseController/FirstFunction, but i need to POST some of the sent values to the SecondFunction. So how can i send a POST value to other functions.
Like this example: FirstFunction has a input value with the name "$value". I want the SecondFunction to use this "$value".
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class BaseController extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function FirstFunction()
{
$value = "Test Value";
/*Insert etc.*/
}
public function SecondFunction()
{
$insertData = [
'product_id' => $value,
];
/*Insert etc.*/
}
}

Change you form action to controller/common function and you will get form data in both function FirstFunction() and SecondFunction() .
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class BaseController extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function common(){
$this->FirstFunction();
$this->SecondFunction();
}
public function FirstFunction()
{
echo '<pre>'; print_r($_POST);
}
public function SecondFunction()
{
echo '<pre>'; print_r($_POST);
}
}

No, you can not send post values to multiple functions. Instead you can do something like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class BaseController extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function FirstFunction()
{
$value = "Test Value";
$this->SecondFunction($value);
/*Insert etc.*/
}
public function SecondFunction($value)
{
$insertData = [
'product_id' => $value,
];
/*Insert etc.*/
}
}
You can refer this blog and this.
I hope this will help you.

Related

Call to undefined method in Codeigniter

I am creating a model there is no error in my case, but it give me error
Message: Call to undefined method Auth_model::chech_user()
Here is my code
Controller
class Auth extends CI_Controller
{
public function __construct()
{
parent::__construct();
$params = array(
'iteration_count_log2' => '8',
'portable_hashes' => TRUE
);
$this->load->library('hash', $params);
$this->load->model('auth_model');
}
public function login()
{
if (isset($_POST) && !empty($_POST)) {
print_r($this->auth_model->chech_user($_POST));
exit;
}
$this->load->view('auth/login');
$this->load->view('layouts/footer');
}
Model
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Auth_model extends CI_Model
{
public function check_user($params = array())
{
echo 'done';
}
}
There's a typo within the code you've provided, you're trying to call chech_user() not check_user() within the controller.
change this line
print_r($this->auth_model->chech_user($_POST));
to
print_r($this->auth_model->check_user($_POST));

Dynamic data in codeigniter

I am new in codeigniter and i am working on ecommerce template. By default whenever index() has been hit, it's showing all products in different sections (in html) and I want to make this dynamic so should I use queries in index() for get different type of records or there is any other way? This is my code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
//echo "hello world";
$this->load->view('index');
}
}
?>
First need to create database and table as per your requirement.
Then you can create model for reference check this link : https://www.codeigniter.com/userguide3/general/models.html
<?php
class Blog_model extends CI_Model {
public function get_data_first()
{
$query = $this->db->get('entries', 10);
return $query->result();
}
public function get_data_second()
{
$query = $this->db->get('entries', 10);
return $query->result();
}
}
?>
After making model go to your controller and include it like:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
$this->load->model('my_model');
$data['first_list'] = $this->my_model->get_data_first();
$data['second_list'] = $this->my_model->get_data_second();
//echo "hello world";
$this->load->view('index', $data);
}
}
?>
Then use you param in index file like :
<html>
<head></head>
<body>
<?php
if($first_list){
foreach($first_list as $each){
echo $each->my_param;
}
}
?>
<?php
if($second_list){
foreach($second_list as $each){
echo $each->my_param2;
}
}
?>
</body>
</html>
I hope this helpful for you.
In Models folder write Home_model.php file for querying database.
Suppose you have a table called 'products'.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home_model extends CI_Model{
public function __construct()
{
parent::__construct();
}
public function getProducts()
{
$this->db->from('products');
$query=$this->db->get();
$out = $query->result_array();
return $out;
}
}
The function 'getProducts' will get you all the products from 'products' table.
Now in your controller load the 'database' library and the model.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('home_model');
$this->load->library('database');
}
public function index(){
$products = array();
$products = $this->home_model->getProducts();
$this->load->view('index',$products);
}
}
In index function function of controller you can call the 'getProduct' function of model.And can pass the data to view.
Documentation link for model.
enter link description here
I hope this helps.

Calling controller/action is not working as expected?

Click link as controller/function (signout/signout) is not working .. I mean if I click link controller/action it's not going to that called function.But controller/action/action (signout/signout/signout) is working in my CI framework..
//not working
echo "<div id='menu'><ul><li>Signout</li></ul></div>";
//working
echo "<div id='menu'><ul><li>Signout</li></ul></div>";
MY CONTROLLER Signout.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Signout extends CI_Controller {
public function __construct() {
// Call the CI_Model constructor
parent::__construct();
$this->db = $this->load->database('default', true);
}
public function index() {
}
public function signout() {
$this->load->view("signout_signout");
}
}
Here I want controller/action(signout/signout) to work ! Because 2 worked method url(signout/signout/signout) is a little long ,so don't like it . How can i manage that?
Call signout() from your index() action. Change:
public function index() {
}
to
public function index() {
$this->signout();
}
or use this and eliminate signout() altogether:
public function index() {
$this->load->view("signout_signout");
}
It was working fine as i check your code.
Try test this code and see
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function welcome()
{
echo "<div id='menu'><ul><li>Signout</li></ul></div>";
echo "<div id='menu'><ul><li>Signout</li></ul></div>";
}
public function signout()
{
echo "signout";
}
public function index()
{
}
}

What is wrong with my Model and Controller?

For the life of me I cannot understand why I am receiving errors with my install of CodeIgniter and this current MVC set up.
The error I see is
Fatal error: Call to undefined method Login::users_model() in /var/www/application/controllers/login.php on line 17
Controller application/controllers/login.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$data = '';
$this->load->model('users_model');
$data['testing'] = $this->users_model()->test_user();
$this->load->view('home',$data);
}
}
Model application/models/users_model.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function test_user() {
return 'Test User #1';
}
}
View application/views/home.php
echo $testing;
No need for function bracket with model name
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$data = '';
$this->load->model('users_model');
$data['testing'] = $this->users_model->test_user();
$this->load->view('home',$data);
}
}
Replace $data['testing'] = $this->users_model()->test_user(); with $data['testing'] = $this->users_model->test_user(); in your controller.
Simply load your model inside the constructor
public function __construct()
{
parent::__construct();
$this->load->model('users_model');
}
Then simply call the relevant functions inside any controller functions like this.
public function index()
{
$data = '';
$data['testing'] = $this-> users_model-> test_user();
$this->load->view('home',$data);
}

Passing common title to all view and models CodeIgniter

I have this controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('text');
}
public function index()
{
$this->home();
}
public function home()
{
$data['title']="Somesite";
$this->load->view("view_home", $data);
}
public function blog()
{
$data['title']="Somesite";
$this->load->view("view_blog", $data);
}
public function answers()
{
$data['title']="Somesite";
$this->load->view("view_answers", $data);
}
}
As you may see $data['title'] is same for all functions, how to make it simpler, to include at the beggining and not to write it in every function, repeat again, and then transfer to view.
Is there a way to tranfer it to function?
Before construct function add this:
public $data = array();
Then in the construct function write:
$this->data['title']="Somesite";
And finally before load view add this:
$data = $this->data + $data;
Now you have same $title everywhere.
Here si simple solution and elegant for transfering one variable to all views :)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
//Class-wide variable to store stats line
protected $title;
function __construct()
{
parent::__construct();
$data->title = "Some site";
$this->load->vars($data);
}
I'm using this method in every projects.
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
//Global variable
public $outputData = array();
public $loggedInUser;
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->helper('url');
$this->load->view('users/users');
}
public function register() {
parent::__construct();
$this->load->helper('url');
$this->load->model('users_model');
//get country names
$countryList = $this->users_model->getCountries();
$this->outputData['countryList'] = $countryList;
$this->outputData['pageTitle'] = "User Registration";
$this->load->view('users/register',$this->outputData);
}
}
View file
<?php if(isset($pageTitle)) echo $pageTitle; ?>
<?php
if(isset($countryList)){
print_r($countryList);
}
?>

Categories