-4

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?

Sarah James
  • 431
  • 5
  • 19
  • There are several issues with your syntax. Please check your use of brackets, semi-colon and $. Also, your logic doesnt make much sense. You might want to rethink whyt you are trying to do here and rewrite from scratch. – ToBe Jan 30 '14 at 14:59

5 Answers5

3

Try with:

<div class="stat <?php echo $idea->getAccess() ? 'unpublished' : ''; ?>"></div>
hsz
  • 148,279
  • 62
  • 259
  • 315
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.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

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

Jim
  • 22,354
  • 6
  • 52
  • 80
  • Best attempt of finding all of his problems so far. Still loads of them in his code though. – ToBe Jan 30 '14 at 15:01
1

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>
cernunnos
  • 2,766
  • 1
  • 18
  • 18
  • What error are you getting? check if the object you are using (in my answer that would be $idea) is an instance of the class that has the method "getAccess()", you can see the class of an object with a simple "echo get_class($idea);" – cernunnos Jan 30 '14 at 15:51
  • remember `class="unpublished"` is an HTML not php. `unpublished` is a css. – Sarah James Jan 30 '14 at 15:53
  • 1
    you cant have two class attributes (you can have more classes, but only 1 attribute) on a single element, you already have one class written on the html, "unpublished" will simply be added to your class if the condition is true, assuming that is what you want. – cernunnos Jan 30 '14 at 15:54
0

You forgot a semi-colon:

getAccess();
Ana Claudia
  • 501
  • 6
  • 16