3

与往常一样,我的 PHP 遇到了麻烦 :( 我对此还是很陌生,我希望它最终会变得更容易......

这让我很头疼,希望有人能帮忙。

我正在尝试从数据库列中提取一串图像。如果图像存在,它们将被显示,否则默认的“无图像”图像以相同的格式显示。

甚至不确定我是否完全正确。数据库列/行中可以有多个值吗?这是一个数组吗?

到目前为止,这是我的代码...

$images_array = "SELECT images FROM properties WHERE property = '".$property."' "; // set images query 
$images = mysql_query($images_array); // run images_array query save as images
$images_count = mysql_num_rows($images);
if ($images_count > 0) {
while ($image = mysql_fetch_array($images)) {
echo'<div id="property_images">
<div id="property_main_image"><img alt="Property Image" src="./images/properties/'.$image['images'].'" /></div>
<div id="property_sub_image"><img alt="Property Image" src="./images/properties/'.$image['images'].'" /></div>
<div id="property_sub_image"><img alt="Property Image" src="./images/properties/'.$image['images'].'" /></div>
</div>';
}
} else {
echo'<div id="property_images">
<div id="property_main_image"><img alt="No Property Image" src="./images/properties/no_image" /></div>
<div id="property_sub_image"><img alt="No Property Image" src="./images/properties/no_image" /></div>
<div id="property_sub_image"><img alt="No Property Image" src="./images/properties/no_image" /></div>
</div>';
}

当图像存在于数据库中时,它会正确显示图像,否则会显示 alt="Property Image"。这导致我认为代码总是将 $images 视为 TRUE ... ??

我可以以这种方式显示多个图像,通过保存多个文件名是相同的 MySQL 值并获取数组/字符串???

在此先感谢您的帮助:)

根据要求我的数据库的结构......

| properties
| ID | address | area | postcode | phone | mobile | tenancy | type | available | deposit | rent | description | bedrooms | bathrooms | communal | kitchens | parking | garden | broadband | property | vacancy | images |
4

2 回答 2

2

$image执行查询时,该变量始终返回 TRUE。

所以我的建议是做到以下几点:

$count = mysql_num_rows($query);
if ($count > 0 ) {
....
}

更新:

$images = mysql_query($images_array); // run images_array query save as images
$images_count = mysql_num_rows($images);

while ($image = mysql_fetch_array($images)) {
    $lettercount = strlen(str_replace(' ', '', $images));
    if($lettercount > 0) {
        /* show the images */
    }else{
        /* show the default images */
    }
}

更新2:

$images_array = "SELECT * FROM properties WHERE property = '".$property."' "; // set images query 
$images = mysql_query($images_array); // run images_array query save as images

while ($row = mysql_fetch_assoc($images)) { 
        $check_string_lenght = strlen(str_replace(' ', '', $row['images']));
    if($check_string_lenght > 0) {
    /* or if($row['images'] != '') { */
       echo'<div id="property_images">
       <div id="property_main_image"><img alt="Property Image" src="./images/properties/'.$row['images'].'" /></div>
       <div id="property_sub_image"><img alt="Property Image" src="./images/properties/'.$row['images'].'" /></div>
       <div id="property_sub_image"><img alt="Property Image" src="./images/properties/'.$row['images'].'" /></div>
       </div>';
} else {
       echo'<div id="property_images">
       <div id="property_main_image"><img alt="No Property Image" src="./images/properties/no_image" /></div>
       <div id="property_sub_image"><img alt="No Property Image" src="./images/properties/no_image" /></div>
       <div id="property_sub_image"><img alt="No Property Image" src="./images/properties/no_image" /></div>
       </div>';
    }

}
于 2013-05-31T13:06:02.130 回答
1
define('NO_IMAGE', '/path/to/no/img'); // path to "noimage" image
$images_array = "SELECT CASE WHEN images IS NULL or images = '' THEN '".NO_IMAGE."' ELSE images END FROM properties WHERE property = '".$property."' ";

如果图像字段值为 null 或空字符串,则图像值 = NO_IMAGE 值,否则为可用值。

于 2013-05-31T15:48:02.943 回答