简体   繁体   English

如何使链接“ index.php?x”在页面上显示x部分

[英]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.). 我正在尝试制作一个带有不同部分的网站,这些部分将带有问号(例如index.php?notes,index.php?options等)。 Normally I see this often on search query pages or blogs (with an ?q="question"). 通常,我经常在搜索查询页面或博客上看到此问题(带有?q =“ question”)。

On my index.php page i have set multiple sections that will be shown if the user presses a href link. 在我的index.php页面上,我设置了多个部分,如果用户按下href链接将显示这些部分。

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. 我知道可以使用css中的锚点链接(带有#options)来完成此操作,但是由于我使用的是php,因此我想尝试一下。

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'] . 使用?q=question您可以通过$_GET['q']在PHP中访问它。 In your case, since you don't have q set, but ?notes you need to access it via $_GET['notes'] . 在您的情况下,由于您没有设置q ,但是?notes需要通过$_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; 您可以设置一个GET变量p并读取它; eg 例如

<a href="?p=notes">notes</a> and in PHP $_GET['p'] <a href="?p=notes">notes</a>和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 我建议使用键,即?p = notes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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