Nirajan Pokharel Nirajan Pokharel - 2 months ago 7
HTML Question

Echo HTML with PHP inside

Please help me turn this into dynamic using echo with HTML and PHP using array range

<?php
$arr = array(1, 2, 3, 4);
$selected='<img src="assets/images/resev.png" class="img-circle" width="40" height="40" />';
$aval='<img src="assets/images/aval.png" class="img-circle" width="40" height="40" />';
foreach ($arr as &$value) {
<button class="btn btn-default" id=$value <?php if (isset($_SESSION['buttons']) && (in_array($value, $_SESSION['buttons']))){ ?> disabled <?php } ?> >
<?php if (isset($_SESSION['buttons']) && (in_array($value, $_SESSION['buttons']))) {
echo $selected ;
}
else {
echo $aval ;
}
?>$value
</button>


so far I have done this

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
echo '<button class="btn btn-default" id='. $value if (isset($_SESSION['buttons']) && (in_array($value, $_SESSION['buttons']))){ disabled }. '> //line 84
</button>' ;
}
?>


and I'm already getting


Parse error: parse error in xyz.php on line 84

Answer

You can do in following way -

<?php
  $arr = array(1, 2, 3, 4);
  foreach ($arr as &$value) {
    $disabled = '';
    if(isset($_SESSION['buttons']) && (in_array($value, $_SESSION['buttons']))) {
        $disabled = 'disabled';
    }  
    echo '<button class="btn btn-default" id='.$value.' '.$disabled.'>Test Button</button>' ;
  }
?>