CodeIgniter Forums

Full Version: Pass Id getting from URL to many function in CodeIgniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am getting an ID from a url like:
Code:
<?php echo base_url();?>Product/product/<?php echo $product->id;?>"

In my sidebar on the same function[view -> Product/product] I am getting it correctly. but when I click some other function in my sidebar the Id is not coming like:
Code:
public function Product($id)
{
    $this->data['product'] = $this->prod->get_product($id);
   $this->load->view('products/header');
   $this->load->view('products/topnav');
   $this->load->view('products/sidebar',$this->data);
   $this->load->view('products/home',$this->data);
   $this->load->view('products/footer');
}

it is working on this page as expected, but when I jump to different function in my sidebar the id is not working because it is not getting it... like:
Code:
public function Data()
{
   $id=$this->Product($id);
   $this->data['product'] = $this->prod->get_product($id);
    $this->load->view('products/header');
   $this->load->view('products/topnav');
   $this->load->view('products/sidebar', $this->data);
   $this->load->view('products/data');
   $this->load->view('products/footer');

}

What I want: How to get the Id which I pass above to all my sidebar functions.

Edit:

Currently I am passing the $id using URL like: 
PHP Code:
<?php echo base_url();?>Product/product/<?php echo $product->id;?>

The above URL clearly shows that the $id will go to Product function which is available in PRODUCT controller. but there are also other functions in PRODUCT controller in which I want that $id. So,
My Question here is: How to get that $id in my all PRODUCT controller functions.
public function Product($id) <-- $id included
public function Data() <-- $id not included

Change it into:
public function Data($id)
(02-26-2019, 10:48 AM)jreklund Wrote: [ -> ]public function Product($id) <-- $id included
public function Data() <-- $id not included

Change it into:
public function Data($id)

Tried but its also not working..
$this->product($id) will only have a value for $id if that method is called as part of servicing a request, Product/product/id.
Calling it the way you do, from the request handling method Product/data, makes no sense.
(02-26-2019, 07:19 PM)ciadmin Wrote: [ -> ]$this->product($id) will only have a value for $id if that method is called as part of servicing a request, Product/product/id.
Calling it the way you do, from the request handling method Product/data, makes no sense.

Yes Sir, I understand it, That's the reason I have posted the question so I can know that how to achieve the above requirements. As I want to show the id in all sidebar functions.
Then you will need to pass the $id to all of your sidebar functions/methods.

PHP Code:
function one($id)
{

}

function 
two($id)
{



Or you could assign the $id to your session data and check it in every function/method.
(02-27-2019, 02:57 AM)InsiteFX Wrote: [ -> ]Then you will need to pass the $id to all of your sidebar functions/methods.

PHP Code:
function one($id)
{

}

function 
two($id)
{



Or you could assign the $id to your session data and check it in every function/method.

Actually Sir I have already tried the value passing into another / all sidebar functions but the result is same, if I use the session or I will pass it into all the function the id is not flowing into the other functions.

I am updating my question by adding more explanation, may be it will help all the users to understand it clearly.
It has to work if your passing the id through the function call.

Are you sure that you are receiving the id?

PHP Code:
class Product extends CI_Controller
{
 
   private $id;

 
   public fuction setId($id)
 
   {
 
       $this->id $id;
 
   }

 
   public getId()
 
   {
 
       return $this->id;
 
   }



Now when ever you need the id in that controllers methods just call getId();

PHP Code:
$id $this->getId(); 

It should still work if your using sessions.
(02-27-2019, 09:37 AM)InsiteFX Wrote: [ -> ]It has to work if your passing the id through the function call.

Are you sure that you are receiving the id?

PHP Code:
class Product extends CI_Controller
{
 
   private $id;

 
   public fuction setId($id)
 
   {
 
       $this->id $id;
 
   }

 
   public getId()
 
   {
 
       return $this->id;
 
   }



Now when ever you need the id in that controllers methods just call getId();

PHP Code:
$id $this->getId(); 

It should still work if your using sessions.


Yes I am 200% sure that I am getting the Id but the Id is only visible in my function / view where I am sending it using the url:

But it is not reflecting in other functions / views, because the $id variable is empty and does not contain any value and its throwing Argument Error :


PHP Code:
TypeArgumentCountError

Message
Too few arguments to function Products::Product(), 0 passed in D:\xampp\htdocs\mypro\system\core\CodeIgniter.php on line 532 and exactly 1 expected 
Then you need to check the value of $id to see if it is 0.