简体   繁体   中英

PHP If statement comparing an text input with a 2D array?

I need to make a task where it compares an array value with an inputted text value.

For the array, the code is this:

<div class="wpsc-quantity-discounts">
        <table>
            <thead>
                <tr>
                    <th class="qty" colspan="2">Quantity:</th>
                    <th class="curr"><span class="hidden">Currency:<span></th>
                    <th class="price">Price:</th>
                </tr>
            </thead>
            <tbody>
                                                <tr>
                                <td class="remove"><a href="#" class="remove_line dashicons dashicons-dismiss"></a></td>
                                <td class="qty">
                                    <input type="text" size="5" value="500"/*this value*/ name="table_rate_price[quantity][]" />
                                    +                                   </td>
                                <td class="curr">USD $</td>
                                <td><input class="newCurrPrice text" value="0.48" name="table_rate_price[table_price][]" /></td>

It already contains the variables I need. I need to compare the first column with an inputted text from my single product page.

                            <?php if(wpsc_has_multi_adding()): ?>
                            <fieldset><legend style="float:left;"><?php _e('Quantity', 'wpsc'); ?>: &nbsp;</legend>
                            <div class="wpsc_quantity_update">
                            <input type="text" id="wpsc_quantity_update_<?php echo wpsc_the_product_id(); ?>" name="wpsc_quantity_update" size="2" value="500"/*This value*/ />
                            <input type="hidden" name="key" value="<?php echo wpsc_the_cart_item_key(); ?>"/>
                            <input type="hidden" name="wpsc_update_quantity" value="true" />
                            </div><!--close wpsc_quantity_update-->
                            </fieldset>

I need to make an if statement where if the inputted text is less than the first column's array, it will return false. If anybody has a suggestion that'd be awesome. I'm still kind of a noob in php so be easy :p. Thanks.

This is the code that I have now.

 <fieldset><legend style="float:left;"><?php _e('Quantity', 'wpsc'); ?>: &nbsp;</legend> <div class="wpsc_quantity_update"> <input type="text" id="wpsc_quantity_update_<?php echo wpsc_the_product_id(); ?>" name="wpsc_quantity_update" size="2" value="500" /> <input type="hidden" name="key" value="<?php echo wpsc_the_cart_item_key(); ?>"/> </div><!--close wpsc_quantity_update--> <php? if ( table_rate_price[1][0] > wpsc_quantity_update(value)): /> <return <input type="hidden" name="wpsc_update_quantity" value="false" /> <?php else: ?>\\ return <input type="hidden" name="wpsc_update_quantity" value="true" />; </fieldset> 

Check documentation for in_array I believe that's what you are looking for.

Here is a snippet to get you started:

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
if (in_array("mac", $os)) {
    echo "Got mac";
}
?>

Looks like table_rate_price[1][] should be table_rate_price[1][0] ? Can't tell without seeing how that array looks, do a print_r($table_rate_price) and show us the results.

I would cast value to an int or float before the comparison since all post data will be String. I'm assuming wpsc_quantity_update() returns the $_POST['wpsc_quantity_update'] value.

floatval() * decimals like 0.48, 3.14, ...

intval * whole numbers 1, 2, 3 ...

if ( table_rate_price[1][0] < floatval(wpsc_quantity_update())) {
  return false;
}
return true; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM