Вопрос

I am trying to do an if statement if a function returns true and the else if it doesn't

Basically i need to show a html class in the return 1. I have try that below but I got an error. syntax error, unexpected 'if' (T_IF) This is what I have tried

    <div class="inner">
        <div class="stat" 
        <?php $access->getAccess() 
        if (getAccess == 1) {
        class="unpublished"
        }
        else {

    }
    ?>
    >
</div>
</div>

this is what the function looks like

 public function getAccess()
    {
        return $this->access;
    }

How can I do this right?

Это было полезно?

Решение 2

You PHP code isn't echoing anything, plus you are missing semicolons.

Try to simplify this a bit:

<div class="stat <?=$idea->getAccess() == 1 ? 'unpublished' : ''?>">

P.S. You need to put all your classes into the same attribute.

Другие советы

Try with:

<div class="stat <?php echo $idea->getAccess() ? 'unpublished' : ''; ?>"></div>

To call a function you need to use brackets:

$access = getAccess();//Not getAccess

I note that you call another getAccess function too so you may have meant to do:

$access = $idea->getAccess() ;
if ($access == 1) {

Edit: Others have pointed out you are also missing a ;.

You need to conceptually separate PHP from your HTML. What is happening is that your PHP is being interpreted by your web server resulting in a HTML document that will be delivered to the client. What you want is to add a class to the div if an $idea has access, something like this:

<div class="inner">
    <div class="stat <?php echo $idea->getAccess() ? "unpublished" : ""?>">
    </div>
</div>

This is equivalent to a much more complex (and in my opinion harder to read):

<div class="inner">
    <div class="stat <?php 
       if ($idea->getAccess()) {
         echo "unpublished";
       }
      ?>">
    </div>
</div>

You forgot a semi-colon:

getAccess();
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top