简体   繁体   English

使用PHP / Ajax更改选项卡(和URL)

[英]Changing Tabs (and URL) with PHP/Ajax

I'm not really sure what the correct term is for this, but I guess it's tabbed URLs or something similar. 我不太确定正确的用词是什么,但是我想它是选项卡式URL或类似名称。 Anyway, I see it a lot on the web and I'm tying to figure out how the effect is aheived. 无论如何,我在网络上经常看到它,我想弄清楚这种效果是如何避免的。

A good example are the tabs found when editing your Github profile at https://github.com/settings/profile 一个很好的例子是在https://github.com/settings/profile上编辑Github配置文件时找到的选项卡

Let's say we have menu with some tabs: 假设我们的菜单带有一些标签:

<ul>
<li><a href="tab1">tab1</a></li>
<li><a href="tab2">tab2</a></li>
<li><a href="tab3">tab3</a></li>
</ul>

and then a content div: 然后是内容div:

<div class="tab-content">

</div>

From what I can see, when the respective tab link is clicked, an ajax request is sent out to fetch the data which then gets inserted into the content area. 据我所知,当单击相应的选项卡链接时,会发出ajax请求以获取数据,然后将其插入内容区域。 Which is pretty much standard from what I understand. 根据我的理解,这几乎是标准的。

However, the part I don't get is how when the url is loaded in a new tab or window, it goes straight to the respective tab. 但是,我没有得到的部分是如何将URL加载到新的选项卡或窗口中时,如何直接进入相应的选项卡。

For example, if you visit: https://github.com/settings/notifications you'll go straight to the notifications tab. 例如,如果您访问: https : //github.com/settings/notifications,您将直接转到通知标签。

I'm trying to implement a similar tab setup with PHP. 我正在尝试使用PHP实现类似的标签设置。 The ajax part isn't really an issue, I can get that working quite easily. ajax部分并不是真正的问题,我可以很轻松地使它工作。 However, getting the url to load the respective tab on a fresh request, is what I'm stuck at. 但是,获取URL以在新请求上加载相应的选项卡是我所坚持的。

The way I was thinking of doing it is simply checking the URL and then loading the tab, like this: 我想到的方法只是检查URL,然后加载选项卡,如下所示:

<div class="tab-content">
<?php
$URL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

if ($URL = "http://example.com/tabs/tab1") {
    echo "this is tab1 content";
}
else if ($URL = "http://example.com/tabs/tab2") {
    echo "this is tab2 content";
}
// etc etc
?>
</div>

But if I have 20 tabs, I don't really want to put 20 if/else statements in my page. 但是,如果我有20个标签,则我真的不想在页面中放置20条if / else语句。 The only other way I was thinking is placing the URL values in an array, and looping through it. 我唯一想到的另一种方法是将URL值放入数组中,并遍历它。 Is there a better way of setting up a tab system like this with PHP? 是否有更好的方法用PHP设置这样的标签系统?

You can also put your data into database and then query database. 您也可以将数据放入数据库,然后查询数据库。 But without using database you can also for example put tab content into files and do something like this: 但是,例如,在不使用数据库的情况下,您也可以将选项卡内容放入文件中并执行以下操作:

<?php
   $URL = $_SERVER['REQUEST_URI'];

   $file = 'path_with_content' . DIRECTORY SEPARATOR . str_replace('/' , DIRECTORY_SEPARATOR, $URL).'.php';
   if (file_exists($file))  {
     echo file_get_contents($file);
  }

So you simple create directory path_with_content/tabs and for each tab you create file inside this directory with the same name as last part as last part of your url (tab1, tab2 and so on) with .php extension (so files should have names tab1.php, tab2.php and so on). 因此,您可以简单地创建目录path_with_content/tabs并为每个选项卡在此目录中创建文件,文件的名称与url的末尾部分(tab1,tab2等)的名称相同,扩展名为.php (因此文件应具有名称tab1 .php,tab2.php等)。

you can use an array such as $navigation = Array('Home' => 'index.php', 'About' => 'about.php'); 您可以使用$ navigation = Array('Home'=>'index.php','About'=>'about.php')之类的数组;

then if you make an global variable like something as: $curPage 那么如果您创建一个全局变量,例如:$ curPage

and you load the array 然后加载数组

for each loop and then check the $curPage is looped and make it another font colour 对于每个循环,然后检查$ curPage是否循环并使它成为另一种字体颜色

the $curPage will be 'Home' or 'About' then show it with another font or bg colour $ curPage将是“ Home”或“ About”,然后以其他字体或bg颜色显示

this I my paste about it - it's not the best one yet it is working - forgot to put out the numbering xD - http://pastebin.com/H1XcUiiE 这是我的贴子-并不是最好的贴子-忘记了编号xD- http://pastebin.com/H1XcUiiE

TIP: Instead of putting it up in ever page you can create a navigation.php file and include it into the area of the navigation bar all the time. 提示:可以将其放置在导航栏区域中,而无需将其放置在任何页面上,而是可以创建navigation.php文件。

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

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