php - How to display items using sessions -
i doing opencourse project have load menu through xml file , keep track of user buys , display in cart. done dont seem access items bought through $_sessions
. idea of how web running simple loads particular category menu using variable , when item bought saving in $_session array when visit cart.php
dont seem see record in $_session
here index.php
<?php session_start(); //loading xml file $dom = simplexml_load_file("../menu.xml"); // variable store category id $categoryid; //to store data of particular category id $names = []; //to store name of category $categoryname; //to count how many categories in there in xml file $categorycount = count($dom->xpath("/menu/category")); //checking if user visited via , if variable set if($_server["request_method"] == "get") { if(!isset($_get["cat"])) { $categoryid = 1; } // checking if variable set within limits of number of categories loaded else if (isset($_get["cat"]) && ($_get["cat"] > 0 && $_get["cat"] <= $categorycount)) { $categoryid = $_get["cat"]; } else { print("invalid category id"); } //now loading appropriate category xml foreach($dom->xpath("/menu/category[@id = '{$categoryid}']") $category) { $categoryname = $category->option; //saving data foreach($category->name $name) { $names[] = [ "optionname" => $name->type, "small" => $name->small, "large" => $name->large, "general" => $name->general ]; } } //loading displaying files include("../views/header.php"); include("../views/viewpage.php"); }
?>
the file header.php
got head
tag of file not pasting here, table produced viewpage.php
code follows
<body> <div class = "cart"> <a href= "cart.php">view cart</a> </div> <?php if(isset($error)): ?> <div class = "warning"> <p>no item selected</p> </div> <?php endif; ?> <table> <thead> <tr> <th colspan = "4"><?= $categoryname ?></th> </tr> <tr> <th>options</th> <th>small</th> <th>large</th> <th>general price</th> </tr> </thead> <tbody> <form action = "buy.php" method = "post"> <?php foreach($names $item): ?> <tr class = "even"> <td><?= $item["optionname"] ?></td> <td> <!-- checking every key value pair of $item if empty , dynamically giving names inputs combining name , price--> <?php if($item["small"] != ""): ?> <?= $item["small"] ?> <input type = "text" name = "<?= $item["optionname"]?>#<?=$item["small"]?>"> </input> <?php endif; ?> </td> <td> <?php if($item["large"] != ""): ?> <?= $item["large"] ?> <input type = "text" name = "<?= $item["optionname"]?>#<?= $item["large"] ?>"> </input> <?php endif; ?> </td> <td> <?php if($item["general"] != ""): ?> <?= $item["general"] ?> <input type = "text" name = "<?= $item["optionname"]?>#<?= $item["general"] ?>"> </input> <?php endif; ?> </td> </tr> <?php endforeach ?> </tbody> </table> <input type = "submit" value = "buy!"> </form> <!-- printing list display categories using categorycount variable--> <div class= "list"> <h4>category</h4> <?php for($i = 1; $i <= $categorycount; $i++) { print("<ul> <li><a href = \"?cat={$i}\">{$i}</a></li> </ul>"); } ?> </div> </body>
the buying process done buy.php
, code follows
<?php session_start(); //print_r($_post); //print("</br>"); $quantity; $typename; $price; foreach($_post $key => $value) { //array save category name, size, price , quantity of each item $cart = []; if(!empty($value)) { $quantity = $value; //print($key ." val ". $value); //print("</br>"); //breaking input name type thats in xml input name, section before '#' $typename = strstr($key, "#", true); //getting part comes after '#' $price = substr($key, strpos($key, "#") + 1); //now converting '_' space , '.' $typename = str_replace("_", " ", $typename); $price = str_replace("_", ".", $price); //checking if '&' in name present convert if & /*if(strpos($typename, "&")) { $typename = str_replace("&", "&", $typename); }*/ //print($typename); //print("</br>"); //print($price); //print("</br>"); //opening xml file search $dom = simplexml_load_file("../menu.xml"); //searching foreach($dom->xpath("/menu/category") $category) { //iterating on every name tag in xml foreach($category->name $name) { //print("</br>checking on run{$name->type} </br></br>"); //checking every type tag if($name->type == $typename) { //storing category name type belongs $cartcategoryname = $category->option; //print("the category {$cartcategoryname}</br>"); //test see size above matched type if($name->small == $price) { $size = "small"; } else if($name->large == $price) { $size = "large"; } else { $size = "general"; } } } /*adding name,size,type quantity in array * added $_session , array * wiped clean.*/ $cart = [ "category" => $cartcategoryname, "type" => $typename, "size" => $size, "price" => $price, "quantity" => $quantity ]; } $_session["cart"][] = $cart; } /* name of category, type, size, price , quantity in * arry cart */ /*$cart[] = [ "category" => $cartcategoryname, "type" => $typename, "size" => $size, "price" => $price, "quantity" => $quantity ];*/ //print_r($cart); //print("</br></br></br>"); //pushing above created arror session global //$_session["cart"][] = $cart; } //print_r($_session); //print("</br>"); /*foreach($_session["cart"] $cartitem) { print($cartitem["category"]."</br>"); print($cartitem["quantity"]."</br>"); print($cartitem["type"]."</br>"); }*/ $host = $_server['http_host']; $uri = rtrim(dirname($_server['php_self']), '/\\'); $extra = 'index.php?cat=1'; header("location: http://$host$uri/$extra"); exit; //header("location: index.php?cat=1"); //exit;
?>
now when use print_r($_session)
in cart.php
cant see of things code
<?php session_start(); foreach($_session["cart"] $cartitem) { print($cartitem["category"]."</br>"); print($cartitem["quantity"]."</br>"); print($cartitem["type"]."</br>"); } //phpinfo();
?>
the xml file follows
<menu> <category id = '1'> <option>pizzas</option> <name> <type>tomato & cheese</type> <small>5.50</small> <large>9.75</large> <general/> </name> <name> <type>onions</type> <small>6.85</small> <large>10.85</large> <general/> </name> </category> <category id = '2'> <option>side orders</option> <name> <type>onion rings</type> <small>2.60</small> <large>2.95</large> <general/> </name> <name> <type>french fries</type> <small>2.85</small> <large>3.85</large> <general/> </name> </category>
the :
$_session["cart"][] = $cart;
in buy.php
not inside (correct) foreach
.
put @ end inside of
foreach($dom->xpath("/menu/category[@id = '{$categoryid}']") $category)
loop.
$cart = [ "category" => $cartcategoryname, "type" => $typename, "size" => $size, "price" => $price, "quantity" => $quantity ]; $_session["cart"][] = $cart; } // end of foreach($dom->xpath("/menu/category[@id = '{$categoryid}']") $category)
Comments
Post a Comment