Codeigniter - Appending variables names to a method name to run Model function - php

Not sure if possible but what I am trying to do is pass a query string through a get request to my controller. From here I am getting the query string value using $_GET['name']. What I would like to do then is use that GET value and append it as the name of the method I will be passing to my Model to return the data I need.
This is because multiple action with pass different query string values to the same controller but then use the value to get the data from different functions within my model.
e.g.
Controller
class ControllerName extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('my_model');
}
public function index() {
$queryName = $_GET['query_string']
// e.g. $queryName = 'customer'
//Use the query string and append to method to pass to Model function
$result = $this->my_model->$queryName._get_query(); //???
//e.g. $result = $this->my_model->customer_get_query();
}
}
My_model
class My_model extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function customer_get_query() {
//...run database query and return result
}
}
Any thoughts?

The usual php should work just fine:
$result = call_user_func([$this->my_model, $queryName.'_get_query']);

Related

get array parameter from what user type in browser

how to get array parameter from what user type in browser with codeigniter3
this is my controller
class header extends CI_Controller{
public function index(){
$this->load->model('header_model');
$hasil = $this->header_model->getData();
var_dump($hasil);
}
}
and this is my model
class header_model extends CI_Model{
public function getData(){
$array = ["000118","000112","000117"];
$this->db->select('NOMOR_DAFTAR, TANGGAL_DAFTAR');
$this->db->where_in('NOMOR_DAFTAR',$array);
return $this->db->get('tpb_header')->result_array();
}
}
$array above i want to get from what user type in browser
You can use header(‘content-type application/json’) in codeigniter to return json array.
For example:
class header extends CI_Controller{
public function index(){
header("Content-type:application/json");
$this->load->model('header_model');
$hasil = $this->header_model->getData();
//var_dump($hasil);
echo json_encode($hasil);
}
}
finally i got the answer, but for note this not using codeigniter pure, there is librabry since method get there is no in codeigniter pure
and this is the answer
class Header_model extends CI_Model {
public function getData($array = []){
$this->db->select('NOMOR_DAFTAR, TANGGAL_DAFTAR');
$this->db->where_in('NOMOR_DAFTAR',$array);
return $this->db->get('tpb_header')->result_array();
}}
class header1 extends Rest_Controller{
public function index_get(){
$array[]=$this->get('NOMOR1');
$array[]=$this->get('NOMOR2');
$this->load->model('header_model');
$hasil = $this->header_model->getData($array);
var_dump($hasil);
}}

When page loads i need access variable from query string

I have controller in codeigniter that name is Product. When I access product controller, I am passing cateid variable in URL as query string parameter and getting on product controller.
I have created two methods in controller with names index and productajax . When I click on product link then controller index method executes. I need to use cateid variable value from URL query string in productajax method when page is initially loaded or controller index method being called.
class Product
{
public function index()
{
$cateid= $this->input->get('cateid'):
$this->load->view('product'):
}
public function productajax()
{
// I need to get cateid from index function
}
}
Hope this will help you :
You can do it by creating a public or private variable $cate_id in class and when index loads then
assign $cate_id variable in your index method and when you access productajax you can get cat id in productajax by using $this->cat_id
Your controller should be like this :
// Product class
class Product extends CI_Controller
{
public $cate_id;
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$cateid = $this->input->get('cateid'):
$this->cate_id = $cateid;
$this->load->view('produxt');
}
public function productajax()
{
echo $this->cate_id;
}
}
For more : http://php.net/manual/en/language.oop5.php

Laravel orm database query inside model function

I am new in Laravel. I want to make some custom functions in models which is related to database query.
Class A Extends Controller{
public function view(){
B::get_user();
}
}
Class B Extends Model{
protected $table = "user";
public function get_user(){
//Here is my database query
}
}
How can I use database query in get_user() function? I know this method:
B::table('user')->get();
You can define query scopes for adding the query on the model as:
public function scopeUser($query)
{
return $query->where('some_field', 'some_value');
}
Then you can use it in you controller as:
B::user()->get();
Docs

codeigniter global array for a model

In my controller I want to declare a global variable which will always fetch all areas from my db
currently I'm passing $data['dropdowns']
in all methods in my class
{
$data['dropdowns']=loading some other model method
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
the thing is I want to now send $data['area'] to all the views without having to declare it again and again in each method
$data['area']= $this->area_model->get_all_locations();
You want to add global variable , but as per my suggest to use global function to use any where to using to send parameter, so please check below my code.
Note : please load your model in application/config/autoload.php file
This is simple demo :
controller
{
$data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name');
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name,user_name');
$this->load->view('commons/header',$data);
}
Your model
function get_records($table_name,$field_name)
{
$this->db->select("$field_name");
$this->db->from("$table_name");
$query=$this->db->get();
return $query->result_array();
}
create a base_controller and placed in application/core
class base_controller extends CI_Controller
{
public $area = array();
function __construct()
{
// Call the Controller constructor
parent::__construct();
$this->get_area();
}
public function get_area() {
$this->load->model('area_model');
$this->area= $this->area_model->get_all_locations();
}
}
now $this->area is available in all controller which extends base_controller and all common functionality you can put here
class homepage extends base_controller{
function __construct()
{
// Call the Controller constructor
parent::__construct();
}
public function index()
{
$data = $this->area; // call this wherever u need
$this->load->view('commons/header',$data);
}
}
importantly $this->area; one can use directly inside view
Create a helper for your custom functions
Eg: custom_helper.php then load your custom helper in autoload.php
In custom_helper.php, create a function for getting area.
if (!function_exists('get_area')) {
function get_area() {
$CI = & get_instance();
$area= $CI->area_model->get_all_locations();
return $area;
}
}
You can call get_area() in your views without declaring in controllers..

error calling member function of alleged non-object in Codeigniter controller

I get the error
Fatal error: Call to a member function retrieve_products() on a non-object
The controller is:
<?php
class Cart extends CI_Controller { // Our Cart class extends the Controller class
public function _construct()
{
parent::_construct(); // We define the the Controller class is the parent.
$this->load->model('Cart_model'); // Load our cart model for our entire class
}
function index()
{
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
}
}
The model is:
<?php
class Cart_model extends CI_Model {
function retrieve_products(){
$query = $this->db->get('products'); // Select the table products
return $query->result_array(); // Return the results in a array.
}
}
I want to say that your call
$data['products'] = $this->cart_model->retrieve_products();
Should be:
$data['products'] = $this->Cart_model->retrieve_products();
Ie: uppercase "C" in cart_model
Maybe we're using different versions (I have 1.7.2), but to declare a model, CI_ does not appear. My working code has the equivalent of:
class Cart_model extends Model
Also, the class should capitalized:
$this->Cart_model->retrieve_products();
(instead of)
$this->cart_model->retrieve_products();
I think its your typo error you have spelled construct function as _construct rather than __construct thats why codeigniter considers it as a function rather than a class constructor and model loading is limited to only that function.

Categories