根据MySQL输出分配div颜色

时间:2012-04-11 20:41:13

标签: php mysql

不知道怎么说标题对不起。基本上我有一个评论系统,它非常简单,从数据库中选择帖子的uid,然后只从另一个数据库输出与该UID绑定的注释。

评论系统的本质是每张海报都是匿名的,因此,很难跟踪你是否与同一个人进行交流,因此,我想把评论包裹起来是一种特定的颜色,并且那个人在该帖子上的每个评论都是那种颜色。将用户评论联系在一起的唯一因素是IP地址。

到目前为止我的代码:

$sql = "SELECT * FROM anonpost_com WHERE uid = '$uid' ORDER BY date DESC";


$result = mysql_query($sql) or print ("Can't select entry from table anonpost.<br />" . $sql . "<br />" . mysql_error());

while($row = mysql_fetch_array($result)) {
    $date = date("l F d Y", strtotime($row['date']));
    $comment = stripslashes($row['comment']);
    $uid = ($row['uid']);
    $cid = ($row['cid']);
    $ip = ($row['ip']);


         ?>



<div id="comments" style="border:1px solid <?php echo $colour; ?>;">
<p><?php echo $comment; ?></p>
<h4>by <i>Anonymous</i> on <?php echo $date; ?></h4>
</div>
<?php
}
?>

$ color来自:     $ color = dechex(rand(0,10000000); 但是我不确定如何在评论中为同一个IP的每个实例制作相同的颜色...

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

我同意IP地址可能不是最佳解决方案,但要做到这一点:

$sql = "SELECT * FROM anonpost_com WHERE uid = '$uid' ORDER BY date DESC";

$colours = array();
$result = mysql_query($sql) or print ("Can't select entry from table anonpost.<br />" . $sql . "<br />" . mysql_error());

while($row = mysql_fetch_array($result)) {
    $date = date("l F d Y", strtotime($row['date']));
    $comment = stripslashes($row['comment']);
    $uid = ($row['uid']);
    $cid = ($row['cid']);
    $ip = ($row['ip']);
    if (!isset($colours[$ip])) {
        $colours[$ip] = dechex(rand(0,10000000);
    }
    $colour = $colours[$ip];


         ?>



    <div id="comments" style="border:1px solid <?php echo $colour; ?>;">
        <p><?php echo $comment; ?></p>
        <h4>by <i>Anonymous</i> on <?php echo $date; ?></h4>
    </div>
<?php
}
?>

请注意,每次加载页面时颜色都会发生变化。