Div 具有简单的可变高度

Div with a simple variable height

所以,我想制作一个 div,它具有我将从数据库中获取的值的高度。为了更好地理解,该值是客户在网站上购买的次数。我正在想办法让我的 div 随着价值的上升而上升。

像这样:

var divHeight = numberOfValues;
numberOfValues = heightOfDiv; (div's height property)

if (numberOfValues == 0) {
   divHeight = 0;
} else {
    divHeight = numberOfValues + 10;
}

逻辑是让我的 div 每个值数字增加 10px。因此,如果值为 10,则高度应为 100px。

实现此解决方案的最佳方法是什么?有我想的那么简单吗? (简单!=简单)。

    <script>
       var divHeight = numOfVisits * 10;
       var divDisplay = document.getElementById("display");

       if(divHeight>0)
              divDisplay.style.height = divHeight + "px";
       else
              divDisplay.style.height = "1px";
    </script>
<?php
$numberOfPurchases = getNumberOfPurchases(); // call the function that queries the database
?>
<script>
var defaultDivHeight = 10;
var purchaseDiv = document.getElementById('purchaseDiv');
if (<?= numberOfPurchases; ?> > 0)
{
    purchaseDiv.style.height = <?= numberOfPurchases; ?> * defaultDivHeight + 'px';
}
else
{
    purchaseDiv.style.height = defaultDivHeight + 'px';
}
</script>

我建议研究在 PHP 中使用 PDO 来查询数据库。