1

I am using usercake to generate links to different permissions.

All works fine if I want hyperlinks, I added in an image bit and it broke the code, it will load but the entire address bar goes like there are no permissions.

Before:

//Links for permission level 3 (xx)
    if ($loggedInUser->checkPermission(array(3))){
    echo "<div id='xx'>
    <ul>
    <li><a href='xx.php'>xxx</a></li>
    </ul></br ></div>";
    }

After

//Links for permission level 3 (xx)
    if ($loggedInUser->checkPermission(array(3))){
    echo "<div id='xx'>
    <ul>
    <li><a href="xxx.php"><img src="xx"></a></li>
    </ul></br ></div>";
    }

It just makes the menu bar disappear.

Any ideas? Am I missing something obvious?

--update is it because I have not escaped properly?

Marriott81
  • 275
  • 2
  • 16

3 Answers3

2

" characters will terminate the string literal, and if you don't follow them with a concatenation operator (either . or ,), your code will throw a syntax error.

You need to escape them using \:

if ($loggedInUser->checkPermission(array(3))){
    echo "<div id='xx'>
    <ul>
    <li><a href=\"xxx.php\"><img src=\"xx\"></a></li>
    </ul></br ></div>";
}

Failing that, since you're not outputting any variable inside your string, you can use ' (single quotes) to begin and terminate the literal, which means you won't have to escape double quotes inside of it. For example:

if ($loggedInUser->checkPermission(array(3))){
    echo '<div id="xx">
    <ul>
    <li><a href="xxx.php"><img src="xx"></a></li>
    </ul></br ></div>';
}
BenM
  • 52,573
  • 26
  • 113
  • 168
  • Thanks, I literally just realized that as you posted, thanks mate will try now – Marriott81 Feb 12 '14 at 10:22
  • As an update, if I watned to style it within the img src (I know I should use the Div to do so but go with me) would it be : \width="\600" \height="\300" ? – Marriott81 Feb 12 '14 at 10:28
  • No, you're not escaping properly there. It would be: `width=\"600\" height=\"300\"`. Please see my edit regarding the single-quotes, which may work better for you. – BenM Feb 12 '14 at 10:28
  • Thanks Ben just saw, changed it to single quotes and it works much better, cannot accept for another 7 mins tho. – Marriott81 Feb 12 '14 at 10:30
  • Out of interest, rather than starting a new question, I have floated them all left in the CSS, they are now all next to each other going right, is there a top function or something in css that I can but them under each other? or just us padding? – Marriott81 Feb 12 '14 at 10:45
  • Use `img { display: block; }`. – BenM Feb 12 '14 at 10:47
0

As you can see from the syntax highlighting in your post, you have a problem with double/single quotes.

Your code should look something like this:

//Links for permission level 3 (xx)
if ($loggedInUser->checkPermission(array(3))){
  echo "<div id='xx'>
  <ul>
  <li><a href='xxx.php'><img src='xx'></a></li>
  </ul></br ></div>";
}

Your outer quotes should stay double but all quotes within the string should be single quotes. This also makes sense as you would want to keep your code consistent. If you've used a single quote within a string then stay with that single quote unless you absolutely must use double, in which case you could escape the quotes:

<li><a href=\"xxx.php\"><img src=\"xx\"></a></li>
Lix
  • 47,311
  • 12
  • 103
  • 131
  • Why should the outer quotes stay double? He's not outputting any variables inside the string literal, so there's no need to enforce double-quotes here. – BenM Feb 12 '14 at 10:26
0

Simply close and then reopen PHP tags. Take HTML as separated as you can from your code .. it's cleaner and more performant ;)

Depending on the structure of the rest of your document you could benefit of the control structures alternative syntax available in PHP (that I suggest to use when it is the case)

  //Links for permission level 3 (xx) 
  if ($loggedInUser->checkPermission(array(3))) : 
?>
  <div id='xx'> 
    <ul>
      <li><a href='xxx.php'><img src='xx'></a></li>
    </ul>
  </div>
<?php endif; 
drAlberT
  • 22,059
  • 5
  • 34
  • 40