Question

I want to add PHP into PHP if statement. More precisely I want to show/hide modules in OpenCart according to permission. I ended up with something like this:

<?php if ($this->user->hasPermission('access', 'module/' <?php echo $extension['name']; ?> ')) { ?>

whole implementation

<?php foreach ($extensions as $extension) { ?>
      <?php if ($this->user->hasPermission('access', 'module/' <?php echo $extension['name']; ?> ')) { ?>
      <tr>
        <td class="left"><?php echo $extension['name']; ?></td>
        <td class="right"><?php foreach ($extension['action'] as $action) { ?>
          [ <a href="<?php echo $action['href']; ?>"><?php echo $action['text']; ?></a> ]
          <?php } ?></td>
      </tr>
      <?php } ?>
      <?php } ?>
Was it helpful?

Solution

You have made a mistake. it should be like:

user->hasPermission('access', 'module/'.$extension['name'])) { ?>

OTHER TIPS

<?php if ($this->user->hasPermission('access', 'module/' <?php echo $extension['name']; ?> ')) { ?>

This won't work. You are already in PHP so attempting to do that other echo within will just result in a syntax error. You are looking to concatenate string here:

<?php if ($this->user->hasPermission('access', 'module/' . $extension['name'] )) { ?>

see docs:http://www.php.net/manual/en/language.operators.string.php

Code is messy and error prone, I suggest you to do it all in PHP.

Echo the HTML and user proper string concatenation.

<?php
foreach ($extensions as $extension) {
    if ($this->user->hasPermission('access', 'module/'.$extension['name'])) {
        echo '<tr>
        <td class="left">'.$extension['name'].'</td>
        <td class="right">';
        foreach ($extension['action'] as $action) { 
           echo '[ <a href="'.$action['href'].'">'.$action['text'].'</a> ]';
        }
          echo '</td>'; 
        echo'</tr>';
    } 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top