一如既往,我的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为真...... ??
我可以用这种方式显示多个图像,通过保存多个文件名是相同的MySQL值并获取数组/字符串???
提前感谢您的帮助:)
按要求提供我的数据库结构......
| properties
| ID | address | area | postcode | phone | mobile | tenancy | type | available | deposit | rent | description | bedrooms | bathrooms | communal | kitchens | parking | garden | broadband | property | vacancy | images |
答案 0 :(得分: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>';
}
}
答案 1 :(得分: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."' ";
如果图像字段值为空或空字符串,则图像值= NO_IMAGE值,否则为可用值。