Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: problem z klasą koszyka - jak tego używać ?
Forum PHP.pl > Forum > PHP
nikosss
witajcie smile.gif

Dopiero zaczynam przygodę z php smile.gif mam taką klasę:

<?php
/*
Basket class for e-commerce purpose

Version : 0.5
Type:Class
Category:Shopping Carts
License: GNU General Public License

Description: This class provides methods for add, remove, update and remove all items
from a basket. The basket works with a session cookie, saving the product ID
and quantity in an array for simple accesing it.
There's an example in this category... called "Basket example using basket class"
*/

class basket {
var $items;
var $empty;

function basket()
{
global $cart;
if(isset($cart))
{
$this->items=unserialize(stripslashes($cart));
if ( gettype($this->items) == "array" )
{
$this->empty=false;
}
else
$this->empty=true;
}
else
$this->empty=true;
}

function additem($id, $name, $count, $prix)
{
if ($this->items[$id][1] > 0 )
{
$this->items[$id][1]+=$count;
}
else
{
$this->items[$id][0]=$name;
$this->items[$id][1]=$count;
$this->items[$id][2]=$prix;
$this->items[$id][3]=stripslashes($name);
}
setcookie("cart",serialize($this->items),0,"/");
$this->empty=false;
}

function removeitem($id) {
$tmp=""; // Don't forget to initialize !
if(!$this->empty) {
while(list($x,$y)=each($this->items)){
if($x!=$id) $tmp[$x]=$y;
};
};
$this->items=$tmp; // Or this will have no effect !
setcookie("cart",serialize($this->items),0,"/");
if(count($this->items)==0) $this->empty=true;
}

function resetArray($basket)
{
reset($basket->items);
}

function countItems($basket)
{
if(!$basket->empty)
{
while ( list($x,$y,) = each($basket->items) )
{
$ant++;
}
}
return $ant;
}
function sumItems($basket)
{
if(!$basket->empty)
{
while ( list($x,$y,) = each($basket->items) )
{
$ant = $ant + $y[1];
}
}
return $ant;
}
function printItems($b)
{
if(!$b->empty)
{
while ( list($x,$y,) = each($b->items) )
{
echo "$x ".$y[0]." ".$y[1]." <a href=\"$PHP_SELF?action=R&id=$x\">Remove</a><br>";
}
}
}
function updateitem($id,$count){
$this->items[$id][1]=$count;
setcookie("cart",serialize($this->items),0,"/");
}

function removeall(){
setcookie("cart",NULL,0,"/");
$this->empty=true;
}

}
?>

</body>
</html>


niby wygląda normalnie ale nie wiem wogóle jak tego używać smile.gif

zrobiłem sobie plik kosz.php gdzie ją wgrałem
dalej zrobiłem sobie plik koszyk.php gdzie zaincludowałem kosz.php i chcialem wrzucic tam produkt:

session_start();
include("test.php");
$basket->additem(100, ala, 1, 22);

ale wywala mi błąd - to samo jak daje $cart;

Czy ktoś może mi pomóc ? przewertowałem forum i jest masa tych klas ale nie ma nigdzie prostego przykładu jak wrzucic produkt jak to skasowac czy zmodyfikowac smile.gif
-=Peter=-
Używaj tagu php... (tak na przyszłość)...

A tak pozatym raczej sprzydałaby się treść błędu, nie uważasz?

Takie moje domysły:

  1. <?php
  2. include("test.php");
  3.  
  4. $basket = new basket();//nie zainicjowałeś zmiennej...
  5.  
  6. $basket->additem(100, 'ala', 1, 22);//stringi (:P) daje się w cudzysłowy lub apastrofy...
  7. ?>
dr_bonzo
Maly OT:
- klasa jest tragicznie napisana
- trzymanie w ciachu ceny produktu to pomylka, przeciez user moze ja sobie zmienic na 1gr i wykupic ci pol sklepu
nikosss
a macie jakąś dobrą klase koszyka ?
jeśli tak - proszę wklejcie z przykładem jej stosowania smile.gif - np jak wrzucić produkt do koszyka

słuchajcie siedziałem siedziałem i znalazłem taką klasę :

mam ją zapisaną w pliku: shoppingcart.php i wyciągnołem ją z osCommerce-a - tam to działało - teraz ponieważ nie moge dodać wszystkiego w jednym poście to podziele to na dwa - w tym wkleiłem ta poprzerabianą klasę a za chwile wkleje opis pliku i problemu

  1. <?php
  2. if(!$session && !$ShoppingCart)
  3. {
  4. $session = md5(uniqid(rand()));
  5. $time = time();
  6. $expired_time = $time - (21600 + $site_cookie);
  7. mysql_query("DELETE FROM ".$prefix.$lg."_sklep_zakupy WHERE date<='$expired_time'");
  8. SetCookie("ShoppingCart", "$session",time()+$site_cookie);
  9. }
  10. class Cart
  11. {
  12. function add_item($prefix,$session,$product,$quan,$product_options) 
  13. {
  14. global $lg;
  15. if($quan < 1) return;
  16. $shopping_query = "SELECT * FROM ".$prefix.$lg."_sklep_zakupy WHERE session='$session' AND product='$product' AND product_options='$product_options'";
  17. $shopping_result = mysql_query($shopping_query);
  18. $number_rows = mysql_num_rows($shopping_result);
  19. if($number_rows==0)
  20. {
  21. $time = time();
  22. $product_options=addslashes($product_options);
  23. $sql = "INSERT INTO ".$prefix.$lg."_sklep_zakupy (session,product,quantity,date,product_options) VALUES ('$session','$product','$quan','$time','$product_options')";
  24. } else {
  25. $row_shopping = mysql_fetch_array($shopping_result);
  26. $quan = $quan + $row_shopping[quantity];
  27. $product_options=addslashes($product_options);
  28. $sql = "UPDATE ".$prefix.$lg."_sklep_zakupy SET quantity='$quan' WHERE session='$session' AND product='$product' AND product_options='$product_options'";
  29. }
  30. }
  31. function delete_item($prefix,$session,$product,$product_options)
  32. {
  33. global $lg;
  34. mysql_query( "DELETE FROM ".$prefix.$lg."_sklep_zakupy WHERE session='$session' AND product='$product' AND product_options='$product_options'");
  35. }
  36. function modify_quantity($prefix,$session,$prod_id,$quantity)
  37. {
  38. global $lg;
  39. $sql = "UPDATE ".$prefix.$lg."_sklep_zakupy SET quantity='$quantity' WHERE session='$session' AND id='$prod_id'";
  40. mysql_query( "$sql");
  41. }
  42. function clear_cart($prefix,$session)
  43. {
  44. global $lg;
  45. mysql_query( "DELETE FROM ".$prefix.$lg."_sklep_zakupy WHERE session='$session'");
  46. }
  47. function cart_total($prefix,$session,$sale)
  48. {
  49. global $lg;
  50. echo"XX$lg";
  51. $result = mysql_query( "SELECT * FROM ".$prefix.$lg."_sklep_zakupy WHERE session='$session'");
  52. if(mysql_num_rows($result) >0)
  53. {
  54. while($row = mysql_fetch_array($result))
  55. {
  56. $price_from_inventory = "SELECT cena FROM ".$prefix.$lg."_oferta WHERE product = '$row[product]'";
  57. $result_inventory = mysql_query("$price_from_inventory");
  58. $row_price = mysql_fetch_array($result_inventory);
  59. $cena = $row_price[cena];
  60. $product_options = $row[product_options];
  61. $product_options = split (",",$product_options);
  62. foreach ($product_options as $assign_id)
  63. { 
  64. $query_ass = "SELECT * FROM ".$prefix.$lg."_opcje where assign_id='$assign_id'";
  65. $result_ass = mysql_query($query_ass);
  66. while($row_ass = mysql_fetch_array($result_ass))
  67. {
  68. $option_price = $row_ass["option_price"];
  69. $option_symbol = $row_ass["option_symbol"];
  70. if($option_symbol=="+")
  71. {
  72. $price = $cena + $option_price;
  73. }
  74. if($option_symbol=="-")
  75. {
  76. $price = $cena - $option_price;
  77. }
  78. }
  79. }
  80. $cur_total = $cur_total + ($price * $row[quantity]);
  81. }
  82. $total = $total + $cur_total;
  83. }
  84. return sprintf("%.2f", $total);
  85. }
  86. function display_contents($prefix,$session,$sale)
  87. {
  88. global $lg;
  89. $count = 0;
  90. $result = mysql_query("SELECT * FROM ".$prefix.$lg."_sklep_zakupy WHERE session='$session'");
  91. while($row = mysql_fetch_array($result))
  92. {
  93. $result_inv = mysql_query("SELECT * FROM ".$prefix.$lg."_oferta WHERE product='$row[product]'");
  94. $row_inventory = mysql_fetch_array($result_inv);
  95. $contents["tytul"][$count] = $row_inventory[tytul];
  96. $contents["cena"][$count] = $row_inventory[cena];
  97. $contents["id"][$count] = $row[id];
  98. $contents["quantity"][$count] = $row[quantity];
  99. $contents["product_options"][$count] = $row[product_options];
  100. $contents["total"][$count] = ($row_inventory[cena] * $row[quantity]);
  101. $count ++;
  102. }
  103. $total = $this->cart_total($prefix,$session,$sale);
  104. $contents[ "final"] = $total;
  105. return $contents;
  106. }
  107. function num_items($prefix,$session)
  108. {
  109. global $lg;
  110. $result = mysql_query( "SELECT * FROM ".$prefix.$lg."_sklep_zakupy WHERE session='$session'");
  111. $num_rows = mysql_num_rows($result);
  112. return $num_rows;
  113. }
  114. }
  115. ?>
Piniek
Proszę o poprawy bbcode w pierwszym poście.
nikosss
no i teraz mam plik koszyk.php w którym mam coś takiego:

  1. <?php
  2. include("start.php");
  3. include("ustawianie.php");
  4. include("ustaw.php");
  5. include("naglowek.php");
  6. include("shoppingcart.php");
  7. artykul(950);
  8.  
  9.  
  10. $cart = new Cart;
  11.  
  12.  if($add){
  13. if($ShoppingCart){ $session = $ShoppingCart; }
  14. $result = mysql_query("SELECT * FROM ".$prefix.$lg."_oferta WHERE id='".cleanData($add,"text")."'");
  15. $row = mysql_fetch_array($result);
  16. foreach ($_POST as $varName => $value)
  17. {
  18.  $opt_short = substr($varName, 0, 7);
  19.  if($opt_short=="dyn_opt") { $product_options .= "$value,"; }
  20.  if(empty($value)){
  21. echo"<br><br>";
  22. echo"<p align=\"center\">Opcja musi mieć wartość - spróbuj ponownie !</p><br><br>";
  23. echo"<p align=\"center\"><a href=\"javascript:history.back()\">Powrót do poprzedniej strony</a></p>";
  24. include("stopka.php");
  25.  }
  26. }
  27. $options_length = strlen($product_options);
  28. $new_len = $options_length - 1;
  29. $product_options = substr($product_options, 0, $new_len);
  30. $cart->add_item($prefix,$session,$row[tytul],$quan,$product_options);
  31. $ShoppingCart=$session;
  32. if($refer_url!=="b"){
  33.  $refer_url = $_SERVER['HTTP_REFERER'];
  34.  echo "<script language=\"javascript\">window.location=\"$refer_url\"</script>";
  35. }
  36.  }
  37.  
  38.  if($remove){
  39. if($remove!=="all"){
  40.  if($ShoppingCart){ $session = $ShoppingCart; }
  41.  $result = mysql_query("SELECT * FROM ".$prefix.$lg."_oferta WHERE product='".cleanData($remove,"text")."'");
  42.  $row = mysql_fetch_array($result);
  43.  $cart->delete_item($prefix,$session,$row[id],$product_options);
  44. }
  45. if($remove=="all"){
  46.  $result = "delete FROM ".$prefix.$lg."_sklep_zakupy WHERE session='$session'";
  47.  $row = mysql_query($result);
  48. }
  49.  }
  50.  
  51.  if($modify) {
  52. if($ShoppingCart){ $session = $ShoppingCart; }
  53. $contents = $cart->display_contents($prefix,$session,$sale);
  54.  for($i = 0; $i < sizeof($quantity); $i++) {
  55.  $product_options = $contents[product_options][$i];
  56.  $oldquan = $contents[quantity][$i];
  57.  $prod_id = $contents[id][$i];
  58.  $newquan = $quantity[$prod_id];
  59.  $chars = array("-", "+", "=", "_", "/", "?", "Ł", "$", "%", "^", "&", "*", "@", "€", "|", "<", ">", "{", "}", "[", "]", "(", ")","A","B","C","D","E","F","G","H","I","F","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
  60.  $newquan = str_replace($chars,"",$newquan);
  61.  if ($newquan==0) { mysql_query( "DELETE FROM ".$prefix.$lg."_sklep_zakupy WHERE session='".cleanData($session,"text")."' AND id='".cleanData($prod_id,"text")."'"); }
  62.  $cart->modify_quantity($prefix,$session,$prod_id,$newquan);
  63.  }
  64.  }
  65.  
  66.  
  67.  
  68. $url = $PHP_SELF;
  69. if(!$ShoppingCart){
  70. $url .= "?session=$session";
  71. echo "<FORM ACTION=$url method=post>xxx</a>\n";
  72. }
  73.  
  74. if($ShoppingCart){ $session = $ShoppingCart; }
  75. $contents = $cart->display_contents($prefix,$session,$sale);
  76. if($contents[product][0] != "")
  77. { 
  78. $x = 0;
  79. while($x != $cart->num_items($prefix,$session))
  80. {
  81. $title_strip = stripslashes($contents[title][$x]);
  82.  
  83. echo"<br>produkt: $title_strip";
  84.  
  85. $current_options = $contents[product_options][$x];
  86. if(empty($current_options)){ echo"brak opcji !"; }
  87. $pass_options = $current_options;
  88. $current_options = split (",",$current_options);  
  89. $price = $contents[cena][$x];
  90. foreach ($current_options as $assign_id)
  91. { 
  92. $query = "SELECT * FROM ".$prefix.$lg."_opcje where assign_id = '$assign_id'";
  93. $result = mysql_query($query);
  94. while($row = mysql_fetch_array($result))
  95. {
  96. $option_id = $row["option_id"];
  97. $value_id = $row["value_id"];
  98. $option_price = $row["option_price"];
  99. $option_symbol = $row["option_symbol"];
  100. if($option_symbol =="+")
  101. {
  102. $price = $price + $option_price;
  103. }
  104. if($option_symbol =="-")
  105. {
  106. $price = $price - $option_price;
  107. }
  108. $query = "select * from ".$prefix.$lg."_opcje_top where option_id = '$option_id'"; 
  109. $result = mysql_query($query)or die("Error: " . mysql_error()); 
  110. while ($row = mysql_fetch_array($result))
  111. {
  112. $option_name=$row["option_name"];
  113. }
  114. $query = "select * from ".$prefix.$lg."_pl_opcje_mid where value_id = '$value_id'"; 
  115. $result = mysql_query($query)or die("Error: " . mysql_error()); 
  116. while ($row = mysql_fetch_array($result))
  117. {
  118. $value_name=$row["value_name"];
  119. }
  120. echo"<br>opcje: <b>$option_name:</b> $value_name";
  121. }
  122. }
  123. $prod_total = $price * $contents[quantity][$x];
  124. $price = sprintf("%.2f", $price);
  125.  
  126. echo"<br>wartość:PLN$price\n";
  127.  
  128. $product = $contents[product][$x];
  129. $id = $contents[id][$x];
  130. echo"ilość: <INPUT TYPE=text class=\"textbox\" size=3 name=quantity[$id] ";
  131. echo"value=\"".$contents[quantity][$x]. "\">";
  132.  
  133. $prod_total = sprintf("%.2f", $prod_total);
  134.  
  135. echo"<br>razem: <font color=\"#FF0000\"><b>PLN<br>$prod_total</b></font>\n";
  136. echo"<br><br><A HREF=\"koszyk.php?product_options=$pass_options&remove=".urlencode($contents[product][$x]);
  137. echo"".(!$ShoppingCart? "&session=$session": ""). "\">usuń</A>";
  138. $cart_total2 = $cart_total2 + $prod_total;
  139. $x ++;
  140. }
  141. echo"<br>============================================<br>";
  142.  
  143.  
  144. echo"<br><br><b>Warto&para;ć zakupów:</b><br><b>PLN".$cart->cart_total($prefix,$session,$sale). "</b><br><br>";
  145.  
  146.  
  147. $colspan = $colspan + 1;
  148.  
  149. <table align=\"center\" bgcolor=\"$bg_colour\" width=\"550\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  150. <tr align=\"center\" valign=\"middle\">
  151. <td bgcolor=\"$bg_colour\" height=\"24\" width=\"120\" background=\"images/white_button.gif\" style=\"cursor: hand;\" onClick=\"location.href='index.php?session=$session'\"><A HREF=\"index.php".(!$ShoppingCart? "?session=$session": ""). "\"><font class=\"wht_btn\">Dodaj produkty</font></A></td>
  152. <td bgcolor=\"$bg_colour\" height=\"24\" width=\"120\" background=\"images/white_button.gif\" style=\"cursor: hand;\" onClick='submit();'>
  153. <input type=\"hidden\" name=\"modify\" value=\"modify\">
  154. <a href='#'><font class=\"wht_btn\">przelicz</font></a></td>
  155. <td bgcolor=\"$bg_colour\" height=\"24\" width=\"120\" background=\"images/white_button.gif\" style=\"cursor: hand;\" onClick=\"location.href='koszyk.php?remove=all&session=$session'\"><a href=koszyk.php?remove=all&session=$session><font class=\"wht_btn\">Opróżnij koszyk</font></a></td>
  156. <td bgcolor=\"$bg_colour\" height=\"24\" width=\"120\" background=\"images/red_button.gif\" style=\"cursor: hand;\" onClick=\"location.href='customer.php?session=$session'\"><a href='customer.php?session=$session'><font class=\"red_btn\">Kasa &raquo;</font></a></td>
  157. <td bgcolor=\"$bg_colour\" height=\"24\" width=\"70\">&nbsp;</td>
  158. </tr>
  159. </table>
  160. ";
  161.  
  162. } else {
  163. echo"<p align=\"center\">Nic nie dodałeś do koszyka <br />&nbsp;<br /><a href=index.php>Wróć i wybierz produkty.</a></p>";
  164. }
  165. echo"</FORM>";
  166.  
  167. $zakupy = $_COOKIE["ShoppingCart"];
  168.  
  169. TTT$lg
  170. <form action=\"koszyk.php?add=914\" name=\"add_product\" method=\"post\">
  171. <table align=\"center\" width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  172. <tr>
  173. <td><INPUT TYPE=\"text\" class=\"textbox\" size=\"3\" value=\"1\" name=\"quan\"></td>
  174. <td height=\"24\" width=\"120\" background=\"img/x_wh.png\" valign=\"middle\" align=\"center\" style='cursor:hand;' onClick='add_product.submit();'><a href='#'><font class=\"wht_btn\">wpierdziel do koszyka</font></a></td>
  175. </tr>
  176. </table>
  177. </form>
  178. <br><br>
  179. <br>X: $zakupy
  180. <br>Y: $session
  181. <br><br>
  182. ";
  183.  
  184. include("stopka.php");
  185. ?>


no i mam problem niby to sie do bazy zapisuje ale zawsze sesja jest inna - podejrzewam ze to przez ciacho ShoppingCart bo jak sobie je chciałem wyświetlić to nic mi sie nie wyświetlilo....... i zawsze wyświetla mi sie ze pusty koszyk jest a dodaje ten sam produkt.. czy ktoś może mi pomóc ?
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.