第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

平均非整數(shù)時(shí)如何顯示半星(星級(jí))

平均非整數(shù)時(shí)如何顯示半星(星級(jí))

PHP
拉風(fēng)的咖菲貓 2023-08-26 17:45:36
我使用此代碼來顯示帶有半滿星的平均評(píng)分(如果平均評(píng)分不是整數(shù))。效果很好。<?php$stars   = '';for ( $i = 1; $i <= $avrating + 1; $i++ ) {$width = intval( $i - $avrating > 0 ? 20 - ( ( $i - $avrating ) * 20 ) : 20 );if ( 0 === $width ) {    continue;}    if($avrating < 5){        $stars .= '<span style="overflow:hidden; width:' . $width . 'px" class="dashicons dashicons-star-filled"></span>';        if ( $i - $avrating > 0 ) {            $stars .= '<span style="overflow:hidden; position:relative; left:-' . $width .'px;" class="dashicons dashicons-star-empty"></span>';         }    }}if($avrating >= 5){    for ($i = 1; $i <= 5; $i++) {        echo '<span style="overflow:hidden;" class="dashicons dashicons-star-filled"></span>';    }  }  echo $stars;?> 但是,在這種情況下,并非所有星星都會(huì)顯示。例如,如果評(píng)分為3.5,則顯示4顆星,第四顆星為半滿。這是圖像:1 : https://i.stack.imgur.com/2aoGx.png但我需要顯示所有五顆星:最后一個(gè)空的四分之一是半填充的(例如,如果評(píng)級(jí)為 3.5)并且前三個(gè)已滿。我可以把所有的星星都拿出來。有了這個(gè)代碼:<?php$stars = '';for ( $i = 1; $i <= 5 ; $i ++ ) {    if ( $i <= $avrating ) {        $stars .= '<span class="dashicons dashicons-star-filled"></span>';    } else {        $stars .= '<span class="dashicons dashicons-star-empty"></span>';    }}                                   echo   $stars;?>  但結(jié)果,我自然達(dá)不到這個(gè)結(jié)果。這是我得到的:你看,第四顆星里沒有一半。我認(rèn)為可以將這兩個(gè)代碼片段結(jié)合起來,但我的知識(shí)還不夠,如何做到這一點(diǎn)?;蛘咭苍S這根本不可能。請(qǐng)幫助解決這個(gè)問題。
查看完整描述

2 回答

?
弒天下

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊

我將創(chuàng)建一個(gè)簡單的函數(shù),它根據(jù)給定的評(píng)級(jí)返回給定星星的填充水平:


<?php

function getStarClass(int $starPosition, float $rating): string

{

  if ($starPosition <= $rating) {

    return 'filled';

  }

  if ($starPosition - $rating < 1) {

    return 'half-filled';

  }

  return 'empty';

}


$avrating = 3.5;

?>


<?php for ($star = 1; $star <= 5; $star++): ?>

  <span class="dashicons dashicons-star-<?= getStarClass($star, $avrating) ?>"></span>

<?php endfor ?>

請(qǐng)注意,我還使用 PHP 的替代語法來顯示 HTML,因?yàn)槲蚁嘈潘m合該目的,但這完全是可選的。


查看完整回答
反對(duì) 回復(fù) 2023-08-26
?
Qyouu

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊

您可以添加一個(gè)額外的子句,檢查循環(huán)是否大于評(píng)級(jí)但小于下一個(gè)評(píng)級(jí)作為半啟動(dòng)評(píng)級(jí)...


$stars = '';

for ( $i = 1; $i <= 5 ; $i ++ ) {

    if ( $i <= $avrating ) {

        $stars .= '<span class="dashicons dashicons-star-filled"></span>';

    } elseif ( $i < $avrating + 1 && $i > $avrating ) {

        $stars .= '<span class="dashicons dashicons-star-half"></span>';

    } else {

        $stars .= '<span class="dashicons dashicons-star-empty"></span>';

    }

}  

您還可以通過將其作為部件添加來減少大量重復(fù)的樣板 HTML...


$stars = '';

for ( $i = 1; $i <= 5 ; $i ++ ) {

    $stars .= '<span class="dashicons dashicons-star-';

    if ( $i <= $avrating ) {

        $stars .= 'full';

    } elseif ( $i < $avrating + 1 && $i > $avrating ) {

        $stars .= 'half';

    } else {

        $stars .= 'empty';

    }

    $stars .= '"></span>';

}  


查看完整回答
反對(duì) 回復(fù) 2023-08-26
  • 2 回答
  • 0 關(guān)注
  • 187 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)