CodeIgniter - Can not run $this->input->post(‘inputName’); in Modular Extensions - php

As my title, I tried call that method but I got an error:
Fatal error: Call to a member function post() on a non-object in C:\xampp\htdocs\cifirst\application\modules\front\controllers\shopping.php on line 11
If I create a controller not in module, that method I can use very easy but in this case can not (everything code in method below can not run). This is my code:
public function add_to_cart() {
$data = array(
'id' => $this->input->post('productId'), // line 11
'name' => $this->input->post('productName'),
'price' => $this->input->post('productPrice'),
'qty' => 1,
'options' => array('img' => $this->input->post('productImg'))
);
$this->load->library('MY_Cart');
$this->cart->insert($data);
//redirect($_SERVER['HTTP_REFERER']);
//echo $_POST['productId'].'-'.$_POST['productName'];
}
And this code doesn't work too:
public function __construct() {
$this->load->library('cart');
$this->load->helper('form');
}
I'm using XAMPP 1.8.1, CodeIgniter 2.1.3 and newest MX. Please help me!

When you're using CodeIgniter functions outside of controllers, models, and views you need to get an instance of Codeigniter first.
class MyClass {
private $CI;
function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('cart');
$this->CI->load->helper('form');
}
public function someFunction() {
$var = $this->CI->input->post("someField");
}
}

If you are calling:
$this->input->post('productId');
inside controller than the problem is with your constructor declaration or your class name
Your construct part should contain code like this:
Class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->CI->load->library('cart');
$this->CI->load->helper('form');
}
public function add_to_cart()
{
$data = array(
'id' => $this->input->post('productId'), // line 11
'name' => $this->input->post('productName'),
'price' => $this->input->post('productPrice'),
'qty' => 1,
'options' => array('img' => $this->input->post('productImg'))
);
}
}
It will work fine if you are calling from helpers function or any other classes than you should do something like this:
function __construct()
{
parent::__construct();
$this->CI->load->library('cart');
$this->CI->load->helper('form');
$this->CI =& get_instance();
}
public function add_to_cart()
{
$data = array(
'id' => $this->CI->input->post('productId'), // line 11
'name' => $this->CI->input->post('productName'),
'price' => $this->CI->input->post('productPrice'),
'qty' => 1,
'options' => array('img' => $this->CI->input->post('productImg'))
);
}

Related

Trying to extend a class to overwrite a function PHP

Ok, I have the parent class from a plugin I'm trying to extend:
class OsBookingHelper {
public static function get_statuses_list(){
return array( LATEPOINT_BOOKING_STATUS_APPROVED => __('Approved', 'latepoint'),
LATEPOINT_BOOKING_STATUS_PENDING => __('Pending Approval', 'latepoint'),
LATEPOINT_BOOKING_STATUS_PAYMENT_PENDING => __('Payment Pending', 'latepoint'),
LATEPOINT_BOOKING_STATUS_CANCELLED => __('Cancelled', 'latepoint'));
}
}
My code to extend follows: It's not working for some reason....
if ( ! class_exists( 'OsNuuCustomAppointmentStatusHelper' ) ) :
class OsNuuCustomAppointmentStatusHelper extends OsBookingHelper {
function __construct(){
parent::__construct();
}
public static function get_statuses_list(){
return array( LATEPOINT_BOOKING_STATUS_APPROVED => __('Approved', 'latepoint'),
LATEPOINT_BOOKING_STATUS_PENDING => __('Pending Approval', 'latepoint'),
LATEPOINT_BOOKING_STATUS_PAYMENT_PENDING => __('Payment Pending', 'latepoint'),
LATEPOINT_BOOKING_STATUS_CANCELLED => __('Cancelled', 'latepoint'),
LATEPOINT_BOOKING_STATUS_COMPLETE => __('Complete', 'latepoint'));
}
endif;
Any ideas? I'm totally at a loss with this...
Oh, before I forget. There's a parent class that is instantiated in another file that calls the file containing my code using include_once

Variables in Trait

I have created a trait in Laravel.
In myconstruct I am calling a setting('value') - This is provided by the qcod/laravel-app-settings package.
But inside my methods when I reference $this->token or $this->org_id it returns NULL
I know the values are set, as they're showing in the config and are also set correctly in the Database.
My PHP code is :
<?php
namespace App\Traits;
use Illuminate\Support\Facades\Log;
trait PropertyBaseTrait
{
private $org_id;
private $token;
private $is_set;
public function __construct()
{
$this->org_id = setting('propertybase_org');
$this->token = setting('propertybase_token');
}
public function base_url($method)
{
return 'https://pb-integrations-api-staging.herokuapp.com/api/v1/'.$method.'/'.$this->org_id.'';
}
public function post_lead($data)
{
$lead_data = array(
'first_name' => '',
'last_name' => '',
'email' => '',
'phone1' => '',
'phone2' => '',
'phone3' => '',
'address' => '',
'city' => '',
'state' => '',
'zip_code' => '',
'country_name' => '',
'landing_page' => '',
'search_term' => '',
'referral_source' => ''
);
$object_type = 'lead';
$action_type = 'create';
dd($this->token);
$endpoint = $this->base_url('messages');
$this->post_data( $endpoint, $object_type, $action_type, json_encode($data));
}
The problem is that you have construct in your trait and maybe in your class where you are using this trait.
possible scenario:
class MyClass {
use MyTraitWithConstructor;
public function __construct(){
...
}
}
In this case trait constructor doesn't work.
What You can do?
You can rename trait construcor like this:
class MyClass {
use PropertyBaseTrait {
PropertyBaseTrait::__construct as private __prConstruct;
}
public function __construct(){
$this->__prConstruct();
...
}
}
Avoid writing constructor in traits. That's what I can say.
Instead, you can make them as normal method, then just call it in your class constructor.
Trait
trait Bar
{
public function init()
{
$this->org_id = setting('propertybase_org');
$this->token = setting('propertybase_token');
}
}
Class
class Foo
{
use Bar;
public function __construct()
{
$this->init();
}
}

Pass values to external class from controller in codeigniter

I have a project in codeigniter in which I have to integrate a payment gateway. The payment gateway integration code file is a separate php file. I want to pass some values to the file.
The file looks like the below code(with lots of functions-I reduces to post here)
class NetworkonlieBitmapPaymentIntegration
{
$networkOnlineArray = array('Network_Online_setting' => array(
'MKEY' => "GgjhJHh467HGGjjj",
'MID' => '123456789',
'amount' => '1000',
'name' => 'ADCB',
'currency' => 'PHP',
));
}
$networkOnlineObject = new NetworkonlieBitmapPaymentIntegration($networkOnlineArray);
I tried to make it as a library and as well as another controller but no luck.
how can I pass values to this file
It is better to create a seperate common functions helper inside helper folder.
commonfunctionshelper.php
<?php
function paymentFun($mkey,$mid,$amount,$name,$currency)
{
$networkOnlineArray = array('Network_Online_setting' => array(
'MKEY' => $mkey,
'MID' => $mid,
'amount' => $amount,
'name' => $name,
'currency' => $currency,
));
}
$networkOnlineObject = new NetworkonlieBitmapPaymentIntegration($networkOnlineArray);
}
?>
Now in your Controller load helpers inside your consturctor
ExampleController.php
<?php
class User_controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('commonfunctions_helper');
}
public function check()
{
//calling common functions helper
paymentFun($mkey,$mid,$amount,$name,$currency);
}
}
This is how you can achieve it.

Is it possible to print all called classes and functions in php?

If I have this example :
class A {
public function test(){
echo 'a';
}
}
class B {
public function test(){
echo 'b;
}
}
class C {
public function test(){
(new A())->test();
}
}
(new A())->test();
(new B())->test();
(new C())->test();
I want to get array with all called functions and classes, something like:
[
'A' => [
'function' => 'test',
'count' => 2,
'miliseconds' => 20,
'miliseconds_each' => [5, 15],
],
'B' => [
'function' => 'test',
'count' => 1,
'miliseconds' => 25,
'miliseconds_each' => [25],
],
'C' => [
'function' => 'test',
'count' => 1,
'miliseconds' => 30,
'miliseconds_each' => [30],
]
]
Notice: I want solution for whole framework not just bad workaround for this small example.
I would do like this, Make a base class and extends to update your analysis.
<?php
class Base{
static $data;
public static function analysis(){
return this::data;
}
function update($function){
if(isset(Base::$data[$function])){
Base::$data[$function] = ['count'=>Base::$data[$function]['count']+1,'function'=>'test'];
}else{
Base::$data[$function] = ['count'=>1,'function'=>'test'];
}
}
}
class A extends Base{
public function test(){
$this->update('a');
echo 'a';
}
}
class B extends Base{
public function test(){
$this->update('b');
echo 'b';
}
}
class C extends Base{
public function test(){
(new A())->test();
}
}
(new A())->test();
(new B())->test();
(new C())->test();
print_r(Base::$data);
?>
I know you can update code to get miliseconds and miliseconds_each
Live demo : https://eval.in/879998

Error Fatal error: Call to a member function insert() on a non-object

I'm using a callback after inserting in the database, to pass values to this other table, i think the method i used its fine. But it gives me this error when its inserting to the other table in the database, probably in my model, or something in the controller im kind of clueless in this error, can someone please help me.
Before someone say its to put $this->load->database(); its already in my autoload in config file :)
Fatal error: Call to a member function insert() on a non-object
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Finances::$inventory_model
Filename: controllers/finances.php
Line Number: 125
CONTROLLER:
public function inventory_management($post_array,$primary_key)
{
$this->load->model('inventory_model');
if($post_array['id_expense']!=null){
$type = 'add';
$id_exp_detail = $primary_key;
$result = $this->inventory_model->insert([
'id_exp_detail' => $id_exp_detail,
'name' => $post_array['brand'],
'quantity' => $post_array['item_quantity'],
'lote' => $post_array['package_code'];
'type' => $type,
'date' => date(),
'id_user' => $this->session->userdata('id_user'),
'id_entity' => $this->session->userdata('id_entity')
]);
}else if ($post_array['name']!=null){
$type = 'sub';
$id_treatment = $primary_key;
$result = $this->inventory_model->insert([
'id_treatment' => $id_treatment,
'name' => $post_array['name'],
'quantity' => $post_data['recomended_dose'],
'lote' => $post_array['id_package'],
'type' => $type,
'date' => date(),
'id_user' => $this->session->userdata('id_user'),
'id_entity' => $this->session->userdata('id_entity')
]);
}else{
$type = 'sub';
$id_fertilization = $primary_key;
$result = $this->inventory_model->insert([
'id_fertilization' => $id_fertilization,
'name' => $post_data['name'],
'quantity' => $post_data['quantity'],
'lote' => $post_data['id_package'],
'type' => $type,
'date' => date(),
'id_user' => $this->session->userdata('id_user'),
'id_entity' => $this->session->userdata('id_entity')
]);
}
if($result){
echo "validation Complete";
}
}
MODEL
class CRUD_model extends CI_Model
{
protected $_table = null;
protected $_primary_key = null;
// ------------------------------------------------------------------------
public function __construct()
{
parent::__construct();
}
public function insert($data)
{
$this->db->insert($this->_table, $data);
// return $this->db->insert_id();
}
MODEL:
class inventory_model extends CRUD_model
{
protected $_table = 'inventory_management';
protected $_primary_key = 'id';
// ------------------------------------------------------------------------
public function __construct()
{
parent::__construct();
}
// ------------------------------------------------------------------------
}
here is the wrong you call wrong model file
$this->load->model('inventory_model');
it should be
$this->load->model('crud_model');
and you use wrong way to pass array in function
$param = array('id_exp_detail' => $id_exp_detail, 'name' => $post_array['brand'], 'quantity' => $post_array['item_quantity'], 'lote' => $post_array['package_code'], 'type' => $type, 'date' => date(), 'id_user' => $this->session->userdata('id_user'), 'id_entity' => $this->session->userdata('id_entity'));
$result = $this->inventory_model->insert($param);

Categories