简体   繁体   中英

html code embeded in php script

I want a button in table td with php condition

Here is my php code.

  <?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;     

}
    }
 ?>

I want this condition in html code. I am confusing how write it in html code Please help me Thanks in advnce

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

here is image to show how I embeded php script in html code 代码图片

You can use shorthand if/else statement

$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>

     ';

Extracted:

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

Here I have changed the original code as follows. Here all the conditions also are added. and I think this will help you. Add this code snippet in the relevant place of your .php code and give it a try.

<?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; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM