Domanda

I am creating an intranet for a vehicle hire company

I set the default price as such:

$dayRate = '90.00';

I have created a field on my table to store a discount day rate if needed, called disc_day_rate which defaults to 0.

I pull the discount price as such

$discDayRate = $hire['disc_day_rate'];

I wish to find the lowest of these two numbers, but I think the default disc_day_rate of 0 is causing issues

I have tried using min(); and if ($discDayrate == "0") methods but after finding many answers on stackoverflow without having to post my own It's time to ask for help with an elegant solution

È stato utile?

Soluzione

This ensures the 0.00 will never cause you a problem.

if ($discDayRate == 0.00) { ## don't use quotes here; it should be saved as a DECIMAL or INT in the database
   $the_rate = $dayRate; ## back to the default
}
else {
   if ($discDayRate < $dayRate) {
      $the_rate = $diskDayRate;
   }
   else {
      $the_rate = $dayRate;
   }
}

$the_rate has your desired rate.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top