Igbeh Emmanuel
Igbeh Emmanuel

Reputation: 53

How can I query mysql table and display result using php conditional statements

I want to show user that he has vaccinated his goat after successfully payment confirmation.
Here is what I tried:

<?php
$treatperiod1=$product_row['treat1'];
$currentDate = date("Y-m-d");
$totalDays = $treatperiod1;
$usedDays = round(abs(strtotime($currentDate)-strtotime($orderdate))/60/60/24);
$remainingDays = $totalDays-$usedDays;

if($remainingDays==0) {
    echo "<a target = '_blank' href ='product_addon.php?treatp=$treatperiod1' class='btn btn-success'><i class='fa fa-pencil'></i>Vaccinate Gaoat</a>";
} elseif($remainingDays==$treatperiod1) {
    echo 'Vaccinated';
} else {
    echo $remainingDays.' Days Left';         
}

?>

I have displayed remaining days for vaccination intervals I want to also display 'Vaccinated' if user payment for vaccination is confirmed. $product_row['treat1']; is a column where the number of days for vaccination is specified. order_details is a table for orders with column confirm for confirmed orders.

Upvotes: 0

Views: 68

Answers (1)

Martin
Martin

Reputation: 22760

Goat Vaccination:
Because Goats don't get Autism

Please read how to use PHP DateTime objects because they will make your life a lot easier on this project.

I will rewrite your code and then tell you what the new code does:

//establish DateTime objects. 
$currentDate = new DateTime(); //now.
$orderDate   = new DateTime($orderdate); //$orderdate format Y-m-d

//Establish how may days since order date and today.
$difference  = $orderDate->diff($currentDate);
$usedDays    = $difference->days;  

/*** 
 Total Period paid for,
 in days, integer.  
 $product_row['treat1'];
 ***/

 $remainingDays = $product_row['treat1'] - $usedDays;
 //Returns an integer value of remaining days. 


if($remainingDays < 1) {
    echo "<a target = '_blank' 
          href ='product_addon.php?treatp=".$treatperiod1."'>
          Vaccinate Goat</a>";
} elseif($remainingDays==$product_row['treat1']) {
    //This will only fire if the goat was vaccinated TODAY.
    echo 'Vaccinated';
} else {
    echo 'Vaccinated:' .$remainingDays.' Days Left';         
}

Hopefully with the help of the link above you should be able to see the basics of what I wrote out for you.

Some notes:

  • Stop making variables that are simply copies of other variables, it's inefficient and confusing.

  • (Try to) Stop sending data to new PHP pages as GET URL values such as /newpage.php?treatp=".$var.". This is very probably very insecure.

  • A more specific answer will need a full explanation from you of your MySQL tables, if nessecary please edit your question and add them.

  • Try and move away from using timestamps and treating timestamp variables as mathematical entities, and use the DateTime object instead.

  • Really, to determine if a payment is made you should have a payment date value in your database (set when a payment is successful) and simply compare this date to the value of the integer of the number of days payment covers, using the ->diff() method illustrated above.

enter image description here

Further information on Goat Vaccinations

Upvotes: 1

Related Questions