Stubworth Posted June 7, 2011 Share Posted June 7, 2011 Hey all I'm working on an auction website. Users can enter a 'Bid From' and 'Bid To' amount to enter bids in a range. So for instance, you bid from £0.01 to £0.10 you place bids for £0.01, £0.02, £0.03 etc. up to and including £0.10. Now comes the part where I am confused, I have a code snippet which finds the values between the two numbers, enters them into the database perfectly. However, on certain random 'ranges' an additional bid is added onto the end. That is, a bid from £0.01 to £0.10 places bids up to £0.11. This only happens sometimes and there is no discernable pattern in the values affected. Users are charged credits for each bid placed in the auction and the number of credits taken when an extra bid is placed are as they should be, i.e. bidding between £0.01 and £0.10 should cost 10 credits, when values £0.01 to £0.11 are entered in the DB, 10 credits are still charged. This problem is only with the values created and entered into the 'bids' table in the database. The code I am using is as follows: if($_POST['bid_type']=='range') { $flag=0; for($j=$_POST['bid_from'];$j<($_POST['bid_to']+0.01);$j+=0.01) { $insert = "INSERT INTO bids(auction, bidder, bid, bidwhen) VALUES($id, $user_id_, $j, '$NOW');"; mysql_query($insert); } } The most important section is the 'for' statement. The $flag statement is to prevent F5 (refresh) reprocessing the PHP on the page. If anyone has any suggestions or ideas as to why this will on an apparently random occasion and an extra value, please let me know! Thanks in advance for any replies! I'd also be happy to look at alternative methods of achieving this same result if you can think of any! :thumbsup: Quote Link to comment https://forums.phpfreaks.com/topic/238692-finding-all-values-between-two-numbers-in-001-increments/ Share on other sites More sharing options...
wildteen88 Posted June 7, 2011 Share Posted June 7, 2011 You're most probably getting 0.11 as the bid rather than 0.10 because you're adding 0.01 in the highlighted code below for($j=$_POST['bid_from'];$j<($_POST['bid_to']+0.01);$j+=0.01) If the bid is 0.01 to 0.10 that for loop will always return 0.11. If you take out the highlighted code above it'll return 0.10 as the bid. Quote Link to comment https://forums.phpfreaks.com/topic/238692-finding-all-values-between-two-numbers-in-001-increments/#findComment-1226579 Share on other sites More sharing options...
Stubworth Posted June 7, 2011 Author Share Posted June 7, 2011 Hi wildteen, thanks for the reply. I changed the code as per your reccomendation with odd results. The bids for £0.01 to £0.10 no longer placed a bid for £0.11 as well and were therefore functioning as intended. However I then entered ranges of £1.30 to £1.40 and £1.40 to £1.50 respectively, both times these entries entered only up to £1.39 and £1.49 respectively. Any further ideas? Quote Link to comment https://forums.phpfreaks.com/topic/238692-finding-all-values-between-two-numbers-in-001-increments/#findComment-1226585 Share on other sites More sharing options...
xyph Posted June 7, 2011 Share Posted June 7, 2011 Try using $j<=$_POST['bit_to'] As of right now, I don't see any verification to make sure $_POST['bid_to'] is xxx.xx, and not xxx.xxx. Also, IMO you're doing this in a bad way. You're better off storing the low and high bids in the table, and calculating ranges after. Here's how I'd do it. Personally I don't understand the point of a low bid unless there are no bis on an item, but I'm used to the seller giving a starting price. Database Dump: -- phpMyAdmin SQL Dump -- version 3.2.0.1 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Jun 07, 2011 at 11:14 AM -- Server version: 5.1.36 -- PHP Version: 5.3.0 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; -- -- Database: `db` -- -- -------------------------------------------------------- -- -- Table structure for table `auction` -- CREATE TABLE IF NOT EXISTS `auction` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user` int(11) NOT NULL, `name` varchar(100) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; -- -- Dumping data for table `auction` -- INSERT INTO `auction` (`id`, `user`, `name`) VALUES (1, 1, 'test'), (2, 1, 'test2'); -- -------------------------------------------------------- -- -- Table structure for table `bids` -- CREATE TABLE IF NOT EXISTS `bids` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user` int(11) NOT NULL, `auction` int(11) NOT NULL, `low` decimal(6,2) NOT NULL, `high` decimal(6,2) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; -- -- Dumping data for table `bids` -- INSERT INTO `bids` (`id`, `user`, `auction`, `low`, `high`) VALUES (1, 1, 1, '50.00', '65.55'), (2, 2, 1, '45.00', '75.25'), (3, 4, 1, '50.50', '82.50'), (4, 3, 1, '35.00', '60.75'); Code coming soon Quote Link to comment https://forums.phpfreaks.com/topic/238692-finding-all-values-between-two-numbers-in-001-increments/#findComment-1226588 Share on other sites More sharing options...
Stubworth Posted June 7, 2011 Author Share Posted June 7, 2011 I believe it is now functioning correctly after some testing with various inputs. The code I used to achieve this can be found below: As per Fou-Lu's suggestions (user on CodingForums.com), I multiplied it all by 100 then divided by 100 when entering the information into the DB table which is a decimal(10,2) field. I also followed xyph's advice regarding limiting the 'from' and 'to' strings to 2 decimal places before multiplying them up. if($_POST['bid_type']=='range') { $flag=0; for($j=(number_format($_POST['bid_from'], 2, '.', ' ')*100);($j<(number_format($_POST['bid_to'], 2, '.', ' ')*100)+1);$j+=1.00) { $insert = "INSERT INTO bids(auction, bidder, bid, bidwhen) VALUES($id, $user_id_, $j/100, '$NOW');"; mysql_query($insert); } } Quote Link to comment https://forums.phpfreaks.com/topic/238692-finding-all-values-between-two-numbers-in-001-increments/#findComment-1226649 Share on other sites More sharing options...
xyph Posted June 8, 2011 Share Posted June 8, 2011 The issue here was bad float rounding... Sorry I didn't get a chance to post my code. Got busy Quote Link to comment https://forums.phpfreaks.com/topic/238692-finding-all-values-between-two-numbers-in-001-increments/#findComment-1226842 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.