dsa1234569 2017-01-11 12:36
浏览 51
已采纳

如何根据SQL列中的值扩展DIV宽度

What I want to achieve: My desired outcome

So essentially when I upvote a client the green bar will increase and when I down vote the red bar increases.

I currently have it working so that when you up or down vote it submits a value to the database in 2 different columns: Positive and Negative. In the picture where the bars are, I currently have empty divs.

So how do I expand the div's based on the number of votes?

My clients.php:

<?php

$clientInfo = "SELECT * FROM Clients ORDER BY Client ASC";
$stmt = sqlsrv_query($conn, $clientInfo);

echo "<div style='width: 100%; display: inline-block;'>";

while ($client = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
    echo "<div class='clientid' style='height: 50px; font-size: 18px; vertical-align: middle; display: inline-block; width: 100%'>" . 

            "
            <div style='width: 35%;'>
                <div style='color: green;'></div>
            </div>

            <div style='display: inline-block; width: 5%;'>      
                <span style='font-size: 20px;' class='hover-cursor fa fa-chevron-up vote-up'></span>
            </div>" .

                 "<div class='hover-cursor hvr-underline-reveal voteup votedown' data-clientid='{$client['ClientID']}' style='width: 20%; display: inline-block;'>" . $client['Client'] . "</div>" . 

            "<div style='display: inline-block; width: 5%;'>
                <span style='font-size: 20px; text-align: right;' class='hover-cursor fa fa-chevron-down vote-down'></span>
            </div>

            <div style='width: 35%;'>
                <div style='color: red;'></div>
            </div>

         </div> 
        <br />";
}

echo "</div>";

?>

Its the two divs with a width of 35% that will be the progress bars.

The columns should show based on a percentage of the total votes, so say there are 4 votes, 3 up 1 down, it shows 75% green, 25% red.

  • 写回答

1条回答 默认 最新

  • doushi4633 2017-01-11 15:39
    关注

    To do this you should take the positive and negative votes from your database and count the total number of votes.

    Then you should find the percentage of both and set the width in you code. Remember to add an "%" and the end so the browser knows you're working with percentage.

    If we take your example above, where 1 is down and 3 is up the math look like this:

    y = (1/4)*100 = 25%
    

    Here I have implemented it in your code:

    $clientInfo = "SELCT * FROM Clients ORDER BY Client ASC";
    $stmt = sqlsrv_query($conn, $clientInfo);
    
    echo "<div style='width: 100%; display: inline-block;'>";
    
    while ($client = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
      $positive = $client['positive'];
      $negative = $client['negative'];
      $total = ($positive+$negative);
    
      // Calculate width in percentage
      $positiveWidth = ($positive/$total)*100."%";
      $negativeWidth = ($negative/$total)*100."%";
    
      // The output
      echo "<div class='clientid' style='height: 50px; font-size: 18px; vertical-align: middle; display: inline-block; width: 100%'>" . 
    
        "
        <div style='width: $positiveWidth;'>
            <div style='color: green;'></div>
        </div>
    
        <div style='display: inline-block; width: 5%;'>      
            <span style='font-size: 20px;' class='hover-cursor fa fa-chevron-up vote-up'></span>
        </div>" .
    
             "<div class='hover-cursor hvr-underline-reveal voteup votedown' data-clientid='{$client['ClientID']}' style='width: 20%; display: inline-block;'>" . $client['Client'] . "</div>" . 
    
        "<div style='display: inline-block; width: 5%;'>
            <span style='font-size: 20px; text-align: right;' class='hover-cursor fa fa-chevron-down vote-down'></span>
        </div>
    
        <div style='width: $negativeWidth;'>
            <div style='color: red;'></div>
        </div>
    
      </div> 
      <br />";
    }
    
    echo "</div>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP