Page 1 of 1
Using session ids with classes.
Posted: Thu Jul 11, 2002 10:06 am
by Rfinance
I am writing a shopping cart application and am using session ids.
1. I find that I need to use accessor methods to pass the session variables from the procedural code to the class I am using. This works.
2. When I use the FORM tag in HTML to pass control back to the begining of the code ( from the class Object) I find that the session variable (namely an associate array) is losing some data. Of course when I use the FORM tag I am not using the accessor method to pass the information back to the session variable.
I am using PHP4 4.0.4pl1 .
I wonder if anyone could advise.
Thanks.
Posted: Thu Jul 11, 2002 10:47 am
by BDKR
OK. Well, I follow what you're saying in the first point, but you are kinda losing me in the second point. The description is vauge. Can you try to explain again?
Is this session var associated with the class you are using?
Thanx,
BDKR (TRC)
Posted: Thu Jul 11, 2002 11:29 am
by Rfinance
Hi BDKR,
I hope this explanation will help.
There are a few session variables. One is an associate array .
I come into the scripting page and instantiate the class. I then use accessors to set the particular variables within the class object (this seems the only way I can use the session variables within the class object). If I perform some actions I go through the whole script page and at the end of it I drop out of the class object code and I use an accessor to get the variable back from the class to the session variable.
The problem is when I don't go through the whole script page and break off using the value passed to the FORM tag. When I go back to the beginning the $cart associate array is not complete.
I've put the important bits of code below (I use inheritance, down casting, up casting etc):
<?
session_start();
$errormsg = "";
require_once ("CartClasses.inc");
class ViewCart extends CartHeader1Page {
-------- Some code in here ----------------------
function SetNEW($new) { $this->new = $new; }
function GetNEW() { return $this->new; }
function DisplayNew() {
$cart = $this->cart;
$new = $this->new;
$DisplayFns = new DisplayFns();
$HTMLFns = new HTMLFns();
$BookFns = new BookFns();
if($cart && array_count_values($cart)) {
// Set $this->cart variable in case we use the $save variable and
// move directly back to the beginning of this script
$change = true;
$images = 1;
// $cart is an associative array. It contain a reference to the isbn and quantity of each book held in the cart.
$BookFns = new BookFns();
// display items in shopping cart
// optionally allow changes (true or false)
// optionally include images (1 - yes, 0 - no)>
echo "<table border = 0 width = 100% cellspacing = 0>
<form action = Cart03ViewCart.php4 method = post>
------ Some code in here ----------------------
// display save change button
if($change == true) {
echo "
</table>
<center><td align = center>
<input type = hidden name = 'cart[]' value = '$cart'>
<input type = hidden name = save value = true>
<input type = image src = \"images/save-changes.gif\"
border = 0 alt = \"Save Changes\">
</td></center>
";
}
echo "</form>";
--- Some code in here ----------------------
}
function Main() {
----- Some code in here -------
} // End of Main.
} // End of Class ViewCart.
/* ----------------------------------------------------------------------- */
$Cart03 = new ViewCart(); // Instantiate the class
$Cart03->SetSave($save);
// Restore the "session variables" and put into class accessor method.
$Cart03->SetCart($cart);
$Cart03->SetItems($items);
$Cart03->SetPrice($total_price);
$Cart03->SetNEW($new);
$Cart03->Process(); // This method uses polymorphism, up and down casting.
// Pass the latest "Class variables" but to the "session variables";
$cart = $Cart03->GetCart() ;
$items = $Cart03->GetItems();
$total_price = $Cart03->GetPrice();
exit;
?>