jamesxg1 Posted November 26, 2009 Share Posted November 26, 2009 Hiya peeps, Ok here is the codes. order.php <?php if(!isset($_POST) OR empty($_POST)) { echo '<h1 align="center">There was an error with your order!</h1><h2 align="center"><a href="index.php">Start again</a></h2>'; } else { $tesco = new Tesco(); foreach($_POST as $key => $value) { $tesco->getPrice($value, $key); } echo '<h1><u>You ordered:</u></h1>'; echo '<h2>'.$cnt[$row['id']].' x '.$row['name'].'</h2>'; echo '<h1><u>Results</u></h1>'; echo '<h2>Cheapest at: ' . $store . '</h2>'; echo '<h2>Amount: £'.$total.'</h2>'; if($total > 50) { echo '<a href="" onclick="" class="buttonn">Deliver it!</a>'; } elseif ($total < 50) { echo '<a href="" onclick="" class="buttonn">Deliver it for £9.99!</a>'; } } ?> tesco.php <?php class Tesco { private $quantity; private $productid; function getPrice($quantity, $productid, $storeid = '1') { $this->quantity = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($quantity))))); $this->productid = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($productid))))); $this->storeid = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($storeid))))); $this->query = "SELECT * FROM `products` WHERE id = '$this->productid' AND store = '$this->storeid'"; $this->result = mysql_query($this->query) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR); while($this->price = mysql_fetch_object($this->result)): if($this->quantity > 1): $this->total[] = $this->price->price*$this->quantity; elseif ($this->quantity = 1): $this->total[] = $this->price->price; endif; endwhile; print_r($this->total); } } ?> As you can see in the tesco.php file i have a print_r() this is what is being returned.. Array ( [0] => ) Array ( [0] => [1] => ) (i have inputed two product id's). Many thanks James. Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Author Share Posted November 26, 2009 Code update. tesco.php <?php class Tesco { private $quantity; private $productid; function getPrice($quantity, $productid, $storeid = '1') { $this->quantity = mysql_real_escape_string(trim(addslashes(strip_tags($quantity)))); $this->productid = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($productid))))); $this->storeid = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($storeid))))); $this->query = "SELECT * FROM `products` WHERE id = '$this->productid' AND store = '$this->storeid'"; $this->result = mysql_query($this->query) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR); while($this->price = mysql_fetch_assoc($this->result)): if($this->quantity > 1): $this->total[] = $this->price['price']*$this->quantity; elseif ($this->quantity = 1): $this->total[] = $this->price['price']; endif; endwhile; foreach($this->total as $this->value): $this->totalprice += $this->value; endforeach; echo $this->totalprice . '<br>'; } } ?> Not its working ok, but i have two arrays being printed from it. Array ( [0] => 1 ) // Why do i get this? Array ( [0] => 1 [1] => 1 ) // this is correct. Many thanks James. Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Author Share Posted November 26, 2009 Im guessing noone knows how to fix this ? Many thanks James. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted November 26, 2009 Share Posted November 26, 2009 It's because you are using is_numeric on your data, which returns a true or false http://uk2.php.net/is_numeric You need to change the sanitization quite a bit. You've also got addslashes inside of mysql_real_escape_string which is just going to mess things up massively if you want to do it the simple way, use is_int() $this->quantity = is_int($quantity) ? (int) $quantity : 1; $this->productid = is_int($productid) ? (int) $productid : 1; $this->storeid = is_int($storeid) ? (int) $storeid : 1; The one at the end is to set the id to 1 by default if it's not an integer. Make sure you change those to what you want Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Author Share Posted November 26, 2009 Hiya mate, Cheers for the reply. Still no luck thoe. Its a nightmare. Many thanks James. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 26, 2009 Share Posted November 26, 2009 just one question do you have multiple products with the same product id in products ? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 26, 2009 Share Posted November 26, 2009 start by echo'ing out your query: echo $this->query; and see if it's what you want it to be. report back. Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Author Share Posted November 26, 2009 just one question do you have multiple products with the same product id in products ? CREATE TABLE IF NOT EXISTS `products` ( `id` int(6) NOT NULL, `image` varchar(32) NOT NULL, `name` varchar(64) NOT NULL, `description` text NOT NULL, `price` int(1) NOT NULL DEFAULT '1', `store` int(11) NOT NULL, `storename` varchar(32) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- -- Dumping data for table `products` -- INSERT INTO `products` (`id`, `image`, `name`, `description`, `price`, `store`, `storename`) VALUES (1, 'iPod.png', 'iPod', 'The original and popular iPod.', 1, 1, ''), (2, 'iMac.png', 'iMac', 'The iMac computer.', 1, 1, ''), (3, 'iPhone.png', 'iPhone', 'This is the new iPhone.', 1, 1, ''), (4, 'iPod-Shuffle.png', 'iPod Shuffle', 'The new iPod shuffle.', 1, 1, ''), (5, 'iPod-Nano.png', 'iPod Nano', 'The new iPod Nano.', 1, 1, ''), (6, 'Apple-TV.png', 'Apple TV', 'The new Apple TV. Buy it now!', 1, 1, ''); (1, 'iPod.png', 'iPod', 'The original and popular iPod.', 1, 2, ''), (2, 'iMac.png', 'iMac', 'The iMac computer.', 1, 2, ''), (3, 'iPhone.png', 'iPhone', 'This is the new iPhone.', 1, 2, ''), (4, 'iPod-Shuffle.png', 'iPod Shuffle', 'The new iPod shuffle.', 1, 2, ''), (5, 'iPod-Nano.png', 'iPod Nano', 'The new iPod Nano.', 1, 2, ''), (6, 'Apple-TV.png', 'Apple TV', 'The new Apple TV. Buy it now!', 1, 2, ''); Thats the sql dump for the products table. Many thanks James. Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Author Share Posted November 26, 2009 start by echo'ing out your query: echo $this->query; and see if it's what you want it to be. report back. Yup all good heres what it displays. SELECT * FROM `products` WHERE id = '1' AND store = '1' SELECT * FROM `products` WHERE id = '2' AND store = '1' Many thanks James. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 26, 2009 Share Posted November 26, 2009 should your code be something like this <?php class Tesco { private $quantity; private $productid; private $storeid; function getPrice($quantity, $productid, $storeid = '1') { $this->quantity = mysql_real_escape_string(trim(addslashes(strip_tags($quantity)))); $this->productid = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($productid))))); $this->storeid = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($storeid))))); $this->query = "SELECT `price`, `price` * $quantity as total FROM `products` WHERE id = 'productid' AND store = '$storeid'"; $this->result = mysql_query($this->query) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR); $this->price = mysql_fetch_assoc($this->result) print_r($this->price): } } ?> Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Author Share Posted November 26, 2009 should your code be something like this <?php class Tesco { private $quantity; private $productid; private $storeid; function getPrice($quantity, $productid, $storeid = '1') { $this->quantity = mysql_real_escape_string(trim(addslashes(strip_tags($quantity)))); $this->productid = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($productid))))); $this->storeid = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($storeid))))); $this->query = "SELECT `price`, `price` * $quantity as total FROM `products` WHERE id = 'productid' AND store = '$storeid'"; $this->result = mysql_query($this->query) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR); $this->price = mysql_fetch_assoc($this->result) print_r($this->price): } } ?> Hiya mate, Cheers for the code. No luck thoe unfortunatly. Many thanks James. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 26, 2009 Share Posted November 26, 2009 that was because I made is small mistake this $this->query = "SELECT `price`, `price` * $quantity as total FROM `products` WHERE id = 'productid' AND store = '$storeid'"; should be $this->query = "SELECT `price`, `price` * $quantity as total FROM `products` WHERE id = '$productid' AND store = '$storeid'"; Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Author Share Posted November 26, 2009 that was because I made is small mistake this $this->query = "SELECT `price`, `price` * $quantity as total FROM `products` WHERE id = 'productid' AND store = '$storeid'"; should be $this->query = "SELECT `price`, `price` * $quantity as total FROM `products` WHERE id = '$productid' AND store = '$storeid'"; Lol. I know i corrected it before using it and still nothing :S. Cheers bud. James. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 26, 2009 Share Posted November 26, 2009 you just want the price and the total price right ? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 26, 2009 Share Posted November 26, 2009 Okay I got something wrong try the following code it should work <?php class Tesco { private $quantity; private $productid; private $storeid; public function getPrice($quantity, $productid, $storeid = '1') { $this->quantity = mysql_real_escape_string(trim(addslashes(strip_tags($quantity)))); $this->productid = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($productid))))); $this->storeid = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($storeid))))); $query = sprintf("SELECT `price`, `price` * %d as total FROM `products` WHERE id = '%s' AND store = '%s'", $this->quantity, $this->productid, $this->storeid); $result = mysql_query($query) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR); $price = mysql_fetch_assoc($result) print_r($price); } } ?> Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Author Share Posted November 26, 2009 Okay I got something wrong try the following code it should work <?php class Tesco { private $quantity; private $productid; private $storeid; public function getPrice($quantity, $productid, $storeid = '1') { $this->quantity = mysql_real_escape_string(trim(addslashes(strip_tags($quantity)))); $this->productid = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($productid))))); $this->storeid = mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($storeid))))); $query = sprintf("SELECT `price`, `price` * %d as total FROM `products` WHERE id = '%s' AND store = '%s'", $this->quantity, $this->productid, $this->storeid); $result = mysql_query($query) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR); $price = mysql_fetch_assoc($result) print_r($price); } } ?> Thats a nice peice of code there mate - cheers. Ermmm still not working thoe this is what i get. Array ( [price] => 100 [total] => 100 ) // should be Array ( [price] => 1 [total] => 1 ) Array ( [price] => 100 [total] => 100 ) // this is correct So the original code i was using from the start does work i just have two array when only one is needed. Many thanks James. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 26, 2009 Share Posted November 26, 2009 are you calling this function within a loop, causing it to display twice, perhaps? Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Author Share Posted November 26, 2009 Yea. A foreach() but i dont have no other way of doing it :S Many thanks James. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 26, 2009 Share Posted November 26, 2009 can you post that chunk of code, please. Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Author Share Posted November 26, 2009 <?php if(!isset($_POST) OR empty($_POST)) { echo '<h1 align="center">There was an error with your order!</h1><h2 align="center"><a href="index.php">Start again</a></h2>'; } else { $cnt = array(); $products = array(); $tesco = new Tesco(); foreach($_POST as $id => $quant) { $tesco->getPrice($quant, $id); $cnt[$id] = $quant; $products[] = $id; } $count = count($products); $query = "SELECT * FROM `products` WHERE id IN(".join($products,',').") LIMIT $count"; $result = mysql_query($query) or trigger_error('<font color="red" size="6"><b>Site Error:</b><br />Could not query the selected database.<br /></font>' . mysql_error(), E_USER_ERROR); echo '<h1><u>You ordered:</u></h1>'; while($row = mysql_fetch_assoc($result)) { echo '<h2>'.$cnt[$row['id']].' x '.$row['name'].'</h2>'; } echo '<h1><u>Results</u></h1>'; echo '<h2>Cheapest at: ' . $store . '</h2>'; echo '<h2>Amount: £'.$total.'</h2>'; if($total > 50) { echo '<a href="" onclick="" class="buttonn">Deliver it!</a>'; } elseif ($total < 50) { echo '<a href="" onclick="" class="buttonn">Deliver it for £9.99!</a>'; } } ?> Cheers James. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted November 26, 2009 Share Posted November 26, 2009 You still have is_numeric on the storeid and product id. This won't work with is_numeric as I originally said Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 26, 2009 Share Posted November 26, 2009 well yes, that'd be why it's displaying more than once. Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Author Share Posted November 26, 2009 Ok here are both of my full codes. order.php <?php define('INCLUDE_CHECK',1); require 'files/system/connect.php'; include 'files/stores/tesco.php'; if(!isset($_POST)) { if(isset($_SERVER['HTTP_REFERER'])) { header('Location : '.$_SERVER['HTTP_REFERER']); exit; } } ?> <!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>iDealz | Price Checker</title> <link rel="stylesheet" type="text/css" href="files/css/cart.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script> <script type="text/javascript" src="files/js/jquery.simpletip-1.3.1.pack.js.txt"></script> <script type="text/javascript" src="files/js/cart.js"></script> </head> <body> <div id="main-container"> <div class="container"> <span class="top-label"> <span class="label-txt">Your order</span> </span> <div class="content-area"> <div class="content"> <?php if(!isset($_POST) OR empty($_POST)) { echo '<h1 align="center">There was an error with your order!</h1><h2 align="center"><a href="index.php">Start again</a></h2>'; } else { $cnt = array(); $products = array(); $tesco = new Tesco(); foreach($_POST as $id => $quant) { $tesco->getPrice($quant, $id); $cnt[$id] = $quant; $products[] = $id; } $count = count($products); $query = "SELECT * FROM `products` WHERE id IN(".join($products,',').") LIMIT $count"; $result = mysql_query($query) or trigger_error('<font color="red" size="6"><b>Site Error:</b><br />Could not query the selected database.<br /></font>' . mysql_error(), E_USER_ERROR); echo '<h1><u>You ordered:</u></h1>'; while($row = mysql_fetch_assoc($result)) { echo '<h2>'.$cnt[$row['id']].' x '.$row['name'].'</h2>'; } echo '<h1><u>Results</u></h1>'; echo '<h2>Cheapest at: ' . $store . '</h2>'; echo '<h2>Amount: £'.$total.'</h2>'; if($total > 50) { echo '<a href="" onclick="" class="buttonn">Deliver it!</a>'; } elseif ($total < 50) { echo '<a href="" onclick="" class="buttonn">Deliver it for £9.99!</a>'; } } ?> <div class="clear"></div> </div> </div> <div class="bottom-container-border"> </div> </div> </div> <div class="tutorial-info"> <u>©</u> Copyright of <a href="">iDealz</a> ® 2009. <u>©</u> </div> </div> </body> </html> tesco.php <?php class Tesco { private $quantity; private $productid; function getPrice($quantity, $productid, $storeid = '1') { $this->quantity = mysql_real_escape_string($quantity); $this->productid = mysql_real_escape_string($productid); $this->storeid = mysql_real_escape_string(is_numeric($storeid)); $this->query = "SELECT * FROM `products` WHERE id = '$this->productid' AND store = '$this->storeid'"; $this->result = mysql_query($this->query) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR); while($this->price = mysql_fetch_assoc($this->result)): if($this->quantity > 1): $this->total[] = $this->price['price']*$this->quantity; elseif ($this->quantity <= 1): $this->total[] = $this->price['price']; endif; endwhile; foreach($this->total as $this->value): $this->totalprice += $this->value; endforeach; print_r($this->total); } } ?> Many thanks James. Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Author Share Posted November 26, 2009 Bump. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 27, 2009 Share Posted November 27, 2009 You need to setup your database properly, I would advise against using object orientated PHP for the sake of using it, you are way overusing $this, only use it when you need to use the value stored in that class property in other methods within the class and declare them (public, protected, private) at the top of the class. Also, your getPrice() method seems pretty useless to me, it doesn't return anything and you are escaping values incorrectly (integers with mysql_real_escape_string()). Also I wouldn't pass the full contents of the $_POST array to the getPrice method, why not create another array within the post array and iterate through its values passing them to getPrice(). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.