dqm74406 2018-10-29 19:10
浏览 68

如何仅在while循环中显示一次值,但显示html循环的长度

I have a while loop that keeps track of two variables, incrementing them as the condition passes or fails. It is definitely incrementing them but it shows every iteration of the increment. I am only wanting to show the values once the while loop has finished. I know I need to close the loop and then display the value but I can't have that happen as I need the HTML to display multiple times for multiple users. I am wondering how to get the HTML to display multiple times but only have the two variables display for each user.

$addedItem = $tradedItem = 0;
$sql = "SELECT DISTINCT adminid, log_operation FROM item_log_table WHERE timestamp BETWEEN '$time' and '$time2' ORDER BY adminid ASC";
$result = $db->prepare($sql);
$result->execute();
while ($row = $result->fetch()) {
    $username = $functions->getUserInfoSingularFromId('username', $row['adminid']);
    $lastLogin = $functions->getUserInfoSingularFromId('previous_visit', $row['adminid']);
    $lastLogin = $adminfunctions->displayDate($lastLogin);

    if ($row['log_operation'] == "ADDED ITEM") {
        $addedItem += 1;
    } else {
        $tradedItem += 1;
    } ?>

    <div class="row">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="border-radius: 16px; margin-top: 25px;">
            <div class="well profile col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center">
                    <p style="text-align:center;font-size: smaller;"><strong>Name | Employee: </strong><span class="tags" id="user-status"><?php echo $username; ?> | <?php echo $row['adminid']; ?></span></p>
                    <p style="text-align:center;font-size: smaller;"><strong>Date Last Logged In: </strong><span class="tags" id="user-status"> <?php echo $lastLogin; ?></span></p>

                    <div class="col-lg-6 left" style="text-align:center;overflow-wrap: break-word;">
                        <h4><p style="text-align: center;"><strong id="user-globe-rank"><?php echo $addedItem; ?></strong></p></h4>
                        <p><small class="label label-success">Items Added</small></p>                    
                     </div>
                     <div class=" col-lg-6 left" style="text-align:center;overflow-wrap: break-word;">
                         <h4><p style="text-align: center;"><strong id="user-college-rank"><?php echo $tradedItem; ?></strong></p></h4>
                         <p> <small class="label label-warning">Items Traded</small></p>
                     </div>
                 </div>
             </div>
         </div>
     </div>
<?php } ?>

What it looks like in the loop

I only want it to show the last iteration of the $addedItem and $tradedItem for each user. Also, excuse the style attributes I'm just prototyping.

  • 写回答

1条回答 默认 最新

  • dph87312 2018-10-29 19:25
    关注

    Since you are using DISTINCT you will only retrieve a single record for the adminid that has log_operation of ADDED ITEM.

    For example if your table has:

    | adminid | log_operation  |
    | 1       |  ADDED ITEM    |
    | 1       |  ADDED ITEM    |
    | 1       |  TRANSFER ITEM |
    | 2       |  ADDED ITEM    |
    

    Your query would only retrieve

    | adminid | log_operation  |
    | 1       |  ADDED ITEM    |
    | 1       |  TRANSFER ITEM |
    | 2       |  ADDED ITEM    |
    

    Resulting in $addedItem always equalling 1 for a specific adminid. Since you're not resetting $addedItem per adminid, your loop will continue incrementing it for each adminid.

    To resolve the issue and total all the log_operations that are ADDED ITEM for an adminid use aggregate functions on the desired log_operation value.

    SELECT 
        adminid,
        SUM(CASE log_operation WHEN 'ADDED ITEM' THEN 1 ELSE 0 END) AS addedItems,
        SUM(CASE log_operation WHEN 'ADDED ITEM' THEN 0 ELSE 1 END) AS tradedItems
     FROM item_log_table
     WHERE `timestamp` BETWEEN '$time' AND '$time2'
     GROUP BY adminid
     ORDER BY adminid ASC
    

    This will retrieve a single row for each adminid, with the total number of log operations that are ADDED ITEM and not.

    Example: https://www.db-fiddle.com/f/muAESWHphmaBheZmf9wZWy/1

    | adminid | addedItems | tradedItems |
    | 1       |  2         | 1           |
    | 2       |  1         | 0           |
    

    Then just retrieve those columns in your html.

    <div class="col-lg-6 left" style="text-align:center;overflow-wrap: break-word;">
        <h4><p style="text-align: center;"><strong id="user-globe-rank"><?php echo  $row['addedItems']; ?></strong></p></h4>
        <p><small class="label label-success">Items Added</small></p>                    
     </div>
     <div class=" col-lg-6 left" style="text-align:center;overflow-wrap: break-word;">
         <h4><p style="text-align: center;"><strong id="user-college-rank"><?php echo $row['tradedItems']; ?></strong></p></h4>
          <p> <small class="label label-warning">Items Traded</small></p>
     </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型