English 中文(简体)
随机颜色和 div 宽度不工作
原标题:Random color and div width not working
  • 时间:2012-05-22 15:52:35
  •  标签:
  • php

这是我的代码:

<?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

Is it easier to display fixed (different) colors for each div? Thank you

最佳回答

更改 :

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>";
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签