2017-04-07 77 views
0

我正在做一个opencourse项目,我必须通过xml文件加载菜单并跟踪用户购买的内容并将其显示在购物车中。几乎所有事情都完成了,但我似乎无法访问通过$_SESSIONS购买的物品。关于如何运行网页的想法很简单,它使用get变量从菜单中加载特定类别,并且在购买商品时,我将它作为数组保存在$ _SESSION中,但当我访问cart.php时,我似乎看不到在$_SESSION如何使用会话显示项目

这里记录我的index.php

<?php 
session_start(); 

//loading the xml file 
$dom = simplexml_load_file("../menu.xml"); 

// variable to store category id 
$categoryid; 

//to store all the data of a particular category id 
$names = []; 

//to store the name of the category 
$categoryname; 

//to count how many categories were in there in the xml file 
$categorycount = count($dom->xpath("/menu/category")); 

//Checking if the user visited via GET and if GET variable is set 
if($_SERVER["REQUEST_METHOD"] == "GET") 
{ 

    if(!isset($_GET["cat"])) 
    { 
     $categoryid = 1; 
    } 
    // checking if the variable is set within the 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 the appropriate category from xml 
    foreach($dom->xpath("/menu/category[@id = '{$categoryid}']") as $category) 
    { 
     $categoryname = $category->option; 

     //saving the data 
     foreach($category->name as $name) 
     { 
      $names[] = [ 
        "optionname" => $name->type, 
        "small" => $name->small, 
        "large" => $name->large, 
        "general" => $name->general 
        ]; 
     } 
    } 

    //loading the displaying files 
    include("../views/header.php"); 
    include("../views/viewpage.php"); 

} 

?>

文件header.php刚刚拿到文件的head标记,以便不在这里粘贴,表由viewpage.php产生这样的代码如下

<body> 
<div class = "cart"> 
    <a href= "cart.php">View Cart</a> 
</div> 
<?php if(isset($error)): ?> 
<div class = "warning"> 
    <p>No Item was 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 as $item): ?> 
       <tr class = "even"> 
        <td><?= $item["optionname"] ?></td> 
        <td> 
         <!-- checking every key value pair of $item if its empty and dynamically giving the names to inputs combining name and 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 the list to display categories using the 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> 

购买过程由buy.php完成,其代码如下

<?php 
session_start(); 
//print_r($_POST); 
//print("</br>"); 

$quantity; 
$typename; 
$price; 
foreach($_POST as $key => $value) 
{ 
    //array to save category name, its size, price and quantity of each item 
    $cart = []; 

    if(!empty($value)) 
    { 
     $quantity = $value; 
     //print($key ." val ". $value); 
     //print("</br>"); 
     //breaking the input name to get the type thats in xml from input name, the section before '#' 
     $typename = strstr($key, "#", true); 

     //getting the part that comes after '#' 
     $price = substr($key, strpos($key, "#") + 1); 

     //now converting the '_' to a space and '.' 
     $typename = str_replace("_", " ", $typename); 
     $price = str_replace("_", ".", $price); 

     //checking if '&' in name is present then convert if to &amp; 
     /*if(strpos($typename, "&")) 
     { 
      $typename = str_replace("&", "&amp;", $typename); 
     }*/ 

     //print($typename); 
     //print("</br>"); 
     //print($price); 
     //print("</br>"); 

     //opening the xml file to search 
     $dom = simplexml_load_file("../menu.xml"); 

     //searching 
     foreach($dom->xpath("/menu/category") as $category) 
     { 
      //iterating over every name tag in xml 
      foreach($category->name as $name) 
      { 
       //print("</br>checking on run{$name->type} </br></br>"); 
       //checking every type tag 
       if($name->type == $typename) 
       { 
        //storing what category name the type belongs to 
        $cartcategoryname = $category->option; 
        //print("the category is {$cartcategoryname}</br>"); 

        //test to see what size the above matched type is 
        if($name->small == $price) 
        { 
         $size = "small"; 
        } 
        else if($name->large == $price) 
        { 
         $size = "large"; 
        } 
        else 
        { 
         $size = "general"; 
        } 
       } 

      } 

      /*adding the name,size,type quantity in an array 
      * which would be added to $_SESSION and the array 
      * would be wiped clean.*/ 
      $cart = [ 
       "category" => $cartcategoryname, 
       "type" => $typename, 
       "size" => $size, 
       "price" => $price, 
       "quantity" => $quantity 
       ]; 
     } 

     $_SESSION["cart"][] = $cart; 
    } 

    /* name of category, its type, size, price and quantity in 
    * arry cart */ 
    /*$cart[] = [ 
      "category" => $cartcategoryname, 
      "type" => $typename, 
      "size" => $size, 
      "price" => $price, 
      "quantity" => $quantity 
      ];*/ 


    //print_r($cart); 
    //print("</br></br></br>"); 

    //pushing the above created arror into session global 
    //$_SESSION["cart"][] = $cart;  
} 
//print_r($_SESSION); 
//print("</br>"); 

/*foreach($_SESSION["cart"] as $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; 

?>

现在,当我在cart.php使用print_r($_SESSION)我看不到任何的东西 代码是

>

xml文件是如下

<menu> 
<category id = '1'> 
    <option>Pizzas</option> 
    <name> 
     <type>Tomato &amp; 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> 
+1

您是否在所有使用会话的页面上使用了session_start()? – nerdlyist

+1

buy.php不会启动会话,因此,您将无法使用$ _SESSION [“cart”],与cart.php – OldPadawan

+0

一样,也包括它也dosent解决问题 –

回答

0

确定拿到了$_SESSIONcart.php工作,通过包括error_reporting(E_ALL); ini_set("display_errors", 1) 我得到一个错误Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in [no active file] on line 0 做了一些谷歌搜索,并发现了XML数据仍然是一个XML数据,一个需要type cast它变成一个string。这样做可以让我看到会话变量cart.php

+0

几乎这样做? :) – OldPadawan

+0

nope还有很多工作要做,但现在解决了这个问题 –

0

的:

$_SESSION["cart"][] = $cart;buy.php不是(正确)foreach内部。

把它在最后但

foreach($dom->xpath("/menu/category[@id = '{$categoryid}']") as $category)循环中。

$cart = [ 
       "category" => $cartcategoryname, 
       "type" => $typename, 
       "size" => $size, 
       "price" => $price, 
       "quantity" => $quantity 
       ]; 

$_SESSION["cart"][] = $cart; 
} // End of foreach($dom->xpath("/menu/category[@id = '{$categoryid}']") as $category) 
+0

one}你的意思是? – OldPadawan

+0

查看更新回答 –

+0

把$ _SESSION [“cart”] [] = $ cart;在最后,但在foreach循环内也没有帮助,再次在cart.php中获得一个空数组....我认为旧的位置也是正确的,我检查了它的oput放置,它在buy.php中很好,它的cart.php点击时不显示任何会话变量 –