1

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>
Peter Wang
  • 13
  • 5

2 Answers2

0

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";
}
?>
meda
  • 45,103
  • 14
  • 92
  • 122
  • So in_array checks if the value is there, but does it also call back the instance of the value? – Peter Wang Jan 31 '15 at 00:42
  • @PeterWang YEAH i dont see why not, try it – meda Jan 31 '15 at 00:47
  • @PeterWang get back to me bro, I can always adjust my answer, what did you need exactly – meda Jan 31 '15 at 01:38
  • Okay, I'll explain like this. The top is a box where you assign a value as "quantity" which interacts with "Price" Like the bottom box, its a text input. The bottom is the quantity box in the product page. This gets traced back to quantity in the other box, and sets the price based on the quantity. What I want : If they user inputs a value lower than the lowest value in the array, it will not add to the cart and throw an error saying "add more" This is all on wordpress ecommerce – Peter Wang Jan 31 '15 at 02:27
0

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; 
LG_PDX
  • 834
  • 8
  • 12
  • The way that the array works is that it is a text input that discounts price based on the quantity that people add to the cart. On the left dimension, there is quantity. on the right there is a price based on the quantity. So like 50 for 0.45 cents 100 for 0.40 300 for 0.38 (...) I need to call the lowest quantity in it and then use it to compare to the class WPSC_quantity_update which contains the value that gets updated to the cart. I want to return false on the button if its below the lowest quantity in the array. – Peter Wang Jan 31 '15 at 01:26
  • Well table_rate_price[1][] adds to the array (odd php shortcut for array_push), you just want a value it sounds like so you must provide an index in the 2nd slot. – LG_PDX Jan 31 '15 at 01:31
  • if it helps, go a page on the website. [link](http://inkvia.com/?wpsc-product=apple-shaped-sticky-note) – Peter Wang Jan 31 '15 at 01:33