Jump to content

using $session here?


lecrackffm
 Share

Recommended Posts

Hello everyone, 

i am building a little 'shop-like' element for a clients Homepage (no direct payment)

Its my first Approach working with php besides very basic php and Processwire stuff, so i am a litte lost here. 

I want to use KnockoutJs for getting User Input, process the Data on the Server (pulling actual prices from a page) and send a single JSON Object, which holds all the Data needed for display in the UI back to knockout .

The data will be send on every "keyup" in the front end. so i want the "checkout" array to be updated every time.

I have some code already and it looks good so far. But obviously, with every ajax request ill get a new object with only the newest data back.

<?php 
// Data comming on every "keyup"
$hardMenge = $input->get->entities("hard");
$softMenge = $input->get->entities("soft");
// more to items will follow

// Different products to choose from (actual prices will come from pages later)
// More Products with different requierements will be added
class Softcover {
	public $menge;
	public $preis;
	public function __construct($menge)
	{
		$this->menge = $menge;
		$this->preis = $menge * 10;
	}
}
class Hardcover {
	public $menge;
	public $preis;
	public $letter;
	public function __construct($menge, $letter)
	{
		$this->menge 	= $menge;
		$this->preis 	= $menge * 10;
		$this->letter = $letter;
		$this->total 	= $letter * .5 + $this->preis;
	}
}

$softy = new Softcover($softMenge);
$harty = new Hardcover($hardMenge,2);

$checkout = array();
$softOut = array("soft" => array("menge" => $softy->menge, "preis" => $softy->preis));
$hardOut = array("hart" => array("menge" => $harty->menge, "preis" => $harty->preis, "mitGravur" => $harty->total));
if (isset($softy) && ($softy->menge > 0)) {
array_push($checkout,  $softOut);
}
if (isset($harty) && ($harty->menge > 0)) {
array_push($checkout,  $hardOut);
}
// JSON object back to knockoutJS
echo json_encode($checkout);

My guess is i might use sessions eg $session here to keep all the data in the "checkout" array, and only refresh relevant parts of it on frontend "keyup"events.

How could i use sessions to achieve that?

Or is my Approach going nowere and i need to rethink everything?

Thanks for helping out.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...