1

In my database I have

| minprice | mediumint(9) | YES |  | NULL |    |

I am trying to create a dropdown that will use the interger provided from the database to populate the value in the select statement.

I have tried single, double quotes, no quotes around the numbers and cannot seem to figure out how to use it.

ie.

echo $row['maxprice']
// output 350000

<option<?php if ($row['maxprice'] <= "350000"): ?> selected="selected"<?php endif; ?>>350000</option>

<option<?php if ($row['maxprice'] <= '350000'): ?> selected="selected"<?php endif; ?>>350000</option>

<option<?php if ($row['maxprice'] <= 350000): ?> selected="selected"<?php endif; ?>>350000</option>

The expected value in the select would be 350000

If the output was 340000 I would still expect 350000

If the output was 360000 I would expect 400000

<label for="">Max Price</label>
<select id="maxprice" name="maxprice">
<option<?php if ($row['maxprice'] < "100000"): ?> selected="selected"<?php endif; ?>>-100000</option>
<option<?php if ($row['maxprice'] <= "100000"): ?> selected="selected"<?php endif; ?>>100000</option>
<option<?php if ($row['maxprice'] <= "150000"): ?> selected="selected"<?php endif; ?>>150000</option>
<option<?php if ($row['maxprice'] <= "200000"): ?> selected="selected"<?php endif; ?>>200000</option>
<option<?php if ($row['maxprice'] <= "250000"): ?> selected="selected"<?php endif; ?>>250000</option>
<option<?php if ($row['maxprice'] <= "300000"): ?> selected="selected"<?php endif; ?>>300000</option>
<option<?php if ($row['maxprice'] <= "350000"): ?> selected="selected"<?php endif; ?>>350000</option>
<option<?php if ($row['maxprice'] <= "400000"): ?> selected="selected"<?php endif; ?>>400000</option>
<option<?php if ($row['maxprice'] <= "450000"): ?> selected="selected"<?php endif; ?>>450000</option>
<option<?php if ($row['maxprice'] <= "500000"): ?> selected="selected"<?php endif; ?>>500000</option>
<option<?php if ($row['maxprice'] <= "550000"): ?> selected="selected"<?php endif; ?>>550000</option>
<option<?php if ($row['maxprice'] <= "600000"): ?> selected="selected"<?php endif; ?>>600000</option>
<option<?php if ($row['maxprice'] <= "650000"): ?> selected="selected"<?php endif; ?>>650000</option>
<option<?php if ($row['maxprice'] <= "700000"): ?> selected="selected"<?php endif; ?>>700000</option>
<option<?php if ($row['maxprice'] <= "750000"): ?> selected="selected"<?php endif; ?>>750000</option>
<option<?php if ($row['maxprice'] <= "800000"): ?> selected="selected"<?php endif; ?>>800000</option>
<option<?php if ($row['maxprice'] <= "850000"): ?> selected="selected"<?php endif; ?>>850000</option>
<option<?php if ($row['maxprice'] <= "900000"): ?> selected="selected"<?php endif; ?>>900000</option>
<option<?php if ($row['maxprice'] <= "950000"): ?> selected="selected"<?php endif; ?>>950000</option>
<option<?php if ($row['maxprice'] >= "1000000"): ?> selected="selected"<?php endif; ?>>1000000+</option>
</select>
7
  • 1
    You do realise that code will set More Than One line in the dropdown to SELECTED Commented Jan 12, 2020 at 16:19
  • 1
    Try putting a space between the tag name and the php tag <option <?php Commented Jan 12, 2020 at 16:28
  • You may also want to add a value="" attribute so you know what was selected later when the PHP has to process the forms data Commented Jan 12, 2020 at 16:29
  • @RiggsFolly I tired the space did not work, I have another select that is pulling a varchar value and that one works fine, it has something to to with not being able to read the interger from the mysql, I think Commented Jan 12, 2020 at 16:31
  • 1
    What is the actual problem here? Commented Jan 12, 2020 at 16:33

3 Answers 3

2

All values received from a database will be a string data type. However the PHP lexer, will automatically evaluate numeric string comparisons as an integer or float value.

When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float. [sic]

This allows for "10" > 2 and "10" > "2" to be evaluated correctly and without the need of typecasting or using intval. [Example]

The main reason for your issue is caused by not using a range to limit the min and max prices to select. As a result you will have multiple values set as selected and the browser will only show the first as the selected option. To circumvent the multiple selected options issue, you would need to use a conditional range like $price >= $min && $price <= $max.

To reduce the repetitive html, you can use an array as your limit definitions, and iterate over the array to draw out the html.

Example: https://3v4l.org/c6kSq

<?php $limits = [
   '-100000' => [
       'min' => null,
       'max' => 100000
   ],
   '100000' => [
       'min' => 100001,
       'max' => 150000
   ],
    //...
    '350000' => [
       'min' => 300001,
       'max' => 350000
   ],
   '400000' => [
       'min' => 350001,
       'max' => 400000,
   ],
    //...
   '1000000' => [
      'min' => 900001,
      'max' => 1000000,
   ],
   '1000000+' => [
      'min' => 1000001,
      'max' => null,
   ],
]; ?>

<!-- ....... -->

<label for="maxprice">Max Price</label>
<select id="maxprice" name="maxprice">
<?php 
$price = $row['maxprice'];
foreach ($limits as $amount => $limit) { 
    $min = $limit['min'];
    $max = $limit['max'];
    ?>
    <option <?php echo((null === $min || $price >= $min) && (null === $max || $price <= $max) ? 'selected' : ''); ?>><?php echo $amount; ?></option>
<?php } ?>
</select>

Alternatively you can also achieve your desired results by using range(100000, 1000000, 50000); to generate your values and use conditions to detect the first < and last > entries, or $price >= $amount - 49999 && $price <= $amount for all other values.

Example: https://3v4l.org/U9B9m

$amounts = range(100000, 1000000, 50000);
end($amounts);
$last = key($amounts);
//alternatively use $last = count($amounts) - 1;

<label for="maxprice">Max Price</label>
<select id="maxprice" name="maxprice">
<?php foreach ($amounts as $i => $amount) {
    if (0 === $i) { ?>
        <option<?php echo($row['maxprice'] < $amount ? ' selected' : ''); ?>><?php echo '-' . $amount; ?></option>
    <?php } ?>
        <option<?php echo($row['maxprice'] >= $amount - 49999 && $row['maxprice'] <= $amount ? ' selected' : ''); ?>><?php echo $amount; ?></option>
    <?php if($last === $i) { ?>
        <option<?php echo($row['maxprice'] > $amount ? ' selected' : ''); ?>><?php echo $amount . '+'; ?></option>
    <?php }  ?>
<?php } ?>
</select>

Results

<label for="maxprice">Max Price (20)</label>
<select id="maxprice" name="maxprice">
        <option selected>-100000</option>
           <option>100000</option>
                <option>150000</option>
                <option>200000</option>
                <option>250000</option>
                <option>300000</option>
                <option>350000</option>
                <option>400000</option>
                <option>450000</option>
                <option>500000</option>
                <option>550000</option>
                <option>600000</option>
                <option>650000</option>
                <option>700000</option>
                <option>750000</option>
                <option>800000</option>
                <option>850000</option>
                <option>900000</option>
                <option>950000</option>
                <option>1000000</option>
            <option>1000000+</option>
</select>

<label for="maxprice">Max Price (340000)</label>
<select id="maxprice" name="maxprice">
        <option>-100000</option>
           <option>100000</option>
                <option>150000</option>
                <option>200000</option>
                <option>250000</option>
                <option>300000</option>
                <option selected>350000</option>
                <option>400000</option>
                <option>450000</option>
                <option>500000</option>
                <option>550000</option>
                <option>600000</option>
                <option>650000</option>
                <option>700000</option>
                <option>750000</option>
                <option>800000</option>
                <option>850000</option>
                <option>900000</option>
                <option>950000</option>
                <option>1000000</option>
            <option>1000000+</option>
</select>

<label for="maxprice">Max Price (360000)</label>
<select id="maxprice" name="maxprice">
        <option>-100000</option>
           <option>100000</option>
                <option>150000</option>
                <option>200000</option>
                <option>250000</option>
                <option>300000</option>
                <option>350000</option>
                <option selected>400000</option>
                <option>450000</option>
                <option>500000</option>
                <option>550000</option>
                <option>600000</option>
                <option>650000</option>
                <option>700000</option>
                <option>750000</option>
                <option>800000</option>
                <option>850000</option>
                <option>900000</option>
                <option>950000</option>
                <option>1000000</option>
            <option>1000000+</option>
</select>

<label for="maxprice">Max Price (1000000)</label>
<select id="maxprice" name="maxprice">
        <option>-100000</option>
           <option>100000</option>
                <option>150000</option>
                <option>200000</option>
                <option>250000</option>
                <option>300000</option>
                <option>350000</option>
                <option>400000</option>
                <option>450000</option>
                <option>500000</option>
                <option>550000</option>
                <option>600000</option>
                <option>650000</option>
                <option>700000</option>
                <option>750000</option>
                <option>800000</option>
                <option>850000</option>
                <option>900000</option>
                <option>950000</option>
                <option selected>1000000</option>
            <option>1000000+</option>
</select>

<label for="maxprice">Max Price (1500000)</label>
<select id="maxprice" name="maxprice">
        <option>-100000</option>
           <option>100000</option>
                <option>150000</option>
                <option>200000</option>
                <option>250000</option>
                <option>300000</option>
                <option>350000</option>
                <option>400000</option>
                <option>450000</option>
                <option>500000</option>
                <option>550000</option>
                <option>600000</option>
                <option>650000</option>
                <option>700000</option>
                <option>750000</option>
                <option>800000</option>
                <option>850000</option>
                <option>900000</option>
                <option>950000</option>
                <option>1000000</option>
            <option selected>1000000+</option>
</select>
Sign up to request clarification or add additional context in comments.

Comments

1
intval($row['maxprice']) <= 350000

1 Comment

intval() is not needed, the PHP lexer will automatically evaluate numeric strings as their integer or float equivalents except when using strict comparison === (Example 3v4l.org/KJO31). See string.conversion
-2

To achieve this , i think You should use The if...elseif...else Statement

see resource : https://www.w3schools.com/php/php_if_else.asp

or you can modify your if statement conditions like this

I know that is coding like that is bad practicing , but that is what he asking for

<?php $maxprice = 350000; ?>
<label for="">Max Price</label>
<select id="maxprice" name="maxprice">
<option <?php if ($maxprice < 100000) : ?> selected="selected" <?php endif; ?>>-100000</option>
<option <?php if ($maxprice == 100000) : ?> selected="selected" <?php endif; ?>>100000</option>
<option <?php if ($maxprice <= 150000 && $maxprice > 100000) : ?> selected="selected" <?php endif; ?>>150000</option>
<option <?php if ($maxprice <= 200000 && $maxprice > 150000) : ?> selected="selected" <?php endif; ?>>200000</option>
<option <?php if ($maxprice <= 250000 && $maxprice > 200000) : ?> selected="selected" <?php endif; ?>>250000</option>
<option <?php if ($maxprice <= 300000 && $maxprice > 250000) : ?> selected="selected" <?php endif; ?>>300000</option>
<option <?php if ($maxprice <= 350000 && $maxprice > 300000) : ?> selected="selected" <?php endif; ?>>350000</option>
<option <?php if ($maxprice <= 400000 && $maxprice > 350000) : ?> selected="selected" <?php endif; ?>>400000</option>
<option <?php if ($maxprice <= 450000 && $maxprice > 400000) : ?> selected="selected" <?php endif; ?>>450000</option>
<option <?php if ($maxprice <= 500000 && $maxprice > 450000) : ?> selected="selected" <?php endif; ?>>500000</option>
<option <?php if ($maxprice <= 550000 && $maxprice > 500000) : ?> selected="selected" <?php endif; ?>>550000</option>
<option <?php if ($maxprice <= 600000 && $maxprice > 550000) : ?> selected="selected" <?php endif; ?>>600000</option>
<option <?php if ($maxprice <= 650000 && $maxprice > 600000) : ?> selected="selected" <?php endif; ?>>650000</option>
<option <?php if ($maxprice <= 700000 && $maxprice > 650000) : ?> selected="selected" <?php endif; ?>>700000</option>
<option <?php if ($maxprice <= 750000 && $maxprice > 700000) : ?> selected="selected" <?php endif; ?>>750000</option>
<option <?php if ($maxprice <= 800000 && $maxprice > 750000) : ?> selected="selected" <?php endif; ?>>800000</option>
<option <?php if ($maxprice <= 850000 && $maxprice > 800000) : ?> selected="selected" <?php endif; ?>>850000</option>
<option <?php if ($maxprice <= 900000 && $maxprice > 850000) : ?> selected="selected" <?php endif; ?>>900000</option>
<option <?php if ($maxprice <= 950000 && $maxprice > 900000) : ?> selected="selected" <?php endif; ?>>950000</option>
<option <?php if ($maxprice <= 1000000 && $maxprice > 950000) : ?> selected="selected" <?php endif; ?>>1000000</option>
<option <?php if ($maxprice > 1000000) : ?> selected="selected" <?php endif; ?>>1000000+</option>
</select>

2 Comments

Oh my. No, do not do this.
I know that is coding like that is bad practicing , but that is what he asking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.