0

This is my code:

<div class="col-xs-6">
  <div class="form-group">
    <label><i class="fa fa-asterisk text-danger"></i> Country</label>
    <select class="form-control select2" id="countryId" name="countryId">
    <option value="0">Select Country</option>
      <?php
        if(!empty($country))
        {
          foreach ($country as $record)
          {
              ?>
              <option value="<?php echo $record->id ?>"><?php echo $record->countryName ?></option>
              <?php
          }
        }
      ?>
    </select>
  </div>
</div>

This is my trying (Not successful):

echo '
<div class="col-xs-6">
  <div class="form-group">
    <label>'.(($required<>'0')?'<i class="fa fa-asterisk text-danger"></i>':"").' '.$friendlyName.'</label>
    <select '.(($readonly<>'0')?'readonly':"").' '.(($disabled<>'0')?'disabled':"").' class="form-control select2 '.$columnClass.'" id="'.$columnId.'" name="'.$columnName.'">
      <option value="0">Select Country</option>'.
        if(!empty($country))
        {
          foreach ($country as $record)
          {
              .'<option value="$record->id">$record->countryName</option>'.
          }
        }
    .'</select>
  </div>
</div>
';

When i run echo code it gives me this error: Parse error: syntax error, unexpected 'if' (T_IF)

How do I echo this block of html and php including foreach loop.

Danial212k
  • 130
  • 1
  • 10
  • 3
    Don't `echo` it. Just have it outside of a PHP block (``). The actual PHP within it is already surrounded by these tags. – Jeto Oct 20 '19 at 07:05

2 Answers2

0

I tried other way:

$html = '
<div class="col-xs-6">
  <div class="form-group">
  <label>'.(($required<>'0')?'<i class="fa fa-asterisk text-danger"></i>':"").' '.$friendlyName.'</label>
  <select '.(($readonly<>'0')?'readonly':"").' '.(($disabled<>'0')?'disabled':"").' class="form-control select2 '.$columnClass.'" id="'.$columnId.'" name="'.$columnName.'">
    <option value="0">Select Country</option>';
      if(!empty($country))
      {
      foreach ($country as $record)
        {
          $html .= '<option value="'.$record->id.'">'.$record->countryName.'</option>';
        }
      }
$html .= '</select>
    </div>
  </div>
';
echo $html;
Danial212k
  • 130
  • 1
  • 10
0

Instead of echoing as string loop each item by using <?php foreach(): ?>

Try this:

<div class="col-xs-6">
  <div class="form-group">
    <label><i class="fa fa-asterisk text-danger"></i> Country</label>
    <select class="form-control select2" id="countryId" name="countryId">
    <option value="0">Select Country</option>
      <?php if(!empty($country): ?>
          <?php foreach ($country as $record): ?>

            <option value="<?php echo $record->id ?>">
               <?php echo $record->countryName ?>
            </option>

          <?php endforeach; ?>
    <?php endif; ?>
   </select>
  </div>
</div>
Franz
  • 354
  • 1
  • 4
  • 15