根据条件通过PHP在页面上动态打印HTML

根据条件通过PHP在页面上动态打印HTML,php,Php,如果数据在JSON文件中可用,我想打印表的一部分。如果数据不存在,它不应该做任何事情 到目前为止,我所做的如下。检查是科目类型是父项,然后生成具有相应数据的表行 <?php $str = file_get_contents('test.json'); $json = json_decode($str, true); $parent= strtr('<tr>

如果数据在JSON文件中可用,我想打印表的一部分。如果数据不存在,它不应该做任何事情

到目前为止,我所做的如下。检查是
科目类型
父项
,然后生成具有相应数据的表行

         <?php
               $str = file_get_contents('test.json');

               $json = json_decode($str, true);
               $parent= strtr('<tr>
                 <td colspan="2" style="color:#263a80;font-size:19px;padding-bottom:20px;padding-top:20px">Parent Information</td>
              </tr>
              <tr>
                 <td style="width:30%;font-weight:700;padding-bottom:10px">First Name</td>
                 <td>@fname</td>
              </tr>
              <tr>
                 <td style="width:30%;font-weight:700;padding-bottom:10px">Last Name</td>
                 <td>@lname</td>
              </tr>
              <tr>
                 <td style="width:30%;font-weight:700;padding-bottom:10px">Mobile Number</td>
                 <td>@mobile</td>
              </tr>
              <tr>
                 <td style="width:30%;font-weight:700;padding-bottom:10px">Email</td>
                 <td><a @email target="_blank" rel="noreferrer">@email</a></td>
              </tr>
              <tr>
                 <td style="width:30%;font-weight:700;padding-bottom:10px">Country</td>
                 <td>@country</td>
              </tr>
              <tr>
                 <td style="width:30%;font-weight:700;padding-bottom:10px">State</td>
                 <td>@state</td>
              </tr>
              <tr>
                 <td style="width:30%;font-weight:700;padding-bottom:10px">City</td>
                 <td>@city</td>
              </tr>
              <tr>
                 <td style="width:30%;font-weight:700;padding-bottom:10px">Postal Code</td>
                 <td>@pcode</td>
              </tr>
              <tr>
                 <td style="width:30%;font-weight:700;padding-bottom:10px">Full Home Address</td>
                 <td>@address</td>
              </tr>', ["@fname"=>$json["json"]["register_session_1"]["first_name"],
                       "@lname"=>$json["json"]["register_session_1"]["last_name"],
                       "@mobile"=>$json["json"]["register_session_1"]["mobile"],
                       "@email"=>$json["json"]["register_session_1"]["email"],
                       "@country"=>$json["json"]["register_session_1"]["country"],
                       "@state"=>$json["json"]["register_session_1"]["state_online"],
                       "@city"=>$json["json"]["register_session_1"]["city"],
                       "@pcode"=>$json["json"]["register_session_1"]["postal_code"],
                       "@address"=>$json["json"]["register_session_1"]["address"]
                       ]);

              if ($json["json"]["register_session_1"]["account_type"]==="parent"){
                 echo $parent;                  
              }
              else {
                 echo "hahahah";
              }
              ?>

问题是它仍然打印表数据。我不知道我做错了什么。if-else条件不工作


我有这个密码,但我似乎不明白为什么。我已在
标记之间添加了此代码。

打印您的
$json[“json”][“register\u session\u 1”][“account\u type”]
,并检查它是否等于
父项。然后尝试删除
===
相同的运算符并使用
=
您能为我提供一个json文件工作的示例吗。当我在您的示例json中将account_type更改为“parent”并运行时,它会回显无效的html(根据前面的注释),如果我将其设置为“parent”,它会嘲笑我。您的代码应该可以工作,我重新创建了它,它在我这方面也可以工作。对我来说也一样