html代码嵌入在php脚本中

时间:2018-03-30 05:36:39

标签: php html

我想要一个带有php条件的表td中的按钮

这是我的PHP代码。

  <?php

<table  border="0">
<thead>
      <tr>
        <th>Order No</th>
        <th>Order Date </th>
        <th>Customer</th>
        <th>Total Price</th>
        <th>Action</th>
      </tr>

    </thead>
    <tbody>
  ';
 foreach($query as $row)

 {
 $output .= '<tr class="heightSetting">
            <td>'.$row->purchase_order_no.'</td>
            <td>'.date("Y:m:d", strtotime($row->updatiion_date)).'</td>
            <td>'.$row->vendor_name.'</td>
            <td>'.$row->total_price.'</td>
            <td><input type="button" class="button modalLink btn" 
            view_id="'.$row->purchase_order_id.'" value="View"
            data-toggle="modal" data-target="#viewModal">
            <input type="button" class="deliver_status button btn" 
            id="'.$row->purchase_order_id.'" value="Delivered">
            </td>
         </tr>

         ';
 }
  $output .= '</table></tbody><br />';
echo $output;     

}
    }
 ?>

我想在html代码中使用这个条件。我很困惑如何在HTML代码中写它请帮助我谢谢你

if($row->delivery == 0){<input type="button" class="deliver_status button 
 btn" id="'.$row->purchase_order_id.'" value="Delivered">}

这里是图像,显示我如何在html代码code image

中嵌入php脚本

2 个答案:

答案 0 :(得分:2)

您可以使用简写if / else语句

$output .= '<tr class="heightSetting">
        <td>'.$row->purchase_order_no.'</td>
        <td>'.date("Y:m:d", strtotime($row->updatiion_date)).'</td>
        <td>'.$row->vendor_name.'</td>
        <td>'.$row->total_price.'</td>
        <td><input type="button" class="button modalLink btn" 
        view_id="'.$row->purchase_order_id.'" value="View"
        data-toggle="modal" data-target="#viewModal">
        '.($row->delivery == 0?'<input type="button" class="deliver_status button btn" 
        id="'.$row->purchase_order_id.'" value="Delivered">':'').'
        </td>
     </tr>

     ';

提取:

($row->delivery == 0?'<input type="button" class="deliver_status button btn" 
        id="'.$row->purchase_order_id.'" value="Delivered">':'')

答案 1 :(得分:1)

这里我更改了原始代码如下。这里还添加了所有条件。我认为这会对你有所帮助。将此代码段添加到.php代码的相关位置并尝试一下。

<?php if(!empty($query)): ?>
  <table  border="0">
    <thead>
      <tr>
        <th>Order No</th>
        <th>Order Date </th>
        <th>Customer</th>
        <th>Total Price</th>
        <th>Action</th>
      </tr>

    </thead>
    <tbody>
      <?php  foreach($query as $row): ?>
        <tr class="heightSetting">
          <td><?php echo $row->purchase_order_no; ?></td>
          <td><?php echo date("Y:m:d", strtotime($row->updatiion_date)); ?></td>
          <td><?php echo $row->vendor_name;?></td>
          <td><?php echo $row->total_price;?></td>
          <td>
            <?php if($row->delivery != 0):?>
                <input type="button" class="button modalLink btn" 
                      view_id="'<?php echo $row->purchase_order_id;?>'" value="View"
                      data-toggle="modal" data-target="#viewModal">
            <?else :?>
              <input type="button" class="deliver_status button btn" 
                 id="'<?php echo $row->purchase_order_id;?>'" value="Delivered">

            <?php endif;?>

          </td>

        </tr>

      <?php endforeach; ?>
    </tbody>
  </table>
<?php else: ?>               
    <div>
        <h3 class="errorShow">Data Not Found!</h3>
    </div>
<?php endif; ?>