2013-05-06 39 views
2

我的网站是搜索引擎返回很多很多的结果与foreach循环这样:五彩的div

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> 
"; 
} 

和上面这个

.resultbox 
{ 
height:auto; 
width:600px; 
background-color:transparent; 
font-size:19px; 
padding:10px; 
padding-left: 30px; 
padding-right: 30px; 
border-left: 6px solid #333; 
} 
.resultbox:hover 
{ 
border-left: 8px solid #555; 
} 

的风格所有结果的resultbox类边界左边的颜色是我想要改变的,我希望它能够生成颜色代码或从颜色代码列表中随机选择样式,所以#333的结果可以是#333#555#999等等。 ... 有任何想法吗?

+0

您尝试过什么吗? – Kasyx 2013-05-06 10:20:18

+0

不知道从哪里开始 – Connor 2013-05-06 10:22:55

+0

谷歌赢了,因为它很简单。 – 2013-05-06 10:23:42

回答

2

变化<div class='resultbox'><div class='resultbox random-color-".rand(1,YOUR_COLOR_LIMIT)."'>并定义颜色,如

.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; 
} 
+0

你犯了一个拼写错误,但否则它的工作,谢谢 – Connor 2013-05-06 10:36:40

1

变化

<div class='resultbox'>

<div class='resultbox' style='border-left-color:$yourColorInCssFormat;'>

样式属性覆盖从类的CSS。 将$yourColorInCssFormat设置为您希望用于div的颜色。例如:$yourColorInCssFormat = '#999';

1

您可以使用内嵌样式为。或者你也可以将用户第n个孩子选择CSS的重复边框颜色方案是这样的:

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

} 

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

} 

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

} 
3

如果u有使用JS没有问题,你当然可以这样做:

$(document).ready(function() { 

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

是这个jquery,我包括什么? – Connor 2013-05-06 10:41:54

+0

您只需包含//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min。js – 2013-05-06 10:45:06

1

首先,尝试了这一点你的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; ?> 

你不是大玩回声这样。

现在为了生成随机颜色,你可以使用php rand();

例如:

//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"} 

然后,您可以在代码中使用的变量$颜色随机指定的颜色之一元素。

希望这会有所帮助!

-Gui

+0

多数民众赞成在很好,但我需要每个结果的结果,这仍然有效,也是这使得它更有效 – Connor 2013-05-06 10:43:39

+0

是的,它会做每件事物的开瓶器foreach和endforeach之间的一切。至于效率,我不能说一个人是否比另一个人更有效率,但是这种方式更加清洁。 – Gondon 2013-05-06 11:32:04

+0

继承人一个很好的链接检查出来:http://php.net/manual/en/control-structures.alternative-syntax.php – Gondon 2013-05-06 11:36:30