Jump to content

jammer

Members
  • Posts

    12
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

jammer's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. hello i am trying to display items in a shopping cart but i am totally stumped for a certain part any help would be appreciated <?php //creates initial cart if(!isset($_SESSION['cart'])) $_SESSION['cart'] = array($_SESSION['id'] => $param["quantity"]); //this will overwrite the quantity if item exists in cart $_SESSION['cart'][$_SESSION['id']] = $param["quantity"]; //display the cart <? for($i=0; $i<count($_SESSION['cart']); $i++){ $items = Product::find(key($_SESSION['cart'])); ?><tr><? ?><td><?= $items['description']; ?></td><? ?><td><?= $_SESSION['cart'][$_SESSION['id']] ?></td><? ?><td><?= $items['price'] ?></td><? next($_SESSION['cart']); ?><tr><? } ?> ?> The $items finds the specific price and description for each item in the cart the problem is displaying the quantity <?= $_SESSION['cart'][$_SESSION['id']] ?> will only display the most recent quantity. I think i need another loop but not sure
  2. hello i have a question about passing parameters When i do $param = array_merge( $_GET, $_POST) some say it is ok to pass just $param while i heard i shouldn't do this example say i have an int(ID) and a decimal(price) stored in a DB and i call a function Product::updatePrice($param); To update the price is it ok to do this? someone told me i should do Product::updatePrice(intval($param["id"], and so on); I guess my problem lies where it works when i do Product::updatePrice($param); but i can't get it to work when i do it Product::updatePrice(intval($param["id"], and so on);
  3. Hello i have a reentrant page that lists a bunch of orders and i am trying to delete an order but i keep getting the error Fatal error: Method name must be a string in /model/Order.php on line 33. Using PHP5 Any help would be appreciated. here is my list of orders code: <?php //Present a list of all orders. //Select and show an order. //Give the option to delete a selected order (which represents //the order having been processed). $top = dirname(dirname(__FILE__)); require_once "$top/model/Order.php"; require_once "$top/order-helper.php"; $param = array_merge( $_GET, $_POST ); $delButton = $param["delButton"]; $pass = $param["pass"]; //$radioChk = ($pass > 0) ? $param["radioChk"] : 0; /*This checks to see if an order is selected * if not the order is initialized to zero * */ if ($pass>0) { $radioChk = $param["radioChk"]; $OrderId = Order::find($radioChk); //finds specific order info from order ID Order::delete($OrderId); } else { $radioChk = 0; } $findAllOrders = Order::findAll(); //finds all orders //$OrderId = Order::find($radioChk); $file = basename($_SERVER['PHP_SELF']); $title = "Show and Delete Orders"; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title><?= $title ?></title> </head> <body> <h2><?= $title ?></h2> <form> <? foreach ($findAllOrders as $order) { echo "<hr />"; dump_order($order); echo "<br />"; $sel = ($radioChk) ? "checked" : ""; ?> <input type="hidden" name="pass" value="1" /> <input type="radio" name="radioChk" value="<?= $order['id'] ?>" <?= $sel ?> > <input type="submit" name="delButton" value="Delete" /> <? //$radioChk = $param["$radioChk"]; echo"id: $radioChk"; //for testing ?> OrderId: <?= $OrderId['id'] ?> <? } ?> </form> </table> </body> </html> for the above code i don't think i need the $OrderID for Order::delete($OrderId); I think i can just do Order::delete($param); But it still gives the same fatal error. here is my MySQL order code: <?php require_once "db/Database.php"; class Order { private static $table = "orders"; private static $table_def = "id INT AUTO_INCREMENT NOT NULL, user VARCHAR(20), items MEDIUMBLOB, created DATETIME, PRIMARY KEY(id)"; public function delete( $order ) { $cx = Database::connect(); $table = self::$table; $id = $order['radioChk']; if (!$cx->$query( "DELETE FROM $table WHERE id='$id'" ) ) throw new Exception($cx->error); } } ?> there error says it starts on line 33 (where the if statement starts) Any ideas?
  4. Hey i have a general question about sessions and when i should start the session. Say i have a page that user can look at to add a product (add to shopping cart). Should i start the session here? or should i wait and start the session on the next page where the user enters the quantity they want? or should i wait and start the session only after the user enters the quantity they want? This is where i thought it should be but wouldn't it be easier to start the session from the very beginning?
  5. yea thanks i figured that out but now for some reason it just isn't updating the price, well the variable is updating just not to the DB, i dunno too tired, i'll try to figure it out tomorrow
  6. hey i am trying to call a function to update a price i have tried a few different things and still no luck, any ideas? when i do the following it updates my prices but changes all the prices in the DB to 0.00 public function updatePrice( $product ) { $cx = Database::connect(); $table = self::$table; $price = $product['price']; if ( !$cx->query( "UPDATE $table SET price = '$price'") ) throw new Exception($cx->error); } When i do the following(also tried some other commands) it normally gives the following error: Fatal error: Uncaught exception 'Exception' with message 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(price) VALUES ('')' at line 1' in public_html/PhpStore/model/Product.php:34 Stack trace: #0 /public_html/PhpStore/admin/upPrice.php(10): Product::updatePrice(Array) #1 {main} thrown in /public_html/PhpStore/model/Product.php on line 34 public function updatePrice( $product ) { $cx = Database::connect(); $table = self::$table; $price = $product['price']; if ( !$cx->query( "UPDATE $table (price) VALUES ('$price')") ) throw new Exception($cx->error);
  7. laffin i don't want the checkbox to really display text maybe i am confused but i want the checkbox value to store price and id i already found the price and description from the DB in <html> <head> <title>Show Products</title> <style> @import url(my.css); </style> </head> <body> <? require_once "model/Product.php"; $all = Product::findAll(); ?> <? require_once "nav.php" ?> <h3>the products</h3> <form action ="addTo-cart.php"> <table border="1" cellpadding="5px"> <tr> <th>id</th> <th>description</th> <th>price</th> </tr> <? foreach ($all as $product): ?> <tr> <td><?= $product['id'] ?></td> <td><?= htmlspecialchars($product['description']) ?></td> <td><?= '$' . $product['price'] ?></td> <td> <input type="checkbox" name="chkbox[$price]" value="<?= $product['description'] ?>"> <input type="submit" name="addTo_cart" value="Add to Cart"/> </td> </tr> <? endforeach ?> </table> </form> where in the for each loop it finds the price and description from the DB and displays the products the checkbox is to buy the item(add it to the cart) so i want to save both of these values for that specific item any idea
  8. hey ray if i do, <input type="checkbox" name="chkbox" value="<?= $product['price'] ?><?= $product['description'] ?>"> and then print_r($_GET["chkbox"]); it will print out both values, however it is both values at the same time for example: 3.99 Terdsandwich now say i want to print 3.99 and then print "is ridiculous for a " Terdsandwich but there is no way for me to add anything between 3 and sandwich it just automatically does price description and your way just prints out Array([$price] => terdsandwich
  9. Hi i was wondering is there any way to store multiple variables to a value with a checkbox. I know you can store more then 1 but is there anyway to have them separate like value"1", value"2" and do on. <input type="checkbox" name="chkbox" value="<?= $product['price'] ?><?= $product['description'] ?>"> i can store these two but say if i want to echo "chkbox" on a separate page it prints both out automatically, is there anyway i can print price and then be able to print the description later? thanks
  10. hey thanks for the reply that was an error that i was over looking . But i guess my real question was how to pass parameters but i finally figured it out, thanks for the help.
  11. Hello I am a newbie working with PHP and SQL using MySQLi API and am really confused at how to call a certain function. I need to add a product to the data base with a price and description. The error is in the addProduct-handler.php @ Product::create($product); I am not even sure if i am callin it right, but it keeps coming up with this error: Fatal error: Uncaught exception 'Exception' with message 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 2' in /PhpStore/model/Product.php:26 Stack trace: #0 /PhpStore/admin/addProduct-handler.php(25): Product::create(NULL) #1 {main} thrown in /PhpStore/model/Product.php on line 26 now I am pretty sure the SQL is correct because i tested it and it allows me to insert, select, and drop tables. Does anyone have any ideas, thanks in advance for the help? <?php //new-product.php $file = basename($_SERVER['PHP_SELF']); $title = "Add a new product ($file)"; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN"> <html> <head> <title><?= $title ?></title> </head> <body> <h2><?= $title ?></h2> <form action="addProduct-handler.php"> <table class="entry"> <tr> <td class="left">Price</td> <td><input type="text" name="price"/></td> </tr> <tr valign="top"> <td class="left">Description</td> <td><textarea name="dsc" rows="8"></textarea></td> </tr> </table> <input type="submit" name="add_product" value="Add Product"/> </form> </body> </html> <?php //addProduct-handler.php $top = dirname(dirname(__FILE__)); require "$top/model/Product.php"; $param = array_merge( $_GET, $_POST ); $price = $param["price"]; $dsc = $param["dsc"]; $esc_price = htmlspecialchars($price); $esc_dsc = htmlspecialchars($dsc); $title = "Added new Product"; $file = basename($_SERVER['PHP_SELF']); $title .= "($file)"; echo "<h2>$title</h2>"; echo "price: $esc_price<br />"; //just for testing echo "description: $esc_dsc<br />"; echo "***Add Product<br />"; Product::create($product); ?> <?php //Product.php require_once "db/Database.php"; class Product { private static $table = "products"; private static $table_def = "id INT AUTO_INCREMENT NOT NULL, description TEXT, price DECIMAL(9,2), PRIMARY KEY(id)"; public function createTable() { $cx = Database::connect(); $table = self::$table; $table_def = self::$table_def; $cx->query( "DROP TABLE IF EXISTS $table" ) or die($cx->error); $cx->query( "CREATE TABLE $table ($table_def)" ) or die($cx->error); } public function create( $product ) { $cx = Database::connect(); $table = self::$table; $description = addslashes($product['description']); $price = $product['price']; if ( !$cx->query( "INSERT INTO $table (description,price) VALUES ('$description', $price)") ) throw new Exception($cx->error); } public function find( $id ) { $cx = Database::connect(); $table = self::$table; $res = $cx->query("SELECT * FROM $table WHERE id=$id") or die($cx->error); return $res->fetch_assoc(); } public function findAll( $fields = null ) { // if null, assume all fields $cx = Database::connect(); $table = self::$table; $field_list = ($fields != null) ? join(",", $fields) : "*"; $res = $cx->query( "SELECT $field_list FROM $table" ) or die($cx->error); $all = array(); while ($map = $res->fetch_assoc()) $all[] = $map; return $all; } } ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.