Array not returning default value set over 1,000,000 when using jquery fancy comma script

121 Views Asked by At

I have the jquery fancy comma script added within my site for use on my form.

1 of the form input fields is a price option where the user enters a specific price related to their needs, Upon doing so they do not need to enter the commas between the price so for example if they type (900000) it will automatically display as (900,000) and this starts from (1,000).

I have a PHP array script running that will return a default value set for within a range that they can input... This is below.

 $data = array(
    array(
        'min' => 0,000,
        'max' => 100,000,
        'value' => 125
    ),
    array(
        'min' => 101,000,
        'max' => 200,000,
        'value' => 195
    ),
    array(
        'min' => 201,000,
        'max' => 300,000,
        'value' => 225
    ),
    array(
        'min' => 301,000,
        'max' => 400,000,
        'value' => 275
    ),
    array(
        'min' => 401,000,
        'max' => 500,000,
        'value' => 325
    ),
    array(
        'min' => 501,000,
        'max' => 600,000,
        'value' => 375
    ),
    array(
        'min' => 601,000,
        'max' => 700,000,
        'value' => 425
    ),
    array(
        'min' => 701,000,
        'max' => 800,000,
        'value' => 475
    ),
    array(
        'min' => 801,000,
        'max' => 900,000,
        'value' => 525
    ),
    array(
        'min' => 901,000,
        'max' => 999,999,
        'value' => 625
    ),
    array(
        'min' => 1,000,000,
        'max' => 3,000,000,
        'value' => 725
    ),
    array(
        'min' => 3,000,001,
        'max' => 6,000,000,
        'value' => 800
    ),
    array(
        'min' => 6,000,001,
        'max' => 600,000,000,
        'value' => 925
    )
);

function getAdjustedPrice($price, &$table) {
    $priceData = current(array_filter($table, function($value) use(&$price) {
        return $value['min'] <= $price && $value['max'] >= $price;
    }));
    return $priceData['value'];
}

$input = intval($_SESSION["userinput"]);
printf("", 
       $input, 
       getAdjustedPrice($input, $data));

The problem i have is as soon as the price exceeds 999,999 and you enter 1,000,000 and above so there are two commas in the input, The array fails to display the default price set for that range.

Any suggestions or advice would be much appreciated. Maybe someone has a better way of setting the default value for each range, Maybe a jquery or javascript based option?

Im still learning the very basics so please do forgive my ignorance or idiotic approach to things!

---- UPDATE (ADDING THE CODE USED TO APPLY COMMAS)

--------------------------  First Section


    function addCommas(val) {
    val = val.replace(/,/g, "");
    var regEx = /(\d+)(\d{3})/;
    while (regEx.test(val)) {
        val = val.replace(regEx, '$1' + ',' + '$2');
    }
    return val;
    };;


    --------------------------- Second section


        // Fancy commas
        $(this).keyup(function() {
            var newValue = addCommas($(this).val());
            $(this).val(addCommas(newValue));
            if (PropValueS.val().indexOf('\u00A3') == -1) {
                PropValueS.val('\u00A3' + PropValueS.val());
            }
        });

    });
1

There are 1 best solutions below

3
On

I'm not quite sure what you're trying to do with the commas since googling jquery fancy comma script didn't yield any results.

What I think is happening is that your data isn't being interpreted the way you think it is. I am not well versed in PHP but

'min' => 3,000,001

Most likely adds three entries to the array, min => 3, 000, and 001.

Also, as a suggestion, I would handle the logic of your data in the following way:

var inputValue = 220000; // Change to the value extracted from the input form
var value = 0;

if(inputValue >= 0 && inputValue < 100000) {
    value = 125;
}
else if(inputValue >= 100000 && inputValue < 200000) {
    value = 195;
}
else if(inputValue >= 200000 && inputValue < 300000) {
    value = 225;
}
...