试图创建交易历史记录

时间:2017-05-26 04:35:10

标签: php mysql

大家好我试图创建一个交易历史记录,从而获得数据库中的详细信息。

当用户拥有并有效订阅时,详细信息显示完美,但是当吊exp过期时没有显示任何内容并且只显示1行。

我想在历史中显示所有活动或过期的交易。这是我的代码。

Active = 1 expired = 0仅供您参考

<?php
$query = "SELECT * FROM `subscriptions` WHERE `username` = '$username'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));

while ($row = mysqli_fetch_array($result))
{
     if ($row['active'] == 1)
     {
         $status    = "Active";
         $transid   = $row['txn_id'];
         $transdate = date('F j, Y',strtotime($row['date']));
         $subid     = $row['id'];
         $payment   = $row['payment'];
     }
     else 
     {
         $status    = "Expired";
         $transid   = $row['txn_id'];
         $transdate = date('F j, Y',strtotime($row['date']));
         $subid     = $row['id'];
         $payment   = $row['payment'];
     }
}
?>

我使用if和else来显示状态是活跃还是过期。

我的HTML代码

<div class="table-responsive">
    <table class="table table-striped table-vcenter">
        <thead>
            <tr>
                <th class="text-center">ID</th>
                <th class="text-center">PRODUCT</th>
                <th class="text-center">STATUS</th>
                <th class="text-center">DATE</th>
                <th class="text-center">PAYMENT</th>
                <th class="text-center">TRANSACTION ID</th>
            </tr>
        </thead>


        <?php
        echo'    
        <tbody class="text-center">

            <tr>
                <td>'.$subid.'</td>
                <td>'.$package.'</td>
                <td>'.$status.'</td>
                <td>'.$transdate.'</td>
                <td>'.$payment.'</td>
                <td>'.$transid.'</td>
            </tr>
        </tbody>';
        ?>
    </table>
</div>

这里有一些上限,例如我想要显示这个订阅仍然有效或不是因为显然这是一个历史

active subscription 问题是,如果用户订阅过期,则不会显示在历史记录中,只显示最后一个

到期时

expired subscription

2 个答案:

答案 0 :(得分:1)

我的版本:)

<强> PHP

$query = "SELECT * FROM `subscriptions` WHERE `username` = ?";
$queryStmt = $con->prepare( $query );

if ( $queryStmt )
{
    $queryStmt->bind_param( "s", $username ); // <-- This prevents SQL injection and may be handy for executing the query several time more efficiently
    $queryStmt->execute();
    $queryStmt->bind_result( $active, $transid, $date, $subid, $payment, $package ); // <-- the order must match the order of the fields in the $query

    $rows = []; // <-- load here the content fo the DB rows
    while( $queryStmt->fetch() )
    {
        // Transform needed variables

        $status = ( $active == 1 ) ? "Active" : "Expired";
        $transdate = date( 'F j, Y', strtotime( $date ) );

        $rows[] = compact( 'satus', 'transid', 'transdate', 'subid', 'payment', 'package' );
    }

    $queryStmt->close();
}

<强> HTML

    <div class="table-responsive">
    <table class="table table-striped table-vcenter">
        <thead>
        <tr>
            <th class="text-center">ID</th>
            <th class="text-center">PRODUCT</th>
            <th class="text-center">STATUS</th>
            <th class="text-center">DATE</th>
            <th class="text-center">PAYMENT</th>
            <th class="text-center">TRANSACTION ID</th>
        </tr>
        </thead>
        <tbody class="text-center">
        <?php foreach( $rows as $row ) : ?>
            <tr>
                <td><?php echo $row['subid'] ?></td>
                <td><?php echo $row['package'] ?></td>
                <td><?php echo $row['status'] ?></td>
                <td><?php echo $row['transdate'] ?></td>
                <td><?php echo $row['payment'] ?></td>
                <td><?php echo $row['transid'] ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
        ?>
    </table>
</div>

答案 1 :(得分:0)

您最初可以将数据库值设置为array

PHP代码

<?php
$query = "SELECT * FROM subscriptions sub LEFT JOIN package pack ON (pack.id=sub.package) WHERE sub.username = '$username'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$tableArray = array();
$counter = 0;
while ($row = mysqli_fetch_array($result))
{
     $tableArray[$counter]['status'] = $row['active'] == 1 ? 'Active' : 'Expired';
     $tableArray[$counter]['transid'] = $row['txn_id'];
     $tableArray[$counter]['transdate'] = date('F j, Y',strtotime($row['date']));
     $tableArray[$counter]['subid'] = $row['id'];
     $tableArray[$counter]['payment'] = $row['payment'];
     $counter++;
}
?>

现在将此数组访问到html部分&amp;通过foreach

迭代它

<强> HTML

 <div class="table-responsive">
    <table class="table table-striped table-vcenter">
        <thead>
            <tr>
                <th class="text-center">ID</th>
                <th class="text-center">PRODUCT</th>
                <th class="text-center">STATUS</th>
                <th class="text-center">DATE</th>
                <th class="text-center">PAYMENT</th>
                <th class="text-center">TRANSACTION ID</th>
            </tr>
        </thead>
        <tbody class="text-center">
            <?php foreach($tableArray as $row) {?>
            <tr>
                <td> <?php echo $row['id'] ?></td>
                <td><?php echo $row['package'] ?></td>
                <td><?php echo $row['status'] ?></td>
                <td><?php echo $row['transdate'] ?></td>
                <td><?php echo $row['payment'] ?></td>
                <td><?php echo $row['transid'] ?></td>
            </tr>
            <?php } ?>
        </tbody>
        ?>
    </table>
</div>

在HTML中,$row['package']将是未定义的,您需要从数据库中获取它,如果这是数据库中的字段。你的问题没有这个领域。请同时包含它

相关问题