Jump to content

Finding all values between two numbers in 0.01 increments.


Stubworth

Recommended Posts

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:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
	}
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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