mysql查询重新调整只有1个结果

时间:2015-08-17 14:17:45

标签: php mysql

我在页面上运行了一个MySQL查询,出于某种原因,我在PHP应用程序上运行时会回复1行。

但是当我在PHP管理员中运行完全相同的查询时,它会返回正确的行数。

这是我正在使用的代码:

<?php 
    include ('includes/headFunctions.php'); 
    include('includes/head.php');
?>

<div class="row container">

<?php
if(isset($_POST['weekly_search'])) {

    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];    

    $reporting = DB::getInstance()->query("SELECT * FROM transactions WHERE transaction_date BETWEEN '2015-08-01' AND '2015-08-17'");

    if(!$reporting->count()) {
        echo 'No results';   
    } else {
        $_SESSION['weekly_records'] = 'true';
        //if I run the foreach here it works correctly
    }
}

    $week_start_date = date("Y-m-d", strtotime('last Sunday'));
    $week_end_date = date("Y-m-d");

    //echo $start_date, '<br>';
    //echo $end_date;
    //die();

            $results = DB::getInstance()->query("SELECT * FROM transactions WHERE transaction_date BETWEEN '$week_start_date' AND '$week_end_date'");       

            if ($results) {

                    $add = 0;
                    $minus = 0;
                    $total_transAdded = 0;
                    $total_transRedeemed = 0;

                    foreach ($results->results() as $result) {
                        $add = $total_transAdded += $result->transaction_add;
                        $minus = $total_transRedeemed += $result->transaction_redeem;
                    };

                    //print Elements
                    echo '<div class="row"><h1>Week Ending:</br> ', $week_end_date, '</h1></div>';
                    echo '<div class="row">';
                    echo '<div class="col-xs-6"><h2>Rewarded</h2>', '<h3>£', round ($add, 2), '</h3></div>';
                    echo '<div class="col-xs-6"><h2>Redeemed</h2></div>', '<h3>£', round ($minus, 2), '</h3></div>';
                    echo '</div>';

            } else {
                echo '<div class="error">No records found</div>';
            } //end if search->data

?>

    <div class="row">
        <div class="col-lg-12">
            <a class="btn btn-primary btn-lg" href="#" data-toggle="modal" data-target="#date_picker_modal">Search By Date</a>
        </div>
    </div>

</div> <!-- End row container -->


<!-- Report Generator Modal -->
<div class="modal fade" id="date_picker_modal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
            <h4 class="modal-title" id="myModalLabel">Add Transaction</h4>
            </div>
            <div class="modal-body">
                <form action="" method="post">
                    <div class="input-group">
                        <span class="input-group-addon">Start Date</span>
                        <input type="date" min='0001-01-01' max='9999-12-31' name="start_date" class="form-control" step="any" min="0" required>
                    </div>
                    <hr>
                    <div class="input-group">
                        <span class="input-group-addon">End Date</span>
                        <input type="date" min='0001-01-01' max='9999-12-31' name="end_date" class="form-control" step="any" min="0" required>
                    </div>
            </div>
            <div class="modal-footer">
                    <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
                    <button type="submit" name="weekly_search" class="btn btn-primary">Search</button>
                </form>
            </div>
    </div>
  </div>
</div>

<?php if(isset($_SESSION['weekly_records'])) {?>

<!-- Report Generator Modal -->
<div class="modal fade" id="weekly_records" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
            <h4 class="modal-title" id="myModalLabel">Reports</h4>
            </div>
            <div class="modal-body">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>Customer</th><th>Added</th><th>Redeemed</th><th>Date</th>
                        </tr>
                    </thead>
                    <tbody>

                        <?php
                            //running the foreacheach only retunrs the last result
                            foreach($reporting->results() as $report) {
                                echo '<tr>';
                                echo '<td><a href="cust_history.php?custid=',$report->cust_id, '">View</a></td>';
                                echo '<td>£', round($report->transaction_add, 2), '</td>';
                                echo '<td>£', round($report->transaction_redeem, 2), '</td>';
                                echo '<td>', $report->transaction_date, '</td>';
                                echo '</tr>';
                            }

                        ?>
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
            </div>
    </div>
  </div>
</div>

<?php } ?>




<?php include 'includes/footer.php'; ?>

1 个答案:

答案 0 :(得分:2)

试试这个:

foreach($reporting->results() as $report) {
                                echo '<tr>';
                                echo '<td><a href="cust_history.php?custid=',$report->cust_id, '">View</a></td>';
                                echo '<td>£', round($report->transaction_add, 2), '</td>';
                                echo '<td>£', round($report->transaction_redeem, 2), '</td>';
                                echo '<td>', $report->transaction_date, '</td>';
                                echo '</tr>';
                            }