简体   繁体   中英

How to make the link 'index.php?x' display x section on a page

I was trying to make a site with different parts which will be shown with a questionmark (like index.php?notes, index.php?options, etc.). Normally I see this often on search query pages or blogs (with an ?q="question").

On my index.php page i have set multiple sections that will be shown if the user presses a href link.

I know that this can be done with the anchor link in css (with #options), but since I am using php, I would like to try that.

This is my code at the moment:

<body>
  <p>
[<a href="?notes">Notes</a>]
[<a href="?categories">Categories</a>]
[<a href="?options">Options</a>]
 </p>

 <?php
if($display == 'categories') {
echo "<h4>Categories</h4>";
} else if ($display == "notes") {
    echo "<h4>Delete Notes</h4>";
} else {
    echo "<h4>Options</h4>";
}
?>
</body>

When using ?q=question you can access it in PHP via $_GET['q'] . In your case, since you don't have q set, but ?notes you need to access it via $_GET['notes'] . Try this:

<?php

if (isset($_GET['categories'])) {
    echo "<h4>Categories</h4>";
} elseif (isset($_GET['notes'])) {
    echo "<h4>Delete Notes</h4>";
} else {
    echo "<h4>Options</h4>";
}

You could set a GET variable p and read that; eg

<a href="?p=notes">notes</a> and in PHP $_GET['p']

if you don't want to use any keys, then you can use this code:

if(isset($_GET)){
    $page = key($_GET);
}

But this solution is not very good it can mess your code later. I suggest to use with key ie ?p=notes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM