2013-06-15 5 views
2

이 오류를 우연히 발견했다 배열 내의 변수와 값 사이에 어떤 수학을 수행하기 때문일 수 있습니다. 내가 잘 모르는 부분은 배열 내의 값에 대해 수학을 수행하는 방법입니다.지원되지 않는 피연산자 유형

($line_cost = $price * $quantity). 

아무에게도 내게이 지침을 제공 할 수 있습니까? 나는 가장 감사 할 것이다! 여기 내 코드합니다 - gettype() 함수 $price 문자열이고 $quantity 배열임을 같게

<?php session_start(); ?> 

<?php 

    $product_id = $_GET['id']; //the product id from the URL 
    $action  = $_GET['action']; //the action from the URL 

    //if there is an product_id and that product_id doesn't exist display an error message 
    if($product_id && !productExists($product_id)) { 
     die("Error. Product Doesn't Exist"); 
    } 

    switch($action) { //decide what to do 

     case "add": 
      $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id 
     break; 

     case "remove": 
      $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id 
      if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. 
     break; 

     case "empty": 
      unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
     break; 

    } 

?> 


<?php 

    if($_SESSION['cart']) { //if the cart isn't empty 
     //show the cart 

     echo "<table border=\"1\" padding=\"3\" width=\"40%\">"; //format the cart using a HTML table 

      //iterate through the cart, the $product_id is the key and $quantity is the value 
      foreach($_SESSION['cart'] as $product_id => $quantity) {  

       //get the name, description and price from the database - this will depend on your database implementation. 
       //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection 
       $sql = sprintf("SELECT name, description, price FROM php_shop_products WHERE id = %d;", 
           $product_id); 

       $result = mysql_query($sql); 

       //Only display the row if there is a product (though there should always be as we have already checked) 
       if(mysql_num_rows($result) > 0) { 

        list($name, $description, $price) = mysql_fetch_row($result); 

       echo "$price"; 
       var_dump($quantity); 

        $line_cost = $price * $quantity;  //work out the line cost 
        $total = $total + $line_cost;   //add to the total cost 

        echo "<tr>"; 
         //show this information in table cells 
         echo "<td align=\"center\">$name</td>"; 
         //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product 
         echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$product_id\">X</a></td>"; 
         echo "<td align=\"center\">$line_cost</td>"; 

        echo "</tr>"; 

       } 

      } 

      //show the total 
      echo "<tr>"; 
       echo "<td colspan=\"2\" align=\"right\">Total</td>"; 
       echo "<td align=\"right\">$total</td>"; 
      echo "</tr>"; 

      //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation 
      echo "<tr>"; 
       echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>"; 
      echo "</tr>";  
     echo "</table>"; 



    }else{ 
     //otherwise tell the user they have no items in their cart 
     echo "You have no items in your shopping cart."; 

    } 

    //function to check if a product exists 
    function productExists($product_id) { 
      //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection 
      $sql = sprintf("SELECT * FROM php_shop_products WHERE id = %d;", 
          $product_id); 

      return mysql_num_rows(mysql_query($sql)) > 0; 
    } 
?> 

<a href="products.php">Continue Shopping</a> 


<?php 

/* 

products table: 
    CREATE TABLE `products` (
     `id` INT NOT NULL AUTO_INCREMENT , 
     `name` VARCHAR(255) NOT NULL , 
     `description` TEXT, 
     `price` DOUBLE DEFAULT '0.00' NOT NULL , 
     PRIMARY KEY (`id`) 
    ); 

*/ 

?> 



</body> 
</html> 
+0

줄 77은 어디에 있습니까? – Kevin

+0

'$ line_cost = $ price * $ quantity' 라인 77의 내용입니까? –

+0

두 개의 * 피연산자 *가 숫자가 아니거나 숫자로 변환 될 수있는 것을 의미합니다. 두 변수는 무엇입니까? – deceze

답변

3

, 캐스트 $price 정수가 있으면 (정수 값에 액세스하기 위해 키 배열 $quantity 사용할 제 정수가 아닌, 타입 변환도).

는 그래서 같은 간다 :

$line_cost =(int)$price * (int)$quantity['key']; 

는 작동 희망!

+0

정확히 내가 찾던 답변의 종류. 감사! –

+0

@MichaelWalkling 도움이 되었어요 :) – Kevin

관련 문제