1

I have an array in PHP like this:

$link_array = [
  "Our_dogs" => "ourdogs.php",
  "About_us" => "aboutus.php",
  "Puppies" => "puppies.php",
  "Contact" => "contact.php",
  "Login" => "login.php",
  "Guestbook" => "guestbook.php"
];

I want to pass this array to javascript and then print it on my page, but if Im on, for example, the page for Our dogs, I dont want that link to show. Is that possible?

I have tried this:

foreach($link_array as $key=>$value){
    if(($key == "Our_dogs") && ($value == "ourdogs.php")){
        $_SESSION['Our dogs'] = $value;
      break;
 }

 $responseText['Our_dogs'] = $_SESSION['Our_dogs'];
 echo json_encode($responseText);

And this for JS:

var response = JSON.parse(this.responseText);
var li = document.createElement('li');
        console.log(response.Our_dogs);
        li.innerHTML = '<a href=\"' + response.Our_dogs + '\">OUR DOGS</a>';
        byId("nav").appendChild(li);

but Im new to this and not sure how to do this. How can I do this in a correct way and is it possible to print only the links to the other pages (not the page showing)?

3
  • I assume you're retrieving the list through an AJAX call? If so, what is your call currently returning? Also, are you currently receiving any errors, etc.? Commented Mar 24, 2017 at 9:46
  • You can refer this link: thisinterestsme.com/passing-php-array-javascript Commented Mar 24, 2017 at 9:50
  • Yes, Im retrieving the list through an AJAX call. No errors, just that i dont know how to pass all the links through correctly and show just the ones I want to show. :) Commented Mar 24, 2017 at 10:21

1 Answer 1

1

Why do you even need JavaScript?

You can do it like this:

$link_array = [
   "Our_dogs" => "ourdogs.php",
   "About_us" => "aboutus.php",
   "Puppies" => "puppies.php",
   "Contact" => "contact.php",
   "Login" => "login.php",
   "Guestbook" => "guestbook.php"
];

foreach($link_array as $key=>$value){
   // note that $_SERVER['PHP_SELF'] returns filename with prepended slash
   if( '/' . $value != $_SERVER['PHP_SELF'] ){
      echo '<li><a href="' . $value . '">' . $key . '</a></li>';
   }
}

You need to add foreach loop in ul#nav tag.

This code is not tested and I'm assuming that your not calling ajax request.

UPDATE:

$link_array = [
   "Our_dogs" => "ourdogs.php",
   "About_us" => "aboutus.php",
   "Puppies" => "puppies.php",
   "Contact" => "contact.php",
   "Login" => "login.php",
   "Guestbook" => "guestbook.php"
];

echo json_encode($link_array);

Ajax:

$.ajax({
   dataType: "json",
   ...
})
.done(function( response ) {
   var currentUrl = window.location.href 
   var currentUrlLastPart = currentUrl.substr(s.lastIndexOf('/') + 1);
   for (var key in response)
      if(val != currentUrlLastPart){
          var li = document.createElement('li');
          li.innerHTML = '<a href="' + response[key] + '">' + key + '</a>';
          document.getElementById("nav").appendChild(li);
      }
   });
});

Something like this?

Sign up to request clarification or add additional context in comments.

3 Comments

I need to pass it to javascript because I want to pass them into an if-statement i have and only show some of the links to logged in users.
Thanks, that might work, but is that really javascript or is it jQuery maybe?
@Isla Ajax call is jQuery, everything else is pure Javascript. I was guessing that you already have your Ajax call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.