El color aleatorio y el ancho div no funcionan
Frecuentes
Visto 265 veces
1
Aquí está mi código:
<?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>";
?>
Mi problema es que cuando lo llamo, todo funciona PERO:
no tengo color
Todos mis DIV que muestran el % tienen el mismo ancho en lugar de $tempWidth2
¿Es más fácil mostrar colores fijos (diferentes) para cada div? Gracias
1 Respuestas
2
Cambiar:
echo "$color2";
a:
echo "#$color2"; // added #, fixes colors
...y cambio:
echo "; width: $tempWidth2 px;\">$votepct2%</div></td><td class=\"each_vote\" width=\"20%\">($vote2 votes)</td></tr>";
...a:
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
... aunque lo haría mucho mejor en términos de legibilidad para devolver una cadena de su función, como esta:
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>";
contestado el 22 de mayo de 12 a las 17:05
Ok... adopté esa forma también. Gracias de nuevo. - Pavlos1316
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas php or haz tu propia pregunta.
Debe verificar el HTML sin formato enviado al navegador. ¿Es lo que esperas? - Oliver Charlesworth
@Oli, lo siento, pero ¿puedes explicarlo más? Gracias - Pavlos1316
Su servidor está generando (X)HTML y enviándolo al navegador; debe verificar el (X) HTML y buscar el problema. - Oliver Charlesworth
Use el
view soure
opción de su navegador para ver lo que está jodido. - core1024@core1024 Votaría a favor de tu comentario, pero no quiero arruinar la yuxtaposición de la respuesta de Oli y la tuya. - octern