0

i'm using a view thats pulls data from a certain node via refference and i show non logged in users a error that they need to login, but i need to show a special node/add link with the nid in it. i'v got the code and it works without the if statement, in the if statement, my code is not working..

<?php global $user;if (!$user->uid){print '<div class="messages error"><h4 class="alert-heading logged_out">Oops!</h4><p>tekst hier</p></div>';}else{
 (arg(0) == 'node' && is_numeric(arg(1))) {  $nid = arg(1);print '<a class="btn" href='/node/add/album&field_band[]=$nid'>Voeg een album toe!<a/>';} ?>



<?php  if (arg(0) == 'node' && is_numeric(arg(1))) {  $nid = arg(1);print '<a class="btn"  href='/node/add/album&field_band[]=$nid'>Voeg een album toe!<a/>';} ?>

the code above works, but not when its in the if/else block...

Please note that i don't have great knowledge about php.

Thanks!

1

You should write the if-else structure like:

<?php 
global $user;
if (!$user->uid) {
   print '<div class="messages error"><h4 class="alert-heading logged_out">Oops!</h4><p>tekst hier</p></div>';
}
else if(arg(0) == 'node' && is_numeric(arg(1))) {  
    $nid = arg(1);
    print '<a class="btn" href='/node/add/album&field_band[]=$nid'>Voeg een album toe!<a/>';
} 

?>

Note that after 'else' you want to test some conditions so you need to put an 'if' statement in front of your test.

| improve this answer | |
0

You're missing an if before the beginning of your second line of code; you should be getting a fatal error for invalid syntax, unless it's being suppressed somehow.

Try changing the first two lines to:

<?php 
global $user;
if (!$user->uid) { 
  print '<div class="messages error"><h4 class="alert-heading logged_out">Oops!</h4><p>tekst hier</p></div>';
} else {
  if (arg(0) == 'node' && is_numeric(arg(1))) { 
    $nid = arg(1);
    print '<a class="btn" href='/node/add/album&field_band[]=$nid'>Voeg een album toe!<a/>';
  } 
}
?>

Or go with Gabriel's solution to use an else if, it's more efficient anyway.

| improve this answer | |
  • I tried al of the above, but it won't work for some reason... the else switch works when i just add tekst after the else, but the php snippet not, hmm – user1064293 Aug 10 '12 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.