在我用php创建的html元素中编写php if语句

在我用php创建的html元素中编写php if语句,php,html,Php,Html,我使用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'>

我使用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>
      ";
}

    

您可以随时启动和停止回显,然后重新启动它

$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>";
}
$cursor=$collection->find(数组('category'=>$ProductType))//获取我的项目
//然后为每个项目创建一个html元素
foreach($游标为$doc){
回声“
$doc[标题]”;
如果($doc['price']!=0){
回显“$doc[价格]”;
}
回声“;
}

你没有。结束你的
echo
,添加你的
如果
,恢复你的
echo
那是无效的代码,顺便说一句,
echo
转义你的引号。最有可能的是
$doc[“title”]
带有
$
$doc[“title”
,可能还想修复
中的引号,将
doc[title]
更改为
{$doc['title']
$doc[price]
{$doc['price']}
$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>";
}