繁体   English   中英

如何使用php条件语句查询mysql表并显示结果

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

我想向用户展示他在成功付款确认后已经给他的山羊接种了疫苗。
这是我尝试过的:

<?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';         
}

?>

我已显示疫苗接种间隔的剩余天数 如果确认用户支付疫苗接种费用,我还想显示“已接种疫苗”。 $product_row['treat1']; 是指定接种天数的列。 order_details是一个订单表,其中确认订单的确认列。

山羊疫苗接种:
因为山羊不会得自闭症

请阅读如何使用 PHP DateTime 对象,因为它们将使您在这个项目中的生活更轻松。

我将重写您的代码,然后告诉您新代码的作用:

//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';         
}

希望在上面链接的帮助下,您应该能够看到我为您写的内容的基础知识。

一些注意事项:

  • 停止制作只是其他变量副本的变量,这是低效且令人困惑的。

  • (尝试)停止将数据作为 GET URL 值发送到新的 PHP 页面,例如/newpage.php?treatp=".$var." . 这很可能非常不安全。

  • 更具体的答案需要您对 MySQL 表进行完整解释,如果需要,请编辑您的问题并添加它们。

  • 尝试不再使用时间戳并将时间戳变量视为数学实体,而是使用DateTime对象。

  • 确实,要确定是否付款,您应该在数据库中有一个付款日期值(付款成功时设置),然后简单地将此日期与付款涵盖的天数的整数值进行比较,使用->diff()方法如上所示。

在此处输入图片说明

有关山羊疫苗接种的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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