Jump to content

seaweed

Members
  • Posts

    64
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

seaweed's Achievements

Member

Member (2/5)

0

Reputation

  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>
×
×
  • 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.