0

我创建了基于产品搜索返回图像名称和 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

4 回答 4

0

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

于 2012-05-13T20:59:33.690 回答
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'/>"; ?>  
}

这样,每个图像都会有一个 'img0' img1、img2 等的 id。

你可能还想稍微改变你的功能:

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;
于 2012-05-13T20:59:44.307 回答
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
}
于 2012-05-13T21:03:32.387 回答
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++;
}
于 2012-05-13T20:58:25.697 回答