我正在寻找的是当下面的代码运行并且点击了相册或音乐链接时,它会发布到同一页面,对吗?因此,url会根据所点击的链接进行更改并在其末尾添加href。那么在点击链接时如何调用以下内容:
<?php echo 'Albums';?>
原始代码:
if(isset($_GET['username']) === true && empty($_GET['username']) === false){
$username = $_GET['username'];
if(user_exists($username) === true){
$user_id = user_id_from_username($username);
$profile_data = user_data($user_id, 'first_name','last_name','email');
?>
<h1><?php echo $profile_data['first_name']; ?>'s Yor Page</h1>
<div id="navWrapper">
<ul>
<li>
<a href="#"><img src="uploads/profile/blank_profile.gif" width="150" height="150" id="blank_profile"></a>
</li>
<nav>
<ul>
<li>
<a href="?albums">Albums</a>
</li>
<li>
<a href="?music">Music</a>
</li>
</ul>
</nav>
</ul>
</div>
<?php
}else{
echo 'Sorry, that user doesn\'t exist';
}
}else{
header('Location: index.php');
exit();
}
include 'includes/overall/overall_footer.php';
?>
现在我点击链接设置返回yorpage.com/lr/username?albums,当我点击此链接并转到url yorpage.com/lr/username时,如何调用php函数或语句?相册?
所以一个简单的回声可以用于演示目的只是为了获得点击的回报是我正在寻找的。如果您需要更多信息,我不认为您这样做我不是要求我为此编码我可以返回图像或专辑等。我只是想知道如何处理被点击的链接并返回一个简单的回声单击“专辑”或“音乐”链接时。谢谢。
答案 0 :(得分:2)
我有点失落,但据我所知,你的问题是:如何在网址中传递参数,以便当我转到网址http://www.example.com/username/albums时,它会转到相册吗?
这是一个类似的例子:
if (isset($_GET['page'])) {
$pageArgs = explode('/', $_GET['page']);
if(isset($pageArgs[0])) {
$username = $pageArgs[0];
} else {
header('Location: index.php');
exit();
}
$action = (isset($pageArgs[1])) ? $pageArgs[1] : false;
switch($action)
{
case 'albums':
print 'These are my albums <br/>';
break;
case 'music':
print 'This is my music! <br/>';
break;
default:
print '
<nav>
<ul>
<li><a href="'.$username.'/albums">Albums</a></li>
<li><a href="'.$username.'/music">Music</a></li>
</ul>
</nav>';
break;
}
}
根据您的需要调整......
注意:请注意我将参数更改为页面而不是用户名,因此您必须更新重写mod规则。我还使用/来分隔页面“参数”。您可以使用 ”?” (我想,不确定)如果你愿意...
这是一个简单的MVC
// Find which page is requested
$pageIDs = pageSelector();
// namespaced pages. You can change to real namespaces with:
// $mPage = '\\Page\\' . $pageIDs[0];
$mPage = 'Page_' . $pageIDs[0];
array_shift($pageIDs);
// Path to Template file (the static elements common to all pages like
// header, footer, logo, navigation, etc...
$templatePath = 'templates/my_template.html';
if(!empty($pageIDs))
$pageArgs = $pageIDs;
else
$pageArgs = false;
//create Page Object, the dynamic content divided by sections
//for instance, content, columnA, columnB
$page = new $mPage($pageArgs);
$template = new Template($templatePath, $page);
//Here's an example of a section
$template->addSection('subNavigation');
print $template;
// PageSelector Function
function pageSelector()
{
if (isset($_GET['page'])) {
$pIDs = explode('/', $_GET['page']);
} else {
$pIDs = array('home');
}
$pagePath = 'pages/' . $pIDs[0] . '.php';
if (file_exists($pagePath)) {
require_once($pagePath);
return $pIDs;
} else {
$pagePath = 'pages/home.php';
require_once($pagePath);
return array('home');
}
}
//Template Object
class Template
{
private $path, $page;
private $sections = array('content');
public function __construct($tPath, $page)
{
$this->path = $tPath;
$this->page = $page;
}
public function addSection($newSectionName)
{
$this->sections[] = $newSectionName;
}
public function __toString()
{
$html = file_get_contents($this->path);
foreach($this->sections as $section) {
$html = str_replace('[%%'.$section.'%%]', $this->page->show($section), $html);
}
return $html;
}
}
class Page_home
{
public function __construct($args) {}
public function show($section)
{
switch($section)
{
case 'content':
return 'this is my main page';
break;
default:
return '';
break;
}
}
}
class Page_user
{
private $subPage;
public function __construct($args)
{
if(isset($args[0]))
$this->username = $args[0];
else {
$this->username = false;
return;
}
if(isset($args[1]))
$this->subPage = $args[1];
else {
$this->subPage = false;
return;
}
}
public function show($section)
{
if($this->username)
{
switch($section)
{
case 'content':
$otp = 'My username is ' . $this->username . '<br/>';
$otp .= $this->showSubPage();
return $otp;
break;
case 'subNavigation':
return $this->nav();
break;
default:
return '';
break;
}
} else {
return 'username not found!';
}
}
protected function nav()
{
return
'<nav>
<ul>
<li><a href="user/'.$this->username.'/albums">Albums</a></li>
<li><a href="user/'.$this->username.'/music">Music</a></li>
</ul>
</nav>';
}
protected function showSubPage()
{
switch($this->subPage)
{
case 'albums':
return 'These are my albums <br/>';
break;
case 'music':
return 'This is my music! <br/>';
break;
}
}
}
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<div id="mainNav">myLink | Go | Here</div>
<nav id="subNav">[%%subNavigation%%]</nav>
<div id="content">[%%content%%]</div>
<footer>my (c) copyright notice</footer>
</body>
</html>
答案 1 :(得分:1)
我可能会感到困惑(因为这是很多文本),但我认为你正在寻找的是关于htaccess的教程...使用重写规则来静默转换网址:
http://domain/[username]/album
到
http://domain.com?script.php?user=[username]&action=album
......或类似的东西。用户将看到第一个网址,但该脚本可以访问user
数组中的action
和$_GET
...然后您可以查看
if(isset($_GET['action']) && $_GET['action']=='album'){ //do something...
有很多教程可供选择,但这里是我发现的first one。