Nirajan Pokharel
Nirajan Pokharel

Reputation: 1139

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

Upvotes: 0

Views: 254

Answers (6)

SysVoid
SysVoid

Reputation: 584

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

$arr = array(1, 2, 3, 4);
foreach ($arr as &$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 ($arr as &$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!

Upvotes: 1

halfer
halfer

Reputation: 20487

(Posted on behalf of the OP).

Working code:

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

Upvotes: 1

G San
G San

Reputation: 81

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>' ;
  }
?>

Upvotes: 1

Deep
Deep

Reputation: 2512

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 template
include_once 'template.phtml';

template.phtml contents (evaluate template)

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

Upvotes: 2

dansalias
dansalias

Reputation: 817

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 ($arr as &$value) : ?>
    <button class="btn btn-default" id="<?=$value;?><?=((isset($_SESSION['buttons']) && (in_array($value, $_SESSION['buttons'])))?' disabled':''?>">
        <?php if (isset($_SESSION['buttons']) && (in_array($value, $_SESSION['buttons']))) : ?>
            <img src="assets/images/resev.png" class="img-circle" width="40" height="40" />
        <?php else : ?>
            <img src="assets/images/aval.png" class="img-circle" width="40" height="40" />
        <?php endif; ?>
    </button>
<?php endforeach; ?>

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.

Upvotes: 1

Paul Karam
Paul Karam

Reputation: 4235

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>' ;

Upvotes: 1

Related Questions