3

is it possible to do some function inside html content ?

here my code :

$parts = explode(',', 'option 1,option 2,option 3');
echo "<div class='form-group'>
        <div class='col-sm-12'>
          <label>label</label>
          <select class='form-control' style='width: 100%;' name='select' placeholder='select'>
          foreach($parts as $index=>$key) {
              echo '<option>'.$key .'</option>';
          }
          </select>
        </div>
      </div>";
6
  • yes, just part then different. Commented May 20, 2016 at 9:08
  • i got error undefined variable . i think because of my variable still considered as html content. how do i split it ?@FrayneKonok Commented May 20, 2016 at 9:11
  • Take a look at my answer and let me if anything wrong or not. Commented May 20, 2016 at 9:12
  • It actually works the other way around. You are adding HTML to PHP coding. Without naming your file some-title.php no code will be parsed/executed. @FrayneKonok answer is correct but won't work if your file is named some-title.html Commented May 20, 2016 at 9:25
  • @IbnuHabibie, Did you choose the right answer??? If you think that is good for you than its okey. Commented May 20, 2016 at 9:28

2 Answers 2

8

Do this think like this way:

Depart the HTML and PHP, use the PHP tag each time when you need inside a HTML page / PHP page.

<?php
$parts = explode(',', 'option 1,option 2,option 3');
?>
<div class='form-group'>
    <div class='col-sm-12'>
        <label>label</label>
        <select class='form-control' style='width: 100%;' name='select' placeholder='select'>
        <?php
        foreach($parts as $index=>$key){
        ?>
            <option><?php echo $key;?></option>
        <?php }?>
        </select>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

This won't work, you should do like this :

    $parts = explode(',', 'option 1,option 2,option 3');
    echo "<div class='form-group'>
        <div class='col-sm-12'>
        <label>label</label>
        <select class='form-control' style='width: 100%;' name='select' placeholder='select'>";
    foreach($parts as $index=>$key) {
        echo '<option>'.$key .'</option>';
    }
    echo "</select></div></div>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.