Jump to content

KevinM1

Moderators
  • Posts

    5,222
  • Joined

  • Last visited

  • Days Won

    26

Everything posted by KevinM1

  1. KevinM1

    eregi error

    Unfortunately, I'm still getting the same error.
  2. I have a form that can only be accessed if the correct $_GET values are passed to its script.  If I wanted to make a sicky form, would setting the form's action to <?php echo $_SERVER['REQUEST_URI'] ?> re-send the $_GET info?
  3. KevinM1

    eregi error

    I'm currently trying to write some form validation into a script, but the eregi function keeps choking on one of my expressions.  I keep getting the error: [quote]Warning: eregi() [function.eregi]: REG_ERANGE in /home/thinkin8/public_html/checkout.php on line 18[/quote] This error occurs where I try checking an address for validity.  My code is: [code] <?php #checkout.php $errMessage = NULL; $mailMessage = NULL; if(isset($_POST['submit'])){   if(!empty($_POST['name']) && eregi("^[a-zA-Z]+([ a-zA-Z-]+)*$", $_POST['name'])){       $name = $_POST['name'];       $n = TRUE;   }   else{       $errMessage .= "Please enter your name!<br />\n";   }   if(!empty($_POST['address1']) && eregi("^[0-9a-zA-Z.- ]+$", $_POST['address1'])){       $address1 = $_POST['address1'];       $a1 = TRUE;   }   else{       $errMessage .= "Please enter your address!<br />\n";   }   if(!empty($_POST['address2']) && eregi("^[0-9a-zA-Z.- ]+$", $_POST['address2'])){       $address2 = $_POST['address2'];   }   else{       $address2 = '';   }   if(!empty($_POST['city']) && eregi("^[a-zA-Z.- ]+$", $_POST['city'])){       $city = $_POST['city'];       $c = TRUE;   }   else{       $errMessage .= "Please enter your city!<br />\n";   }   if(!empty($_POST['state']) && eregi("^[a-zA-Z]{2}$", $_POST['state'])){       $state = $_POST['state'];       $s = TRUE;   }   else{       $errMessage .= "Please enter your state!<br />\n";   }   if(!empty($_POST['zipcode']) && eregi("^[0-9]{5}[-0-9]{4}?$", $_POST['zipcode'])){       $zipcode = $_POST['zipcode'];       $z = TRUE;   }   else{       $errMessage .= "Please enter your zipcode!<br />\n";   }   if($n && $a1 && $c && $s && $z){       $mailMessage .= "$name\n$address1\n$address2\n$city, $state $zipcode";       mail('kevin@thinkingmachineonline.com', 'Test message', $mailMessage);       echo "Your mail has been sent, $name<br />\n";   }   else{       echo "<div style='color: red;'>$errMessage</div>\n";   } } ?> <form name="checkout" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">   Name: <input type="text" name="name" /><br />   Address 1: <input type="text" name="address1" /><br />   Address 2: <input type="text" name="address2" /><br />   City: <input type="text" name="city" /><br />   State: <input type="text" name="state" /><br />   Zipcode: <input type="text" name="zipcode" /><br />   <input type="submit" name="submit" value="Checkout" /> </form> [/code] I'm just trying to see if the user entered in normal address stuff (alphanumeric characters, and the period, hyphen, and space), so I'm a bit at a loss as to why this error is appearing, especially when my first regex, which validates a person's name, works correctly.  Please help.
  4. Thanks for your help, it appears to be working correctly now. I know my code is a bit...jumbled, at least with the showCart method.  I broke the form up unto two pieces (the form tags in viewcart.php, the inputs in the class) because I wasn't sure if the form would use the right action if I put all of the form's code in the showCart method.  In other words, I thought that the <?php echo $_SERVER['PHP_SELF'] ?> bit would refer to the shopping cart class file, rather than the viewcart file. If you have any suggestions on how to clean up that bit of code (or anything else that's messy), I'd love to hear it. And yeah, this thread should probably be moved over to the general PHP Help area.
  5. There's a tutorial here: http://www.phpfreaks.com/tutorials/147/0.php
  6. I've almost got it working.  I decided to use the checkbox method -- unfortunately, it doesn't always remove what I want, often just leaving the item(s) I've checked in the shopping cart.  I'm not getting any errors, though, unless I click on the 'Remove Checked Items' submit button when there's nothing in the cart at all, so I think I'm on the right track (and that's an easy error to remedy, regardless).  I think my problem is stemming from it being a sticky form.  I can usually remove something from the cart when I first go to my viewcart.php script, but all subsequent attempts fail.  Any ideas?  I'll post my code below. My modified ShoppingCart class: [code] <?php class ShoppingCart{    private $Cart = array();    public function addProduct($name, $price, $quantity){       $this -> Cart[] = new Product($name, $price, $quantity);    }    public function removeProduct($key){       unset($this -> Cart[$key]);    }    public function clearCart(){       $this -> Cart = array();    }    public function getTotal(){       $total = 0;       foreach($this -> Cart as $value){          $total += $value -> getPrice();       }       return $total;    }    public function showCart(){       echo "<table><tr><th>Name:</th><th>Quantity:</th><th>Total Price:</th></tr>\n";       foreach($this -> Cart as $value){          echo "<tr><td><input type='checkbox' name='tmpCart[]' />{$value->getName()}</td><td>{$value->getQuan()}</td><td>\${$value->getPrice()}</td></tr>\n";       }       echo "</table>\n";    } } ?> [/code] My viewcart.php file: [code] <?php include('../php_config/config.php'); session_start(); ob_start(); if(!isset($_SESSION['ip'])){    $ip = $_SERVER['REMOTE_ADDR'];    $myCart = new ShoppingCart(); } else{    if($_SERVER['REMOTE_ADDR'] != unserialize(urldecode($_SESSION['ip']))){       $ip = $_SERVER['REMOTE_ADDR'];       $myCart = new ShoppingCart();    }    else{       $ip = unserialize(urldecode($_SESSION['ip']));       $myCart = unserialize(urldecode($_SESSION['myCart']));    } } if(isset($_POST['submit'])){    foreach($_POST['tmpCart'] as $key => $value){       if(!empty($value)){          $myCart -> removeProduct($key);       }    } } if(isset($_POST['clear'])){    $myCart -> clearCart(); } include('../templates/header.inc'); echo "<form name='shoppingcart' action='{$_SERVER['PHP_SELF']}' method='POST'><br />\n"; $myCart -> showCart(); echo "<input type='submit' name='submit' value='Remove Checked Items' />\n<input type='submit' name='clear' value='Remove All Items' />\n</form><br />\n"; include('../templates/footer.inc'); ob_end_flush(); $_SESSION['ip'] = urlencode(serialize($ip)); $_SESSION['myCart'] = urlencode(serialize($myCart)); ?> [/code] EDIT: After some more testing, I think my problem is actually caused by my tmpCart array not staying matched up with myCart's internal array when individual items are deleted, but I'm not sure how to fix it.
  7. Hmm...I might use the checkbox method so one doesn't have to delete each item at a time if they have multiple items they want to remove.  Actually, that would probably be easier, as I can just use the cart's key values as the checkbox values, and unset those items that way.
  8. [quote author=utexas_pjm link=topic=119145.msg487819#msg487819 date=1166489503] Use the index of the item in your internal array, $cart.  The code would looks something like this: [code] <?PHP ... deleteItem($index){   unset($this->Cart[$index]); } ... ?> [/code] [/quote] True, but that's not what I was really asking. ;) Say my showCart method is modified as follows: [code] <?php public function showCart(){   echo "<table><tr><th>Name:</th><th>Quantity:</th><th>Total Price:</th></tr>\n";   foreach($this -> Cart as $key => $value){       echo "<tr><td><input type='submit' name='submit{$key}' value='Delete' /></td><td>{$value->getName()}</td><td>{$value->getQuan()}</td><td>\${$value->getPrice()}</td></tr>\n";   }   echo "</table>\n"; } ?> [/code] And the script that displays the cart is something like: [code] <?php include('../php_config/config.php'); session_start(); ob_start(); //do session validation/verification here, and retrieve $myCart if(isset($_POST['submit'])){ // <-- trouble area   //remove item } //show the form regardless ?> <form name="cart" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST"> <?php $myCart -> showCart(); ?> </form> [/code] How can I get a hold of submit{$key}, which comes from the showCart method, in order to check if it's set or not?  Since my cart has no size limit, it would be inefficient to loop through the entire cart, wouldn't it?  Or is that really the only way I can get a hold of the right item to delete?
  9. I have a very simple shopping cart class that is going to be used for my work's e-commerce site.  The class is as follows: [code] <?php class ShoppingCart{   private $Cart = array();   public function addProduct($name, $price, $quantity){       $this -> Cart[] = new Product($name, $price, $quantity);   }   public function clearCart(){       $this -> Cart = array();   }   public function getTotal(){       $total = 0;       foreach($this -> Cart as $value){         $total += $value -> getPrice();       }       return $total;   }   public function showCart(){       echo "<table><tr><th>Name:</th><th>Quantity:</th><th>Total Price:</th></tr>\n";       foreach($this -> Cart as $value){         echo "<tr><td>{$value->getName()}</td><td>{$value->getQuan()}</td><td>\${$value->getPrice()}</td></tr>\n";       }       echo "</table>\n";   } } ?> [/code] I'd like to add an Amazon.com-esque ability to remove individual items from the cart.  I'm thinking that I can add a 'delete' button to each item in the showCart method, but that's where I'm encountering my problem. From what I can see, Amazon handles removing items by having each 'delete' button (visible when you view your shopping cart) as a form submission button.  My only problem is with delete button handling upon submission.  Each button has a value of delete#, with # = i+1.  How would I get a hold of the right delete value on submission?
  10. [quote author=Orio link=topic=119139.msg487489#msg487489 date=1166465137] Removed :) Orio. [/quote] Sweet, thanks. :)
  11. You're probably looking for the nl2br function.  It transforms newlines (\n) into HTML's non-breaking whitespace (<br />).
  12. Will the unset array elements be physically removed from the array, or merely turned into null values?  I ask because I plan on using a foreach on the modified array after the proper elements are removed, and thus I don't want to have any null-value gaps in the datastructure.
  13. Is there a function that removes/deletes an item from an array, returning the newly reduced array and not the element that was removed/deleted?
  14. [quote author=redbullmarky link=topic=119095.msg487249#msg487249 date=1166446619] the invalid argument would be a result of the foreach being passed something other than an array (including a null value). you can also try changing the $Cart declaration in your class to: [code] <?php    private $Cart = array(); ?> [/code] [/quote] That did the trick!  Thanks a lot! :)
  15. Yes, this is the same one as before, but I figure I might get a response if I put all of the info in one post. I'm getting the following warning: [quote]Warning: Invalid argument supplied for foreach() in /home/thinkin8/php_classes/ShoppingCart.php on line 27[/quote] This is the code utilizing the ShoppingCart class (it's autoloaded through the included config.php): [code] <?php include('../php_config/config.php'); session_start(); ob_start(); if(!isset($_SESSION['ip'])){   $ip = $_SERVER['REMOTE_ADDR'];   $myCart = new ShoppingCart(); } else{   if($_SERVER['REMOTE_ADDR'] != unserialize(urldecode($_SESSION['ip']))){       $ip = $_SERVER['REMOTE_ADDR'];       $myCart = new ShoppingCart();   }   else{       $ip = unserialize(urldecode($_SESSION['ip']));       $myCart = unserialize(urldecode($_SESSION['myCart']));   } } include('../templates/header.inc'); echo "IP address: $ip<br />\n"; $myCart -> showCart(); echo <<<EOT <div>   <img src="images/store/storefront_01.jpg" alt="" /><img src="images/store/storefront_02.jpg" alt="" /> </div> <div>   <a href="viewcat.php?cat=acer_laptops"><img src="images/store/storefront_05.jpg" alt="" /></a>   <img src="images/store/storefront_06.jpg" alt="" />   <a href="viewcat.php?cat=lenovo_laptops"><img src="images/store/storefront_04.jpg" alt="" /></a> </div> <div>   <a href="viewcat.php?cat=averatec_laptops"><img src="images/store/storefront_09.jpg" alt="" /></a>   <a href="viewcat.php?cat=panasonic_laptops"  style="margin-left: 68px;"><img src="images/store/storefront_11.jpg" alt="" /></a> </div> <div>   <img src="images/store/storefrontbar.jpg" alt="" /> </div> <div>   <a href="viewcat.php?cat=acer_desktops"><img src="images/store/storefront_05.jpg" alt="" /></a>   <img src="images/store/desktop.jpg" style="margin-left: 25px; alt="" />   <a href="viewcat.php?cat=lenovo_desktops" style="margin-left: 30px;"><img src="images/store/storefront_04.jpg" alt="" /></a> </div> <div>   <a href="#"><img src="images/store/storefront_19.jpg" alt="" /></a>   <a href="viewcat.php?cat=dandh_desktops" style="margin-left: 53px;"><img src="images/store/storefront_21.jpg" alt="" /></a> </div> <div>   <img src="images/store/storefrontbar.jpg" alt="" /> </div> <div>   <a href="viewcat.php?cat=peripherals"><img src="images/store/storefront_24.jpg" alt="" /></a> </div> <div>   <img src="images/store/storefront_25.jpg" alt="" /> </div> EOT; include('../templates/footer.inc'); $_SESSION['ip'] = urlencode(serialize($ip)); $_SESSION['myCart'] = urlencode(serialize($myCart)); ob_end_flush(); ?> [/code] This is the ShoppingCart class itself.  As you can see, line 27 is the foreach of the showCart method [code] <?php class ShoppingCart{   private $Cart;   public function addProduct($name, $price, $quantity){       $this -> Cart[] = new Product($name, $price, $quantity);   }   public function clearCart(){       $this -> Cart = array();   }   public function getTotal(){       $total = 0;       foreach($this -> Cart as $value){         $total += $value -> getPrice();       }       return $total;   }   public function showCart(){       echo "<table><tr><th>Name:</th><th>Quantity:</th><th>Total Price:</th></tr>\n";       foreach($this -> Cart as $value){         echo "<tr><td>{$value->getName()}</td><td>{$value->getQuan()}</td><td>\${$value->getPrice()}</td></tr>\n";       }       echo "</table>\n";   } } ?> [/code] Another poster suggested that having a $myCart variable with an empty $Cart property was causing the warning.  This is not the case, however, as my test script, with an empty cart, generates no warnings whatsoever.  My test code: [code] <?php #test3.php -- test to see if the shopping cart works include('../php_config/config.php'); $cart = new ShoppingCart(); $cart -> addProduct('tomato', 0.99, 5); $cart -> addProduct('Nike Shoes', 50.99, 1); $cart -> addProduct('Playstation 3', 600.00, 1); $cart -> showCart(); $total = $cart -> getTotal(); echo "<br />\nTotal price of the items in your shopping cart: \${$total}<br />\n"; $cart -> clearCart(); $cart -> showCart(); $total = $cart -> getTotal(); echo "<br />\nTotal price of the items in your shopping cart: \${$total}<br />\n"; ?> [/code] In this script, showCart works flawlessly, even after clearCart is called.  No warnings at all.  What makes this so frustrating is that both scripts (my test script and my live script) are on the same server, so they're both running the same version of PHP (5.1.6) and they're both using the same ShoppingCart file, as there's only one on the server. So, is this a case of PHP acting buggy?  Or is there anything else I can try? Thanks.
  16. I hate to bump this up, but I still haven't found the cause of the error, and I'd like to fix it before my site goes live.  Please help.
  17. [quote author=hitman6003 link=topic=118331.msg485052#msg485052 date=1166129945] It's a warning, so it's not affecting the execution.  Do you still get that warning if there are items in the shopping cart?  Most likely the $Cart variable is empty, so there is nothing for it to loop through, which causes a warning from foreach. [/quote] Sorry for the delay in response. I've tried the showCart method with an empty cart before, and it executed properly (showing an empty cart) without any warnings.  I think the problem is with me trying to save/restore cart info from the session data.
  18. Well, I'm getting another OOP syntax error.  It's: [quote]Warning: Invalid argument supplied for foreach() in /home/thinkin8/php_classes/ShoppingCart.php on line 27[/quote] The error appears when I try using the ShoppingCart's showCart method. Code using the class: [code] <?php include('../php_config/config.php'); session_start(); ob_start(); if(!isset($_SESSION['ip'])){   $ip = $_SERVER['REMOTE_ADDR'];   $myCart = new ShoppingCart(); } else{   if($_SERVER['REMOTE_ADDR'] != unserialize($_SESSION['ip'])){       $ip = $_SERVER['REMOTE_ADDR'];       $myCart = new ShoppingCart();   }   else{       $ip = unserialize($_SESSION['ip']);       $myCart = unserialize($_SESSION['myCart']);   } } include('../templates/header.inc'); echo "IP address: $ip<br />\n"; $myCart -> showCart(); echo <<<EOT <div>   <img src="images/store/storefront_01.jpg" alt="" /><img src="images/store/storefront_02.jpg" alt="" /> </div> <div>   <a href="viewcat.php?cat=acer_laptops"><img src="images/store/storefront_05.jpg" alt="" /></a>   <img src="images/store/storefront_06.jpg" alt="" />   <a href="viewcat.php?cat=lenovo_laptops"><img src="images/store/storefront_04.jpg" alt="" /></a> </div> <div>   <a href="viewcat.php?cat=averatec_laptops"><img src="images/store/storefront_09.jpg" alt="" /></a>   <a href="viewcat.php?cat=panasonic_laptops"  style="margin-left: 68px;"><img src="images/store/storefront_11.jpg" alt="" /></a> </div> <div>   <img src="images/store/storefrontbar.jpg" alt="" /> </div> <div>   <a href="viewcat.php?cat=acer_desktops"><img src="images/store/storefront_05.jpg" alt="" /></a>   <img src="images/store/desktop.jpg" style="margin-left: 25px; alt="" />   <a href="viewcat.php?cat=lenovo_desktops" style="margin-left: 30px;"><img src="images/store/storefront_04.jpg" alt="" /></a> </div> <div>   <a href="#"><img src="images/store/storefront_19.jpg" alt="" /></a>   <a href="viewcat.php?cat=dandh_desktops" style="margin-left: 53px;"><img src="images/store/storefront_21.jpg" alt="" /></a> </div> <div>   <img src="images/store/storefrontbar.jpg" alt="" /> </div> <div>   <a href="viewcat.php?cat=peripherals"><img src="images/store/storefront_24.jpg" alt="" /></a> </div> <div>   <img src="images/store/storefront_25.jpg" alt="" /> </div> EOT; include('../templates/footer.inc'); $_SESSION['ip'] = serialize($ip); $_SESSION['myCart'] = serialize($myCart); ob_end_flush(); ?> [/code] My ShoppingCart class: [code] <?php class ShoppingCart{   private $Cart;   public function addProduct($name, $price, $quantity){       $this -> Cart[] = new Product($name, $price, $quantity);   }   public function clearCart(){       $this -> Cart = array();   }   public function getTotal(){       $total = 0;       foreach($this->Cart as $value){         $total += $value -> getPrice();       }       return $total;   }   public function showCart(){       echo "<table><tr><th>Name:</th><th>Quantity:</th><th>Total Price:</th></tr>\n";       foreach($this->Cart as $value){         echo "<tr><td>{$value->getName()}</td><td>{$value->getQuan()}</td><td>\${$value->getPrice()}</td></tr>\n";       }       echo "</table>\n";   } } ?> [/code] Interestingly, I had originally had my session variables both url(de/en)coded and (un)serialized, which gave me the error: [quote]Fatal error: Call to a member function showCart() on a non-object in /home/thinkin8/public_html/storefront.php on line 28[/quote] I'm thinking my error is of the 'duh' variety, but I can't seem to find it.  Any ideas? Thanks :)
  19. ...but I'm not 100% sure how to implement it the way I want it to be implemented.  I'm writing scripts for my work's e-commerce side of things, and would like it if I could notify the user that their item has been added once they click "Add item to cart" (which will be a form submission button).  I'm just not sure how to do it with a sticky form. I know how to make a generic sticky form -- check if the form's been submitted, and react accordingly.  I'm just concerned about the post-submit behavior.  Would something like the following work: [code] <?php if(isset($_POST['submit'])){   echo "$_POST['quantity'] of $_POST['name'] has been added to your cart!"; . . . } ?> <form...> . . . [/code] If so, how would the end result be displayed?  Would the form not appear because it has been submitted, or would the order confirmation bit be displayed with the form?
  20. Sweet, that did the trick! Thanks for your help. :)
  21. It looks like my problem was some sort of double quote/single quote conflict.  Putting everything into one big echo statement, while ugly, fixed the problem. I have another syntax problem in a different file, though.  I'm trying to do a session test, checking to see if my sessions are saving the objects I've created properly.  I get the following error: [quote]Fatal error: Call to a member function showCart() on a non-object in /home/thinkin8/public_html/sessiontest2.php on line 17[/quote] My class code is: [code] <?php class ShoppingCart{   private $Cart;   public function addProduct($name, $price, $quantity){       $this -> Cart[] = new Product($name, $price, $quantity);   }   public function clearCart(){       $this -> Cart = array();   }   public function getTotal(){       $total = 0;       foreach($this->Cart as $value){         $total += $value -> getPrice();       }       return $total;   }   public function showCart(){       echo "<table><tr><th>Name:</th><th>Quantity:</th><th>Total Price:</th></tr>\n";       foreach($this -> Cart as $value){         echo "<tr><td>{$value->getName()}</td><td>{$value->getQuan()}</td><td>\${$value->getPrice()}</td></tr>\n";       }       echo "</table>\n";   } } ?> [/code] I have one file that populates the cart, and another that outputs it, in order to see if my session info is being passed properly from page to page.  The problem seems to be stemming from the testCart variable not realizing it's a ShoppingCart object.  My setup code is: [code] <?php #sessiontest.php include('../php_config/config.php'); session_start(); $testCart = new ShoppingCart(); $testCart -> addProduct('Tomatoes', 0.99, 4); $testCart -> addProduct('Playstation 3', 600.00, 1); $testCart -> addProduct('Nike Shoes', 75.00, 1); $_SESSION['testCart'] = urlencode(serialize($testCart)); ?> <html> <head></head> <body> <a href="sessiontest2.php">Click</a> </body></html> [/code] My code calling the showCart method is: [code] <?php #sessiontest2.php include('../php_config/config.php'); session_start(); if(!isset($_SESSION['testCart'])){   $testCart = new ShoppingCart(); } else{   $testCart = urldecode(unserialize($_SESSION['testCart'])); } $testCart -> showCart(); ?> [/code] Any ideas on how I can get it to think it's a ShoppingCart object?
  22. Removing the tab didn't help....
  23. Anyone mind helping me figure out what's causing the following syntax error? [quote]Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/thinkin8/public_html/viewitem.php on line 46[/quote] The code in question is: [code] <?php #viewitem.php script include('../php_config/config.php'); include('../dbconnect.php'); session_start(); ob_start(); if(!isset($_SESSION['cartInfo'])){   $myCart = new ShoppingCart(); } else{   $myCart = urldecode(unserialize($_SESSION['cartInfo'])); } if(isset($_GET['cat'])){   $tableName = $_GET['cat']; } else{   header("Location: www.thinkingmachinestore.com"); } if(isset($_GET['id'])){   $id = $_GET['id']; } else{   header("Location: www.thinkingmachinestore.com"); } $query = "SELECT * FROM $tableName WHERE {$tableName}_id = '$id'"; $result = mysql_query($query); if(mysql_num_rows == 1){   $row = mysql_fetch_assoc($result);   $description = nl2br($row['description']);   //start outputting the product info   echo <<<EOF   <div> <!-- left side -->   <img src=$row['pic_url'] alt='' /><br />   <p>$description</p>   </div>   <div> <!-- right side -->   <p>$row['name']</p>   <p>$row['price']</p>   </div>   EOF; } $_SESSION['cartInfo'] = urlencode(serialize($myCart)); ob_end_flush(); ?> [/code] If I've counted my lines correctly, it looks like it's breaking where I'm trying to output the image in the heredoc. Any ideas?
  24. My border: none is quite literally connected to the anchor element.  In other words: [code] a{    border: none; } [/code] But that didn't work. EDIT: but setting the images' border to 0 did.  Thanks, ToonMariner. :)
  25. I'm using images as hyperlinks.  Unfortunately, they have a border around them signifying that they're hyperlinks.  I've tried a {border: none;}, but it doesn't remove the border.  Any ideas?
×
×
  • 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.