Php 如果类别行为空,如何显示默认图像
若用户并没有图片,并且我们的数据库中的类别为空,那个么该行将显示默认照片。我还需要在每个类别4图像。。 我该怎么做请帮我谢谢Php 如果类别行为空,如何显示默认图像,php,Php,若用户并没有图片,并且我们的数据库中的类别为空,那个么该行将显示默认照片。我还需要在每个类别4图像。。 我该怎么做请帮我谢谢 // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->conne
// Create connection
$conn = new mysqli($servername, $username,
$password, $dbname); // Check connection if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); }
$sql_pro = "SELECT imgname from providers where categories = 'Diet and Fitness' ";
$run=mysqli_query($conn,$sql_pro);//here run the sql query.
while($row=mysqli_fetch_array($run)){//while look to fetch the result and store in a array $row.
$imgname = $row[0];
$categories="Diet and Fitness";
if($categories=='null')
{
echo "<img src='images/default.png' width='139px' height='100px'>";
}
else
{
echo " <img src='uploads/providers/$imgname' width='139px' height='100px'>";
}
$conn->close(); } ?>
//创建连接
$conn=newmysqli($servername,$username,
$password,$dbname);//如果($conn->connect\U错误),请检查连接{
die(“连接失败:“.$conn->connect_error);}
$sql_pro=“从类别为‘饮食和健身’的提供者中选择imgname”;
$run=mysqli\u查询($conn$sql\u pro)//在这里运行sql查询。
while($row=mysqli_fetch_array($run)){//while查找以获取结果并存储在数组$row中。
$imgname=$row[0];
$categories=“饮食和健身”;
如果($categories=='null')
{
回声“;
}
其他的
{
回声“;
}
$conn->close();}?>
有两件事:
- 首先,您需要使类别名称动态化,因为它应该通过用户提交的表单来自
请求数据
- 其次,您需要更改
条件以检查if
变量是否有任何值,这将决定是否显示来自DB的实际图像或默认图像$imagename
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Assuming Categoty name provided from request-data as user is
// submitting a form and it is coming in "category_name" key.
// If it is not there in request-data then set it to
// default value as "Diet and Fitness"
$categories = (isset($_REQUEST['category_name']) && !empty($_REQUEST['category_name'])) ? $_REQUEST['category_name'] : 'Diet and Fitness';
// Use "$categories" set above instead of hardcoded category name
$sql_pro = "SELECT imgname from providers where categories = '" . $categories . "'";
$run = mysqli_query($conn,$sql_pro); //here run the sql query.
// Just a guess if mysqli_num_rows() will provide us total number
// of rows in resultset. Please correct this function if not
// returning total row count.
if(mysqli_num_rows($run) > 0) {
while($row = mysqli_fetch_array($run)){
//while look to fetch the result and store in a array $row.
$imgname = $row[0];
// This is not required now as we have category name
// in "$categories" variable.
//$categories="Diet and Fitness";
// Check if "imagename" is empty in database then use a
// default image otherwise use what is coming from DB.
if($imgname == null OR $imgname == '') {
echo "<img src='images/default.png' width='139px' height='100px'>";
} else {
echo " <img src='uploads/providers/$imgname' width='139px' height='100px'>";
}
}
} else {
echo "<img src='images/default.png' width='139px' height='100px'>";
}
$conn->close();
?>
如果静态分配类别,该类别将如何为空我有不同的类别,我想在每个类别中显示4个图像,但如果数据库中没有图像,且类别为空,则将显示默认图像,如如果($categories='null'| |$imgname==''| |$imgname==null)
@VishnuBhadoriya我现在应该做什么尝试@Sintostill给出的代码不显示默认图像它只显示数据库中存在的图像,然后将整个,同时将循环放入if
检查数据库中是否有任何行else
显示默认图像。代码已修改。