我需要一个帮助。我正在显示一些来自数据库的数据,但我需要在每个循环迭代中使用PHP动态设置类名。我在下面解释我的代码。
<?php
$class=array('classname'=>'pic1-caption bottom-to-top','classname'=>'pic1-caption top-to-bottom','classname'=>'pic1-caption left-to-right','classname'=>'pic1-caption right-to-left','classname'=>'pic1-caption rotate-in','classname'=>'pic1-caption rotate-out','classname'=>'pic1-caption open-up','classname'=>'pic1-caption open-down','classname'=>'pic1-caption open-left','classname'=>'pic1-caption open-right','classname'=>'pic1-caption come-left','classname'=>'pic1-caption come-right');
$sql=mysql_query("select * from phr_health_care where status=1 order by health_id desc");
$key=0;
while($row=mysql_fetch_array($sql)){
?>
<div class="pic1">
<img src="backend/uploads/<?php echo $row['image'] ?>" class="pic1-image" alt="pic1"/>
<span class="pic1-caption bottom-to-top"> //here need to set class name dynamically.
<h1 class="pic1-title">DIABETICS</h1>
<p class="newp"><?php echo $row['title'] ?></p>
<a href="health.html" class="detall">Go for Details</a>
</span>
此处所有数据都来自数据库,我需要动态设置span element
中的类名,这些类名在$class
变量中声明。我也需要在$class
变量内声明所有类将在循环继续时完成,它将从开始开始直到db值获取尚未完成。请帮助我。
答案 0 :(得分:2)
如果我理解你的问题,这就是你想要做的事情:
<?php
$class=array('A','B','C');
$length = count($class);
$key=0;
while(...) {
$index = $key % $length;
$current_class= $class[$index];
$key += 1;
}
当$ key增长时,$ current_class将循环每个类值。
$ key将采用:0,1,2,3,4,5,6等
但$ index将采用:0,1,2,0,1,2等
所以$ class [$ index]将是:'A','B','C','A','B','C'等。
(参见http://php.net/manual/en/language.operators.arithmetic.php处的“模数”运算符)
答案 1 :(得分:2)
是的,您可以将数组四舍五入以动态使用。这是这类事情的例子。
修改后的脚本:
修改数组:
$class = array('pic1-caption bottom-to-top',
'pic1-caption top-to-bottom',
'pic1-caption left-to-right',
'pic1-caption right-to-left',
'pic1-caption rotate-in',
'pic1-caption rotate-out',
'pic1-caption open-up',
'pic1-caption open-down',
'pic1-caption open-left',
'pic1-caption open-right',
'pic1-caption come-left',
'pic1-caption come-right');
SQL:
$sql=mysql_query("select * from phr_health_care where status=1 order by health_id desc");
其余代码:
$len = count($class);
$key = 0;
while($row=mysql_fetch_array($sql)){
?>
<div class="pic1">
<img src="backend/uploads/<?php echo $row['image'] ?>" class="pic1-image" alt="picture"/>
<span class="<?php echo $class[$len%$key];?>">
<h1 class="pic1-title">DIABETICS</h1>
<p class="newp"><?php echo $row['title'] ?></p>
<a href="health.html" class="detall">Go for Details</a>
</span>
</div>
<?php
$key = ($key == ($len-1)) ? 0 : ($key + 1);
}?>
答案 2 :(得分:1)
您需要对代码进行一些更改,这些更改由评论本身说明:
您需要将$class
数组转换为索引数组而不是关联数组,因为所有键都相同且错误。
<?php
$class=array('pic1-caption bottom-to-top','pic1-caption top-to-bottom','pic1-caption left-to-right','pic1-caption right-to-left','pic1-caption rotate-in','pic1-caption rotate-out','pic1-caption open-up','pic1-caption open-down','pic1-caption open-left','pic1-caption open-right','pic1-caption come-left','pic1-caption come-right'); //I make it an indexed array because associative array with same key is wrong and give you error
$sql=mysql_query("select * from phr_health_care where status=1 order by health_id desc");
$key=0;// start counter
$class_count = count($class); // take count of class array
while($row=mysql_fetch_array($sql)){
?>
<div class="pic1">
<img src="backend/uploads/<?php echo $row['image'] ?>" class="pic1-image" alt="pic1"/>
<span class="<?php echo $class[$key];?>"><!-- get classes dynamically-->
<h1 class="pic1-title">DIABETICS</h1>
<p class="newp"><?php echo $row['title'] ?></p>
<a href="health.html" class="detall">Go for Details</a>
</span>
<?php
if($key == $class_count-1){ // check when classes array values ends start count from 0 again
$key = 0;
}else{
$key++;
}
}?>
注意: - 由于mysql_*
已弃用,现在请使用mysqli_*
或PDO
。感谢强>