Php 五彩div

Php 五彩div,php,css,Php,Css,我的网站是一个搜索引擎,它返回许多带有foreach循环的结果,例如: foreach ($xml->channel->item as $result) { $ltitle = $result->title; $ldesc = $result->description; $url = $result->displayUrl; $link = $result->link; if (strlen($ltitle) >

我的网站是一个搜索引擎,它返回许多带有foreach循环的结果,例如:

foreach ($xml->channel->item as $result) {
    $ltitle = $result->title;
    $ldesc = $result->description;
    $url = $result->displayUrl;
    $link = $result->link;

    if (strlen($ltitle) > 60)
    {
$title = substr($ltitle,0,60).'...' ;
    }
    else
    {
    $title = $ltitle;
    }

if (strlen($ldesc) > 195)
    {
$desc = substr($ldesc,0,195).'...' ;
    }
    else
    {
    $desc = $ldesc;
    }


    echo "

<br>


<div class='resultbox'>

<a class='normal' style='text-decoration:none;font-size:huge;font-weight:bold' href='$link'>$title</a><br>
<div style='padding-top:3px;padding-bottom:4px;width:580px;'>
<font style='text-decoration:none;font-size:small;font-family:Arial;'>$desc<br></font></div>
<a style='text-decoration:none;' href='$link'><font style='text-decoration:none;font-size:small;color:green;font-weight:bold;'>$url<br></font></a>

</div>
";
}
左边边框的颜色是我想要更改的,我希望它能从颜色代码列表中随机生成或设置样式,这样结果,除了所有#333可以是#333#555#999等等。。。。。有什么想法吗?

改变

style属性覆盖类中的css。 将
$yourColorInCssFormat
设置为您希望用于div的颜色。例如:
$yourColorInCssFormat='#999'

更改为
,并定义如下颜色

.random-color-1 {
     border-left: 8px solid #555;
}
.random-color-2 {
     border-left: 8px solid #555;
}
.....
.random-color-YOUR_COLOR_LIMIT {
     border-left: 8px solid #555;
}

您可以使用内联样式。或者,您也可以使用css的第n个子项来重复边框颜色方案,如下所示:

.resultbox:nth-child(n+1):hover {

}

.resultbox:nth-child(2n+1):hover {

}

.resultbox:nth-child(3n+1):hover {

}

如果您在使用JS时没有问题,您当然可以这样做:

$(document).ready(function () {

    $('.resultbox').mouseenter(function() {
        var randomColor = Math.floor(Math.random()*16777215).toString(16);
     $('.resultbox').css("border-left", " 8px solid #"+randomColor);    
    });
});

首先,为您的foreachloop尝试以下方法:

<?php foreach ($xml->channel->item as $result): ?>
    <?php 
       $ltitle = $result->title;
       $ldesc = $result->description;
       $url = $result->displayUrl;
       $link = $result->link;

       if (strlen($ltitle) > 60){
           $title = substr($ltitle,0,60).'...' ;
       }else{$title = $ltitle;}

       if (strlen($ldesc) > 195){
           $desc = substr($ldesc,0,195).'...' ;
       }else{$desc = $ldesc;}
    ?>



    <div class='resultbox'>

        <a class='normal' style='text-decoration:none;font-size:huge;font-weight:bold' href='<?php      echo $link ?>'><?php echo $title; ?></a>
    <br>
    <div style='padding-top:3px;padding-bottom:4px;width:580px;'>
    <font style='text-decoration:none;font-size:small;font-family:Arial;'>
        <?php echo $desc; ?><br>
    </font>
    </div>
        <a style='text-decoration:none;' href='<?php echo $link; ?>'><font style='text-  decoration:none;font-size:small;color:green;font-weight:bold;'><?php echo $url; ?><br></font>  </a>

    <?php endforeach; ?>
然后可以在代码中使用变量$color将其中一种颜色随机分配给元素

希望这有帮助


-Gui

你试过什么吗?不知道从哪里开始谷歌赢了,因为它很简单。你介意使用JS吗?@Connor检查我的解决方案。给你无限的选择。这个jquery,我应该包括什么?你只需要包括//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js这很好,但我需要为每个结果设置一个resultbox,这样是否仍然有效,这是否会提高效率,它将为每一个项目执行介于开始foreach和结束foreach之间的所有操作。至于效率,我不能说其中一个是否比另一个更高效,但是这样做会更干净。这里有一个很好的链接可以查看:
<?php foreach ($xml->channel->item as $result): ?>
    <?php 
       $ltitle = $result->title;
       $ldesc = $result->description;
       $url = $result->displayUrl;
       $link = $result->link;

       if (strlen($ltitle) > 60){
           $title = substr($ltitle,0,60).'...' ;
       }else{$title = $ltitle;}

       if (strlen($ldesc) > 195){
           $desc = substr($ldesc,0,195).'...' ;
       }else{$desc = $ldesc;}
    ?>



    <div class='resultbox'>

        <a class='normal' style='text-decoration:none;font-size:huge;font-weight:bold' href='<?php      echo $link ?>'><?php echo $title; ?></a>
    <br>
    <div style='padding-top:3px;padding-bottom:4px;width:580px;'>
    <font style='text-decoration:none;font-size:small;font-family:Arial;'>
        <?php echo $desc; ?><br>
    </font>
    </div>
        <a style='text-decoration:none;' href='<?php echo $link; ?>'><font style='text-  decoration:none;font-size:small;color:green;font-weight:bold;'><?php echo $url; ?><br></font>  </a>

    <?php endforeach; ?>
//Generate a random number between the two parameters
$randomNumber = rand(1, 3);

//Use this number to dictate what the variable color should be
if($randomNumber == 1){$color = "#333"}
elseif($randomNumber == 2){$color = "#555"}
elseif($randomNumber == 3){$color = "#999"}