php-如果存在则显示图像字符串,如果字段为空则显示默认图像
作者:互联网
像往常一样,我在使用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 =“属性图像”.这使我认为代码始终将$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 |
解决方法:
执行查询时,$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>';
}
}
标签:if-statement,select,mysql,php 来源: https://codeday.me/bug/20191123/2065974.html