php - 在我用 php 创建的 html 元素中编写 php if 语句
问题描述
我使用 php 根据来自 mongodb 集合的文档创建 html 元素。
$cursor = $collection->find(array('category'=>$ProductType)); //gets my items
//then for each item create an html element
foreach($cursor as $doc){
echo "
<div class= 'product'>
<h2> ".doc["title"]." </h2> //add product name property as element
//here i want to check if my product has a price then display the price
//but i do not know how to use if statement here
</div>
";
}
问题是我不知道如何在echo " ...";
创建 html 元素的地方使用 if 语句。
这就是我想要做的:
if(doc['price']!=0){
echo <h2> ".doc["price"]." </h2>
}
解决方案
您可以随时启动和停止回声,然后重新启动它
$cursor = $collection->find(array('category'=>$ProductType)); //gets my items
//then for each item create an html element
foreach($cursor as $doc){
echo "<div class= "product">
<h2>$doc[title]</h2>";
if($doc['price']!=0){
echo "<h2>$doc[price]</h2>";
}
echo "</div>";
}
推荐阅读
- gatsby - gatsby-browser-entry.js 中的“模块解析失败”错误
- c# - 如何在瓷砖地图碰撞中获取所有碰撞的瓷砖?
- android - 如果内容超出屏幕宽度,如何显示多行文本视图?
- android - 使用 qt creator 构建 apk 的问题
- excel - 返回条件格式单元格内部颜色的函数
- python - 如何创建具有特定 python 版本的 conda 环境?
- javascript - 当我使用 include() 而不是 Ajax 时,为什么我的网格系统不工作?
- javascript - 无法将反应组件导入我的 app.js
- wordpress - 使用 Wordpress Gutenberg 在自定义 React 组件中动态添加(内部)块
- javascript - 在javascript中将多个数组元素组合成单个对象