动态调用时,php分别设置图像样式

时间:2012-05-13 20:49:59

标签: php image call styling dynamically-generated

我创建了根据产品搜索返回图片名称和ID的功能。我想显示所有图像,但我希望每个图像都有不同的样式。我想有一种方法可以增加div id,但不确定。这是我的功能和电话:

public function getImages($searchterm=""){
$sql = "SELECT id FROM prod_detail 
WHERE prod_detail.int_prod_code LIKE '$searchterm' ORDER BY `id` ASC";

$stmt = mysqli_query($this->connection, $sql)or die(mysqli_error($this->connection));
while($result = mysqli_fetch_array($stmt)){

$sql2="SELECT image_name FROM images WHERE image_ref={$result['id']}";
$stmt2 = mysqli_query($this->connection, $sql2)or die(mysqli_error($this->connection));
$num_rows = mysqli_num_rows($stmt2);

while($result2 = $stmt2->fetch_assoc()){
foreach ($result2 as $key => $value) {
$returns = $this->dir . $value;
}
}
}
return $returns;
}

这是显示图像的函数调用

foreach($newProd->getAllProductSpec($searchterm) as $item => $val){
echo "<img src='server/php/files/{$val['image_name']}' 
alt='Flying screens' style='float:left;margin:0 30px 20px 0'/>"; ?>  
}

4 个答案:

答案 0 :(得分:0)

$count = 0;
foreach($newProd->getAllProductSpec($searchterm) as $item => $val){
    echo "<img src='server/php/files/{$val['image_name']}' class='img-{$count}' alt='Flying screens' style='float:left;margin:0 30px 20px 0'/>"; ?>  
    $count++;
}

答案 1 :(得分:0)

然后,您可以为每个图像标记添加一个id,并在css文件中为每个id应用一种样式。 (例如迈克尔对代码所做的答案相同)

答案 2 :(得分:0)

将显示图像的功能更改为:

foreach($newProd->getAllProductSpec($searchterm) as $item => $val){
echo "<img src='server/php/files/{$val['image_name']}'
alt='Flying screens' id='img{$item}' style='float:left;margin:0 30px 20px 0'/>"; ?>  
}
这样,每个图像的id都为'img0'img1,img2等。

你可能还想稍微改变你的功能 它所在的部分:

while($result2 = $stmt2->fetch_assoc()){
foreach ($result2 as $key => $value) {
$returns = $this->dir . $value;
}
}
}
return $returns;

错误,因为它不返回数组;

替换为:

$returns = array();    
while($result2 = $stmt2->fetch_assoc()){
foreach ($result2 as $key => $value) {
$returns[] = $this->dir . $value;
}
}
}
return $returns;

答案 3 :(得分:0)

您可以轻松地在显示图像的函数调用中添加某种增量:

$i=1;

foreach($newProd->getAllProductSpec($searchterm) as $item => $val){
  echo "<img src='server/php/files/{$val['image_name']}' 
  alt='Flying screens' id='image-id-$i' style='float:left;margin:0 30px 20px 0'/>"; 

  $i++; // increment this value each time the for each loop completes
}