如何使用PHP停止连续罚款

时间:2017-08-22 13:29:56

标签: php mysqli

我想在点击它并将其保存在数据库中后停止我的代码中的连续罚款我的问题是因为如果我点击按钮它只保存在return.php中,但计数是连续的每天就像在图片中一样,我已经尝试过,但它没有工作

  

view_borrow.php

<div class="container">
<form method="POST" action="return_save.php">
    <div class="margin-top">
        <div class="row">   
            <div class="span12">        
                    <div class="alert alert-info"><strong>Borrowed Books</strong></div>
                        <table cellpadding="0" cellspacing="0" border="0" class="table" id="example">

                            <thead>
                                <tr>
                                    <th>Book title</th>                                 
                                    <th>Borrower</th>                                 
                                    <th>Type</th>                                 
                                    <th>Date Borrow</th>                                 
                                    <th>Due Date</th>                            
                                    <th>Date Returned</th>
                                    <th>Fines</th>
                                    <th>Borrow Status</th>
                                </tr>
                            </thead>
                            <tbody>

                              <?php  $user_query=mysqli_query($dbcon,"select * from borrow
                            LEFT JOIN member ON borrow.member_id = member.member_id
                            LEFT JOIN borrowdetails ON borrow.borrow_id = borrowdetails.borrow_id
                            LEFT JOIN book on borrowdetails.book_id =  book.book_id 
                            ORDER BY borrow.borrow_id DESC
                              ")or die(mysqli_error());
                                while($row=mysqli_fetch_array($user_query)){


$currentdate = date('Y/m/d');

$start = new DateTime($returndate=$row['due_date']); 

$end = new DateTime($currentdate);



$fines =0;

if(strtotime($currentdate) > strtotime($returndate)){
$days= $start->diff($end, true)->days;


$fines = $days > 0 ? intval(floor($days)) * 15 : 0;

$fi = $row['borrow_details_id'];
mysqli_query($dbcon,"update borrowdetails set fines='$fines' where borrow_details_id = '$fi'");

}

                                $id=$row['borrow_id'];
                                $book_id=$row['book_id'];
                                $borrow_details_id=$row['borrow_details_id'];

                                    ?>
                                <tr class="del<?php echo $id ?>">


                                <td class="test"><?php echo $row['book_title']; ?></td>
                                <td class="test"><?php echo $row['firstname']." ".$row['lastname']; ?></td>
                                <td class="test"><?php echo $row['type']; ?></td>
                                <td class="test"><?php echo $row['date_borrow']; ?></td> 
                                <td class="test"><?php echo $row['due_date']; ?> </td>
                                <td class="test"><?php echo $row['date_return']; ?> </td>
                                 <td class="test"><?php echo "₱ ".$fines; ?></td>
                                <td class="test"><?php echo $row['borrow_status'];?></td>
                                <td > <a rel="tooltip"  title="Return" id="<?php echo $borrow_details_id; ?>"
                                 href="#delete_book<?php echo $borrow_details_id; ?>" data-toggle="modal"   
                                  class="btn btn-success"><i class="icon-check icon-large"></i>Return</a>
                                <?php include('modal_return.php'); ?>
                                <td></td> 

                                </tr>
                                <?php  }  ?>
                            </tbody>
                        </table>


        </div>  
        </div>
    </div>
    </form>
</div>

这是我点击返回按钮

的模态
  

modal_return.php

    <div id="delete_book<?php echo $borrow_details_id; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-body">
        <div class="alert alert-success">Do you want to Return this Book?</div>

    </div>
    <div class="modal-footer">
        <a class="btn btn-success" href="return_save.php<?php echo '?id='.$id; ?>&<?php echo 'book_id='.$book_id; ?>">Yes</a>
        <button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-remove icon-large"></i>&nbsp;Close</button>
    </div>
</div>

This is the table

1 个答案:

答案 0 :(得分:0)

您在$start$end$currentdate)之间进行了日期差异,但从未使用过$returndate。 PHP 7有一个空的合并运算符??,对于您的场景非常方便(否则只使用普通if/else或其三元等价物?:)。

使用现有代码,您的费用始终为borrowdatecurrentdate。以下代码可以解决您的问题。

$borrowdate = new Datetime($row['due_date']);
$currentdate = new Datetime();
$returndate = new Datetime($row['date_return']);

// PHP >= 7
$days = $borrowdate->diff($returndate ?? $currentdate, true)->days;

// PHP < 7
$days = $borrowdate->diff($returndate ?: $currentdate, true)->days;