Jump to content

seaweed

Members
  • Posts

    64
  • Joined

  • Last visited

    Never

Everything posted by seaweed

  1. Can anyone point out why this isn't incrementing the qty of items already in the cart? Maybe it's a Friday Brain Fart... session_start(); if (isset($_SESSION['CART']) == false || isset($_SESSION['CART']['ITEMS']) == false) { $_SESSION['CART']['ITEMS'] = array(); } foreach ($_POST AS $sku => $qty) { if (!empty($qty)) { // if the product sku is already in the cart, do this // foreach ($_SESSION['CART']['ITEMS'] as $cart_item => $item) { if ($item['item_sku'] == $sku) { $_SESSION['CART']['ITEMS'][$cart_item]['item_qty'] = $_SESSION['CART']['ITEMS'][$cart_item]['item_qty'] + $qty; $found = true; break; } } // if the product sku is not already in the cart, do this // if (!$found) { $_SESSION['CART']['ITEMS'][] = array('item_sku' => $sku, 'item_qty' => $qty); } } }
  2. I have no idea why the above didn't work but I put a quick hack in and it worked... if ($category == 0) { $this->_category = "5000"; $this->_whereCat = "post_cat NOT LIKE :category"; } else { $this->_category = $category; $this->_whereCat = "post_cat = :category"; } $this->stmt = $this->_dbh->prepare("SELECT * FROM posts WHERE ... AND ($this->_whereCat) AND ..."); That seems to work since we only have 60 categories now and will never have over 100, so any categoryid not 5000 shows up.
  3. I tried that but it didn't work... if I choose a category it works, but if I don't (it is set to 0 if it is empty) I get no results... $statement = "SELECT * FROM posts WHERE"; if ($this->_category == 0) { $statement .= " (post_price < :price) AND (post_class = :class) AND (MATCH (post_title, post_description) AGAINST (+:keywords IN BOOLEAN MODE))"; } else { $statement .= " (post_cat = :category) AND (post_price < :price) AND (post_class = :class) AND (MATCH (post_title, post_description) AGAINST (+:keywords IN BOOLEAN MODE))"; } $this->stmt = $this->_dbh->prepare($statement); Say I choose the category 'apples' and I get 6 results.... if I don't choose a category, I should at least get those same 6 results plus results from all other categories with the words apples in the description or title... but if I don't choose a category I get nadda...
  4. How would you do this if you were using prepared statements, since you can't build your queries and then insert them in prepared statements? Say you have this query: $this->stmt = $this->_dbh->prepare("SELECT * FROM table WHERE (name = :name) AND (city = :city)"); How would you write a conditional for city being empty? I've tried this with no luck: $this->stmt = $this->_dbh->prepare("SELECT * FROM posts WHERE (name = :name) IF($city == "", '', 'AND (city = :city)"); Any ideas? I have the same problem, a value that may be left blank, but in prepared statements you can't build queries and then insert them, it errors out.
  5. Why are sessions problematic? When you send the form, on the receiving page just create temp variables like this: if ($error_exists or some other condition) { $_SESSION['username'] - $_POST['username']; $_SESSION['phone'] - $_POST['phone']; header("Location: form.html"); exit; } and then on the form itself echo out the values if they are not empty like this: <form> <input type="text" name="username" value="<?php echo $_SESSION['username']; ?>" /> <input type="text" name="phone" value="<?php echo $_SESSION['phone']; ?>" /> </form> and then maybe kill the session values so they don't get re-used $_SESSION['username'] = null; $_SESSION['phone] = null;
  6. I have a database class that uses PDO prepared statements. The connection is like this: public function dbConnect() { $this->_dbh = new PDO('mysql:host=localhost; dbname=db_name', 'username', 'password'); } I have a few questions about it. 1. Should I put this code in my constructor method? 2. Should I set $dbh = null; at the end of the script or leave it open? This application will be hammering this class with queries non-stop, it's the crux of the website, looking up products in a catalog.
  7. Here's what I ended up with for the updatecart script, it works flawlessly so far. Do you see anything wrong that I'm missing? I need to account for items w/o a color or size etc. still, I can see that. It's got more nests than a birdhouse but it works great - think/hope/pray! <?php session_start(); if(isset($_SESSION['CART']) == false || isset($_SESSION['CART']['ITEMS']) == false) { $_SESSION['CART']['ITEMS'] = array(); } $do = $_POST['do']; // this is like add, subtract or remove $prod_id = $_POST['prod_id']; $prod_color = $_POST['prod_color']; $prod_size = $_POST['prod_size']; if (($prod_id == "") || ($prod_id == "0") || ($prod_id == NULL)) { echo "prod_id left blank"; // this verbiage is just for development, I'll change it later } elseif (is_numeric($prod_id) == false) { echo "is_numeric failed on prod_id"; // this verbiage is just for development, I'll change it later } else { $found = false; foreach($_SESSION['CART']['ITEMS'] as $cart_item_id => $item) { if(($item['prod_id'] == $prod_id) && ($item['prod_color'] == $prod_color) && ($item['prod_size'] == $prod_size)) { switch ($do) { case "add": $_SESSION['CART']['ITEMS'][$cart_item_id]['prod_qty']++; $found = true; break; case "sub": if($_SESSION['CART']['ITEMS'][$cart_item_id]['prod_qty'] <= 1) { unset($_SESSION['CART']['ITEMS'][$cart_item_id]); $found = true; break; } else { $_SESSION['CART']['ITEMS'][$cart_item_id]['prod_qty']--; $found = true; break; } // closes inner else case "rem": unset($_SESSION['CART']['ITEMS'][$cart_item_id]); $found = true; break; } // closes switch $found = true; } // Closes if } // closes foreach } // closes outter else header("Location: cart.php"); exit; ?> ?>
  8. BTW, whenever I sit down to start a new project, I ALWAYS start by jamming to some old Blink 182 tunes, stuff from Dude ranch or Cheshire cat, it reminds me of the dotcom era when we all had jobs! Every time I get started, I open up Aliens Exist or Dammit and get in the mood.
  9. That makes sense, thank you. it worked and now I know.
  10. Okay, error fixed. Now, it isn't echoing anything to the cart. Here's the various parts: header.php <?php session_start(); if(isset($_SESSION['CART']) == false || isset($_SESSION['CART']['ITEMS']) == false) { $_SESSION['CART']['ITEMS'] = array(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> addtocart.php <?php session_start(); if(isset($_SESSION['CART']) == false || isset($_SESSION['CART']['ITEMS']) == false) { $_SESSION['CART']['ITEMS'] = array(); } // This data needs cleaned before it goes to SQL!!!!! // $prod_id = $_POST['prod_id']; $prod_qty = $_POST['prod_qty']; $prod_size = $_POST['prod_size']; $prod_color = $_POST['prod_color']; foreach($_SESSION['CART']['ITEMS'] as $cart_item_id => $item) { if($item['prod_id'] == $prod_id) { $_SESSION['CART']['ITEMS'][$cart_item_id]['prod_qty']++; } else { $_SESSION['CART']['ITEMS'][] = array( 'prod_id' => $prod_id, 'prod_qty' => $prod_qty, 'prod_size' => $prod_size, 'prod_color' => $prod_color ); } } header("Location: cart.php"); exit; ?> cart.php <?php include ('header.php') ?> <div class="main"> <?php include ('leftcol.php') ?> <div class="catbody"> <div class="cart"> <div class="cartheaderrow"> <div class="cartheaderrow_id">ID</div> <div class="cartheaderrow_desc">Description</div> <div class="cartheaderrow_qty">Qty</div> <div class="cartheaderrow_price">Price</div> <div class="cartheaderrow_subtotal">Subtotal</div> <div class="cartheaderrow_update"></div> </div> <?php foreach($_SESSION['CART']['ITEMS'] as $item) { echo " <div class=\"cartrow\"> <div class=\"cartrow_id\">$item[prod_id]</div> <div class=\"cartrow_desc\">$item[prod_color] $item[prod_size]</div> <div class=\"cartrow_qty\">$item[prod_qty]</div> <div class=\"cartrow_price\">xxx</div> <div class=\"cartrow_subtotal\">xxx</div> <div class=\"cartrow_update\">+ / -</div> </div> "; } ?> </div> </div> </div> <?php include ('footer.php') ?> </div> </BODY> </HTML>
  11. I tried that too, befor eyou posted, but I still get the error. It's weird... The header is like this: <?php session_start(); if(isset($_SESSION['CART']) == false || isset($_SESSION['CART']['ITEMS'] == false)) { $_SESSION['CART']['ITEMS'] = array(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>
  12. That throws a parse error: Parse error: syntax error, unexpected T_IS_EQUAL, expecting ',' or ')' in header.php on line 3 line 3 is: if(isset($_SESSION['CART']) == false || isset($_SESSION['CART']['ITEMS'] == false) I tried moving the parens around with no luck.
  13. Okay, brb... let me finish killing this quessadilla and I'll try that.
  14. header.php starts with... <?php session_start(); if (! $_SESSION[active]) { $_SESSION[active] = 1; } ?>
  15. <?php include ('header.php') ?> <div class="main"> <?php include ('leftcol.php') ?> <div class="catbody"> <div class="cart"> <div class="cartheaderrow"> <div class="cartheaderrow_id">ID</div> <div class="cartheaderrow_desc">Description</div> <div class="cartheaderrow_qty">Qty</div> <div class="cartheaderrow_price">Price</div> <div class="cartheaderrow_subtotal">Subtotal</div> <div class="cartheaderrow_update"></div> </div> <?php foreach($_SESSION['CART']['ITEMS'] as $item) { echo " <div class=\"cartrow\"> <div class=\"cartrow_id\">$item[prod_id]</div> <div class=\"cartrow_desc\">$item[prod_color] $item[prod_size]</div> <div class=\"cartrow_qty\">$item[prod_qty]</div> <div class=\"cartrow_price\">xxx</div> <div class=\"cartrow_subtotal\">xxx</div> <div class=\"cartrow_update\">+ / -</div> </div> "; } ?> </div> </div> </div> <?php include ('footer.php') ?> </div> </BODY> </HTML>
  16. Okay here's what I have: addtocart.php $prod_id = $_POST['prod_id']; $prod_qty = $_POST['prod_qty']; $prod_size = $_POST['prod_size']; $prod_color = $_POST['prod_color']; foreach($_SESSION['CART']['ITEMS'] as $cart_item_id => $item) { if($item['prod_id'] == $prod_id) { $_SESSION['CART']['ITEMS'][$cart_item_id]['prod_qty']++; break; } else { $_SESSION['CART']['ITEMS'][] = array( 'prod_id' => $prod_id, 'prod_qty' => $prod_qty, 'prod_size' => $prod_size, 'prod_color' => $prod_color ); } } header("Location: cart.php"); exit; cart.php foreach($_SESSION['CART']['ITEMS'] as $item) { echo " <div class=\"cartrow\"> <div class=\"cartrow_id\">$item[prod_id]</div> <div class=\"cartrow_desc\">$item[prod_color] $item[prod_size]</div> <div class=\"cartrow_qty\">$item[prod_qty]</div> <div class=\"cartrow_price\">xxx</div> <div class=\"cartrow_subtotal\">xxx</div> <div class=\"cartrow_update\">+ / -</div> </div> "; } The error I get, assuming it's a logic problem with the array? Warning: Invalid argument supplied for foreach() in cart.php on line 17
  17. Few more quick questions... 1. What's the best way to initiate the session for this application? Currently all of my pages use this in a header include file: session_start(); if (! $_SESSION[active]) { $_SESSION[active] = 1; } I just creates the dummy variable $active. When you get the the addtocart.php script though, it doesn't have a header as there's no html, should I just use this same session check, or actually check for the array like this? if (!isset($_SESSION['CART']['ITEMS']) || (!is_array($_SESSION['CART']['ITEMS']))) { $_SESSION['CART']['ITEMS'] = array(); I'm not sure on the way to do this or syntax... 2. What's the best way to check if the item already exists in the cart and increment it, or decrease it if they want to remove it or subtract one? Maybe use in_array()?
  18. It's basically like playing catch with a ball (the patient_id variable) and two people (the two scripts).... The first person throws the ball up in the air (into the URL) and the second person catches it, then throws it back into the URL etc... as long as you're passing it back and forth correctly it should work. If not, try echoing the value of the patent_id variable instead of using the header () part, to see if it has a value.
  19. What he said - pass it with the hidden input field and use $Patient_ID = $_POST['Patient_ID']; to get the value. And then after the UPDATE SQL part, use a header redirect like this: header("Location: patient_form_edit.php?Patient_ID=$Patient_ID); exit; ?> Then when you go back to that page, $Patient_ID=$_GET['Patient_ID']; will grab the Patient_ID value from the URL and use it to re-query the database and display the data on the edit form.
  20. session_register is depreciated... http://us2.php.net/session_register
  21. but... I need to switch over to persistent connection with mysql_pconnect because that will stay connected from page to page, correct? Even if I do not re-connect every page that needs it?
  22. I'm using the POST method to pass data to a script, and then putting the data into a $_SESSION array. Ultimately the data will be put into an SQL database, so i want to cleanse it with mysql_real_escape_string, but when I use mysql_real_escape_string and then put the data into the $_SESSION array, all of the variables are empty. For example, this works: $prod_id = $_POST['prod_id']; $prod_qty = $_POST['prod_qty']; $prod_size = $_POST['prod_size']; $prod_color = $_POST['prod_color']; $_SESSION['CART']['ITEMS'][] = array( 'prod_id' => $prod_id, 'prod_qty' => $prod_qty, 'prod_size' => $prod_size, 'prod_color' => $prod_color ); This does not: $prod_id = mysql_real_escape_string($_POST['prod_id']); $prod_qty = mysql_real_escape_string($_POST['prod_qty']); $prod_size = mysql_real_escape_string($_POST['prod_size']); $prod_color = mysql_real_escape_string($_POST['prod_color']); $_SESSION['CART']['ITEMS'][] = array( 'prod_id' => $prod_id, 'prod_qty' => $prod_qty, 'prod_size' => $prod_size, 'prod_color' => $prod_color ); Is there a reason why? Is there a better way to clean the data before I stuff it in the session?
  23. Okay, perfect. I tested it and it works: (I know, I need to sanitize my input)... <?php session_start(); if (! $_SESSION[active]) { $_SESSION[active] = 1; } $id = $_POST['id']; $qty = $_POST['qty']; $size = $_POST['size']; $color = $_POST['color']; $_SESSION['CART']['ITEMS'][] = array( 'product_id' => $id, 'product_qty' => $qty, 'product_size' => $size, 'product_color' => $color ); header("Location: shoppingcart.php"); ?> <?php foreach($_SESSION['CART']['ITEMS'] as $item) { echo "product_id: " . $item['product_id'] . " "; echo "qty: " . $item['product_qty'] . " "; echo "size: " . $item['product_size'] . " "; echo "color: " . $item['product_color'] . " "; echo "<br /><br />"; } ?> This outputs: product_id: 31 qty: 21 size: Small color: Blue product_id: 32 qty: 17 size: Medium color: Red product_id: 33 qty: 5 size: Medium color: Blue product_id: 34 qty: 7 size: X-Large color: White So, I can place an SQL insert statement in the foreach loop to insert a new row in my orderItems table for each product, correct? If it's that simple, I'm done! Echoing the cart to the user is easy as pie.
×
×
  • 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.