I have a cycle that displays options for products with a price. How to make sure that the radio tag always has a checked mark on the option with the maximum price?

 <?php foreach ($option['product_option_value'] as $option_value) { ?> <input type="radio" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option_value['product_option_value_id']; ?>" /> <?php echo $option_value['name']; ?> <?php echo $option_value['price']; ?> <?php } ?> 
  • 2
    At the beginning, you need to add the maximum value to the $max variable, and then simply compare it with it <input type="radio" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option_value['product_option_value_id']; ?>" <?= $option_value['price'] == $max ? 'checked': '' ; ?> /> <input type="radio" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option_value['product_option_value_id']; ?>" <?= $option_value['price'] == $max ? 'checked': '' ; ?> /> <input type="radio" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option_value['product_option_value_id']; ?>" <?= $option_value['price'] == $max ? 'checked': '' ; ?> /> - Alexey Shimansky
  • Thanks for the help, it works! :) - akasergej
  • @ Alexey Shimansky Code in the comments is inconvenient to read, make out the answer. - AK
  • @AK ...... done! - Alexey Shimansky

1 answer 1

At the beginning it is necessary to add the maximum value to the $max variable, and then simply compare with it

 <input type="radio" ....... <?= $option_value['price'] == $max ? 'checked': '' ; ?> /> 

How you find the maximum value is up to you. If you had them all in an array, it was enough to do so:

 $max = max($array_of_values);