php - 如果 [在 php 中] 为空,如何从检索中跳过图像字段?
问题描述
我正在制作博客系统,其中包含:文本和图像,如果用户不插入图像,它应该跳过该图像字段来检索,而它在每个帖子中显示空图像字段。这段代码有什么问题?或者有什么可能的方法,if/else 条件语句在 echo 中?
$get_posts="SELECT * FROM posts";
$run_posts=mysqli_query($con,$get_posts);
while($row_post=mysqli_fetch_array($run_posts)){
$post_id=$row_post['post_id'];
$user_id=$row_post['user_id'];
$post_img=$row_post['post_image'];
$post_cnt=$row_post['post_cnt'];
$post_date=$row_post['post_date'];
echo"<div class='posts'>
<p>$post_date</p>
<p><img src='img/$post_img'width='380'height='auto'/></p>
<p>$post_cnt</p>
<a href='single.php?post_id=$post_id'style='float:right;'>
<button>See Replies or Reply to This</button></a>
</div><br/>";
}
解决方案
是的,您必须使用 if else 条件:
$get_posts="SELECT * FROM posts";
$run_posts=mysqli_query($con,$get_posts);
while($row_post=mysqli_fetch_array($run_posts)){
$post_id=$row_post['post_id'];
$user_id=$row_post['user_id'];
$post_img=$row_post['post_image'];
$post_cnt=$row_post['post_cnt'];
$post_date=$row_post['post_date'];
$output = '<div class="posts"><p>'.$post_date.'</p>';
if($post_img != ''){
$output .= '<p><img src="img/'.$post_img.'" width="380" height="auto"/></p>';
}
$output .= '<p>'.$post_date.'</p><p>'.$post_cnt.'</p>
<a href="single.php?post_id='.$post_id.' "style="float:right;">
<button>See Replies or Reply to This</button></a>
</div><br/>';
echo $output;
}
推荐阅读
- c# - 如何使用 Stacks 处理递归函数中的无限 for 循环?
- java - 使recyclerview和cardview中的按钮可点击
- javascript - Uncaught TypeError: o.widget is not a function at mouse.min.js Wordpress
- php - 将mysql查询更改为laravel查询
- django - 如何验证 django URLfield 是否来自特定域或主机名?
- python - 无法解压缩具有密码保护的文件
- reactjs - 如何在打字稿中获取传递的通用反应组件的道具类型
- image-processing - ImageMagick 中 convert -unsharp 和 convert -adaptive-sharpen 有什么区别
- angularjs - Angular 9 - Webpack Chrome DevTools 如何决定观看哪些文件夹?
- java - 制作 qustom EnumChatFormatting 颜色