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.

1
  • 3
    Don't echo it. Just have it outside of a PHP block (<?php ... ?>). The actual PHP within it is already surrounded by these tags. Commented Oct 20, 2019 at 7:05

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

3 Comments

Hello. Did you read my comment above?
yes but I asked if there is any way to echo that all code.
echo is just sending stuff to the output. By having the HTML directly in the file, it does exactly the same thing (outputs it). There seems to be no point in using echo in your case, you could just paste it somewhere and get the same result.
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>

4 Comments

my all string is already in php i cant use php tag again
what do you mean? @Danial212k
i am echoing this code in loop
don't echo it as string try my solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.