2012-05-22 33 views
1

這裏是我的代碼:隨機顏色和DIV寬度不工作

<?php 
    // Returns a random RGB color (used to color the vote bars) 
    function getRandomColor2() 
    { 
     $r2 = rand(128,255); 
     $g2 = rand(128,255); 
     $b2 = rand(128,255); 
     $color2 = dechex($r2) . dechex($g2) . dechex($b2); 
     echo "$color2"; 
    } 

    echo "<table id=\"tblResults2\" align=\"center\">"; 

    // Get max vote count 
    $doc = new DOMDocument(); 
    $doc->load("../vote_dir/xml/results.xml"); 
    $maxvotes2 = 0; 
    $pollitems2 = $doc->getElementsByTagName("pollitem"); 
    foreach($pollitems2 as $pollitem2) 
    { 
    $votes2 = $pollitem2->getElementsByTagName("votes"); 
    $vote2 = $votes2->item(0)->nodeValue; 
    $maxvotes2 = $maxvotes2 + $vote2; 
    } 

    // Generate the results table 
    $doc = new DOMDocument(); 
    $doc->load("../vote_dir/xml/results.xml"); 
    $pollitems2 = $doc->getElementsByTagName("pollitem"); 
    foreach($pollitems2 as $pollitem2) 
    { 
    $entries2 = $pollitem2->getElementsByTagName("entryname"); 
    $entry2 = $entries2->item(0)->nodeValue; 
    $votes2 = $pollitem2->getElementsByTagName("votes"); 
    $vote2 = $votes2->item(0)->nodeValue; 
    $tempWidth2 = $vote2/$maxvotes2; 
    $tempWidth2 = 300 * $tempWidth2; 
    $votepct2 = round(($vote2/$maxvotes2) * 100); 
    echo "<tr><td width=\"45%\" class=\"polls\">$entry2</td>"; 
    echo "<td width=\"35%\" class=\"resultbar\"><div class=\"bar\" style=\"background-color: "; 
     getRandomColor2(); 
     echo "; width: $tempWidth2 px;\">$votepct2%</div></td><td class=\"each_vote\" width=\"20%\">($vote2 votes)</td></tr>"; 
    } 
    echo "<tr><td width=\"45%\" class=\"total\" colspan=\"3\">Σύνολο ψήφων: $maxvotes2</td>"; 
    echo "</table>"; 
?> 

我的問題是,當我把它,一切正常但是:

  1. 我沒有得到任何顏色

  2. 顯示%的所有DIV都是相同的寬度而不是$ tempWidth2

爲每個div顯示固定(不同)顏色更容易嗎? 謝謝

+0

您應該檢查發送到瀏覽器的原始HTML。這是你期望的嗎? –

+0

@Oli我很抱歉,你能否進一步解釋?謝謝 – Pavlos1316

+0

您的服務器正在生成(X)HTML並將其發送到瀏覽器;您應該檢查(X)HTML並查找問題。 –

回答

2

變化:

echo "$color2"; 

到:

echo "#$color2"; // added #, fixes colors 

...和變化:

echo "; width: $tempWidth2 px;\">$votepct2%</div></td><td class=\"each_vote\" width=\"20%\">($vote2 votes)</td></tr>"; 

...到:

echo "; width: {$tempWidth2}px;\">$votepct2%</div></td><td class=\"each_vote\" width=\"20%\">($vote2 votes)</td></tr>"; 
// removed a space, wrapped var in {}, fixes widths 

...儘管你會做在可讀性方面要好得多,從你的函數返回一個字符串,像這樣:

function getRandomColor2() 
{ 
    $r2 = rand(128,255); 
    $g2 = rand(128,255); 
    $b2 = rand(128,255); 
    $color2 = dechex($r2) . dechex($g2) . dechex($b2); 
    return "#".$color2; 
} 

// ... 

echo "<tr>" 
    . "<td width=\"45%\" class=\"polls\">{$entry2}</td>" 
    . "<td width=\"35%\" class=\"resultbar\">" 
    . "<div class=\"bar\" style=\"background-color: ".getRandomColor2()."; width: {$tempWidth2}px;\">{$votepct2}%</div>" 
    . "</td>" 
    . "<td class=\"each_vote\" width=\"20%\">({$vote2} votes)</td>" 
    . "</tr>"; 
+0

非常感謝。 – Pavlos1316

+0

好的,我也採用了這種形式。再次感謝你。 – Pavlos1316

+0

不用擔心... :-) – DaveRandom