2016-11-21 183 views
1

我在显示默认图像时遇到问题。我不知道该把它放在哪里。当没有图像显示时显示默认图像

$result = $conn->query("SELECT * FROM adoption;"); 

if($result->num_rows !=NULL){ 
    while($rows = $result->fetch_assoc()) { 
     $AAnimalName = $rows['AAnimalName']; 
     $Abreed = $rows['Abreed']; 
     $Asex = $rows['Asex']; 
     $Acolor = $rows['Acolor']; 
     $image = $rows ['image']; 
     ?> 
     <div class="container-custom1"> 
      <?php echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>'?> 
      <?php echo "<i><h1 class='junction'><a style='cursor:pointer' class='junction'>".$AAnimalName."</a></h1></i>"."<br>".$Asex." /".$Abreed."<br>".$Acolor."<br>"?></div> 
     <?php 
    } 
} 

回答

1

尝试这里面while循环:

$image_location = "admin/function/upload/".$image; 
if(file_exists($image_location)) { 
    echo '<img src = "'.$image_location .'" width = "248" height="190" class="age1" title>'; 
} 
else { 
    echo '<img src = "admin/function/upload/default_image.jpg'" width = "248" height="190" class="age1" title>'; 
} 
+0

里面while循环里面吗? – Powerballs

+0

yes''while''loop –

+0

''div class =“container-custom1”><?php' this code –

0

改变这样的行:

<?php echo '<img onerror="this.src=\'img/logo.png\'" src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>'?> 

img/logo.png将是默认图像

+0

好让我试试 – Powerballs

+0

@Powerballs,确保默认路径存在! –

0

试试这个:

If(file_exist('your_file_path')) 
{ 
    echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>'; 
} 
else 
{ 
    echo '<img src = "admin/function/upload/default_image.jpg'" width = "248" height="190" class="age1" title>'; 
} 

这将或者检查文件是否存在没有。如果存在,它会显示给定的文件。否则是一个默认文件。

+0

里面while while循环? – Powerballs

+0

是的,你可以把这段代码放在 –

0
$image = (!empty($rows ['image'])) ? $rows ['image'] : "default.png" ; 
+1

您能否请您在答案中加入解释,以便将来的读者能够理解*它的工作原理? –

0

使用三元操作和设置图像

<?php 

    $image = (!empty($rows ['image'])) ? $rows ['image'] : "default.png" ; 
$image_location = "admin/function/upload/".$image; 
echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>'; 

    ?>