Hi,
I'm not very much used to with this kind of things, trying to call a class variable from a different page... Please look at the code below:

class.php

class bag{
  private $no_of_items=0;
  private $sub_total;

  public function update(){
  $this->no_of_items = $this->no_of_items++; 
  return true; 
  }
}

index.php

$cart = new bag;
//Some calculations...
$cart->update();
echo $cart->no_of_items; //output is right

product.php

//Some other code
echo $cart->no_of_items; //output is wrong

Why this output inside product.php is not updated as index.php? How to do this ???
Please help ... Thanks in advance.

Ahsan, Tokyo

Recommended Answers

All 19 Replies

Your $cart is not kept when switching to other pages. You need to store it in a session variable if you need it.

I'm not sure how your code is working in the first place. You can not echo out the value of a private or protected class member. So if your class does contain a private $no_of_items you can not display it directly.

<?php
class bag{
  private $no_of_items=0;
  private $sub_total;
 
  public function update(){
  $this->no_of_items = $this->no_of_items++; 
  return true; 
  }
}

$cart = new bag;
//Some calculations...
$cart->update();
echo $cart->no_of_items;  // <== Should give a fatal error!

put this on top of your product.php

include("class.php");

Your $cart is not kept when switching to other pages. You need to store it in a session variable if you need it.

Thanks for your quick reply pritaeas
But I dont want to use session variable bcoz to update a session variable we need to submit a form. As we cannot do it on the same page... I forgot to mention my product.php is actually under the index.php page.

http://www.w3schools.com/php/php_sessions.asp

Thanks for the link.

I'm not sure how your code is working in the first place. You can not echo out the value of a private or protected class member. So if your class does contain a private $no_of_items you can not display it directly.

<?php
class bag{
  private $no_of_items=0;
  private $sub_total;
 
  public function update(){
  $this->no_of_items = $this->no_of_items++; 
  return true; 
  }
}

$cart = new bag;
//Some calculations...
$cart->update();
echo $cart->no_of_items;  // <== Should give a fatal error!

But there are no fatal errors! I dont know why :?:

put this on top of your product.php

include("class.php");

Thanks for your suggestion phpbegginners.
Nothing happened. My $no_of_items variable doesn't change...

how about if you remove "=0" in your private $no_of_items=0;

just type inside your class.

private $no_of_items;

But I dont want to use session variable bcoz to update a session variable we need to submit a form.

What do you mean by this?

But there are no fatal errors! I dont know why :?:

use this on top of page

ini_set("display_errors", 1);

I remember this in php4 you can try.

<?php
class Cart {
    var $items;  // Items in our shopping cart

    // Add $num articles of $artnr to the cart

    function add_item($artnr, $num) {
        $this->items[$artnr] += $num;
    }

    // Take $num articles of $artnr out of the cart

    function remove_item($artnr, $num) {
        if ($this->items[$artnr] > $num) {
            $this->items[$artnr] -= $num;
            return true;
        } elseif ($this->items[$artnr] == $num) {
            unset($this->items[$artnr]);
            return true;
        } else {
            return false;
        }
    }
}
?> 



<?php
$cart = new Cart;
$cart->add_item("10", 1);

$another_cart = new Cart;
$another_cart->add_item("0815", 3);
?> 


you can used these code below in constructor..

<?php
class Cart {
    /* None of these will work in PHP 4. */
    var $todays_date = date("Y-m-d");
    var $name = $firstname;
    var $owner = 'Fred ' . 'Jones';
    /* Arrays containing constant values will, though. */
    var $items = array("VCR", "TV");
}

/* This is how it should be done. */
class Cart {
    var $todays_date;
    var $name;
    var $owner;
    var $items = array("VCR", "TV");

    function Cart() {
        $this->todays_date = date("Y-m-d");
        $this->name = $GLOBALS['firstname'];
        /* etc. . . */
    }
}
?>

You can not access the class variable that has been declared as private from outside the class. You either declare it public (instead of private) which is really not a good practice or add a method that returns the value of the variable.

class bag{
  private $no_of_items=0;
  private $sub_total;

  public function update(){
  $this->no_of_items = $this->no_of_items++; 
  return true; 
  }

  public function get_no_of_items() {
    return $this->no_of_items;
  }
}

Get the number of items:

echo $cart->get_no_of_items();

I am not sure if this is the core of your problem though.

What do you mean by this?

By this what I mean is, without refreshing a page we can get a updated session variable to display.

Actually, I'm on the same page but two places. One place I need to calculate the variable and another place I like to display it without refreshing the page.

Hope you understand my intension.

put the variable in session then add 1 using increment..is that what you mean?

$_SESSION['counter']=1 ...++

$_SESSION['item']=$_SESSION['counter'] + 1 

$_SESSION['item'] = 2

You can not access the class variable that has been declared as private from outside the class. You either declare it public (instead of private) which is really not a good practice or add a method that returns the value of the variable.

class bag{
  private $no_of_items=0;
  private $sub_total;

  public function update(){
  $this->no_of_items = $this->no_of_items++; 
  return true; 
  }

  public function get_no_of_items() {
    return $this->no_of_items;
  }
}

Get the number of items:

echo $cart->get_no_of_items();

I am not sure if this is the core of your problem though.

Thanks for your answer!
But its not what I'm asking for ...

put the variable in session then add 1 using increment..is that what you mean?

$_SESSION['counter']=1 ...++

$_SESSION['item']=$_SESSION['counter'] + 1 

$_SESSION['item'] = 2

Thanks again.
I tried this one.
The actual problem is we can not display a session variable without refreshing the page.
So, after increasing session variable its remaining the same. When I press F5, it shows the updated value .... that's what my problem is.
I want the value to be displayed without refreshing the page...

the easiest way is to use session variable, but if you dont like to use session variable try this.
put on top of your index

include_once("class.php");

since you are already using the instantiated value of class into your index.php

the easiest way is to use session variable, but if you dont like to use session variable try this.
put on top of your index

include_once("class.php");

since you are already using the instantiated value of class into your index.php

This line is already on the top of my index.php
And I declared the object $cart inside index.php

You can not change a session variable from your page without sending a http request to the server. If you do not want to refresh the page you have to use Ajax. So when user clicks something on your page (maybe a select element) javascript function hooked to that event sends http request to the server script that updates the session variable and response of the request is used to update another element on the page (again with javascript). Search for Ajax examples on this forum or elsewhere. You should find plenty of explanations (sorry to direct you to google since I am very short on time at the moment).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.