Skip to content Skip to sidebar Skip to footer

Echo Html With Php Inside

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

Solution 1:

To avoid such problems and errors in future you must separate your code for blocks:

  1. Define values
  2. Calculate dynamic values
  3. Include template
  4. Evaluate template

Like this:

index.php contents

<?php// define$a = 1;
$b = 2;
$c = null; // value for fallback// calculate$c = $a + $b;

// include templateinclude_once'template.phtml';

template.phtml contents (evaluate template)

<div><p><?=$a?></p><p><?=$b?></p><?phpif ($c !== null) { ?><p><?=$c?></p><?php } else { ?><p>$c variable has not calculated</p><?php } ?></div>

Solution 2:

You can do in following way -

<?php$arr = array(1, 2, 3, 4);
  foreach ($arras &$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>' ;
  }
?>

Solution 3:

I don't think that you can call an if statement inside your echo function call, you need to seperate them from each others, something to try could be this:

echo'<button class=\"btn btn-default\" id='. $value;
if (isset($_SESSION['buttons']) && (in_array($value, $_SESSION['buttons']))) 
{
  echo'disabled';
} 
echo'></button>' ;

Solution 4:

Combining html and php using echo statements can quickly become messy and unreadable.

Read up on the alternative syntax for control structures and also on the ternary operator. Applying both conventions, your code would look more like this:

<?php$arr = array(1, 2, 3, 4);
    foreach ($arras &$value) : ?><buttonclass="btn btn-default"id="<?=$value;?><?=((isset($_SESSION['buttons']) && (in_array($value, $_SESSION['buttons'])))?' disabled':''?>"><?phpif (isset($_SESSION['buttons']) && (in_array($value, $_SESSION['buttons']))) : ?><imgsrc="assets/images/resev.png"class="img-circle"width="40"height="40" /><?phpelse : ?><imgsrc="assets/images/aval.png"class="img-circle"width="40"height="40" /><?phpendif; ?></button><?phpendforeach; ?>

The parse error is a result of your attempt to chain an if statement ($value if...) with an echo statement, and also just having disabled instead of echo 'disabled' in your if {} statement.

Solution 5:

Aside from readability, the issue can be fixed like this:

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

Here I used what's known as a ternary operator.

Here's it, but a bit clearer:

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

A ternary operator is similar to an if statement, but is commonly used for inline things.

The syntax is this:

$myVar = (expression ? true : false);

Here's an example of how to get "Woo!" when a number is 1.

$number = 1;
$wooOrNe = ($number === 1 ? "Woo!" : "Ne."); // Woo!

Post a Comment for "Echo Html With Php Inside"