简体   繁体   English

如何根据数据库中的数据更改div的位置

[英]How to change a div's position according to data from database

So I use an api connection to fetch some data. 因此,我使用api连接来获取一些数据。 The information I get represents a barlike div with a: 我得到的信息代表一个条形的div,其中包含:

$target = number_format((float)$data->data[2][32][3], 2, '.', '');

And an average of an array: 和一个数组的平均值:

public static function try($data)
        {

            $array = [];
            $sum = 0;
            $path = $data->data[2];
            foreach($path as $key => $item){
                if($key > 1 && isset($item[5])){ 
                    $array[] = (int)$item[5];
                    $sum += (int)$item[5];
                }
            }
            $avg = ($sum / count($array));
            return json_encode(number_format((float)$avg, 2, '.', ''));
        }

So the target is: 9.33% and the average is 14.77% I tried to make a function to fill a div (.outer) using another div (.inner) and the target is a third div shaped like a bar (.target). 所以目标是:9.33%,平均值是14.77%。我试图使一个函数使用另一个div(.inner)填充div(.outer),目标是形状像条形图(.target)的第三个div。 What I try to do is make the .iner div complete a percentage of the .outer div according to the result(in this case 14.77). 我想做的是根据结果使.iner div占.outer div的百分比(在本例中为14.77)。 The problem occurs when the average (.iner div) is higher than the target (which right now is the .outer div width). 当平均值(.iner div)高于目标值(现在是.outer div宽度)时,就会出现此问题。 Right now the .target div is not dynamic, it is placed in the right of the .outer div, the .outer div representing the target. 现在,.target div不是动态的,它位于.outer div的右侧,.outer div表示目标。 This doesn't work when the average is higher than the target. 当平均值高于目标值时,这将不起作用。 When this happens I want to make the .target div go behind the .inner div procentually according to how much the average exceeds the target. 当发生这种情况时,我想根据平均值超出目标的数量来使.target div落后于.inner div。 CSS: CSS:

.outer, .inner, .target {
      height: 14px;
      margin-bottom: 5px;
    }

    .outer {
      background-color: #cccccc;
      width: 200px;
      margin: 0 auto;
    }

    .inner {
      background-color: #66a3ff;

    }

    .target {
       background-color: black;
       width: 3px;
       height: 14px;
       float: right;
    }

HTML: HTML:

<div class="outer"> 
        <div class="target"></div>                   
        <div class="inner" style="width: <?php echo round( 100 * \Helper::getDataForTry($data)) ?>%;"></div>


</div>

Function for percentage : 百分比函数:

public static function getDataForTry($data)
        {
            $target = number_format((float)$data->data[2][32][3], 2, '.', '');
            $array = [];
            $sum = 0;
            $path = $data->data[2];
            foreach($path as $key => $item){
                if($key > 1 && isset($item[5])){ 
                    $array[] = (int)$item[5];
                    $sum += (int)$item[5];
                }
            }
            $avg = ($sum / count($array));
            $percent = $avg / $target;
            return json_encode(number_format((float)$percent, 2, '.', ''));
        }

Image of how the final product should look like: 最终产品外观的图像:

图片

In the first case scenario the average exceeds the target In the second case the target is at the end of the outer div and the inner div procentually changes its width according to how much he has left until he reaches the target (which I already have done). 在第一种情况下,平均值超出目标;在第二种情况下,目标位于外部div的末尾,而内部div根据到达目标为止的剩余空间来逐渐改变其宽度(我已经完成了此操作) )。

Instead of using a black background and floating the inside divs, use positioning and borders to achieve the desired behaviour. 与其使用黑色背景并浮动内部div,不如使用定位和边框来实现所需的行为。

Also, you need to define the base value of the outer div first (PHP): 另外,您需要先定义外部div(PHP)的基值:

$target =  9.33;
$avg    = 14.77;

$base   = max($target, $avg);

$base will now be set to the highest value, in this case, it's the average (14.77). $base现在将设置为最高值,在这种情况下,它是平均值(14.77)。 Since your outer div shall be 200px wide, you can calculate the sizes of the inner divs in PHP first: 由于外部div的宽度应为200px,因此您可以先在PHP中计算内部div的大小:

$bar_width    = 200px; // Change that to whatever you want.
$target_width = $bar_width / 100 * ($target / $base * 100);
$avg_width    = $bar_width / 100 * ($avg / $base * 100);

OR calculate the width in CSS directly ( calc ): 直接在CSS中计算宽度( calc ):

<div class="outer"> 
    <div class="target" style="width: calc(<?php echo $bar_width; ?> / 100 * (<?php echo $target; ?> / <?php echo $base; ?> * 100))"></div>                   
    <div class="inner" style="width: calc(<?php echo $bar_width; ?> / 100 * (<?php echo $avg; ?> / <?php echo $base; ?> * 100))"></div>
</div>

For styling, position the outer div relatively and the inner ones absolutely, use a black border for the target without background. 要进行样式设置,请将外部div相对放置,将内部div绝对放置,对没有背景的目标使用黑色边框。

See an example below: 请参阅以下示例:

 .outer, .inner, .target { height: 14px; margin-bottom: 5px; } .outer { background-color: #cccccc; width: 200px; margin: 0 auto; position: relative; font-size: 10px; line-height: 14px; font-family: sans-serif; } .inner { background-color: #66a3ff; position: absolute; z-index: 1; width: calc(200 / 100 * 14.77px); color: white; } .target { background-color: transparent; width: 19px; position: absolute; border-right: 3px solid black; z-index: 2; color: black; text-align: right; } 
 <h3>Average higher than target</h3> <div class="outer"> <div class="target" style="width: calc(200px / 100 * (9.33 / 14.77 * 100))">9.33%&nbsp;</div> <div class="inner" style="width: calc(200px / 100 * (14.77 / 14.77 * 100))">&nbsp;14.77%</div> </div> <h3>Average lower than target</h3> <div class="outer"> <div class="target" style="width: calc(200px / 100 * (14.77 / 14.77 * 100))">14.77%&nbsp;</div> <div class="inner" style="width: calc(200px / 100 * (9.33 / 14.77 * 100))">&nbsp;9.33%</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM