我要找的只是当运行下面的代码并单击专辑或音乐链接时,它会发布到同一页面,对吗?因此,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?相册,当我点击这个链接并转到url yorpage.com/lr/username时,我如何调用php函数或语句?相册?
因此,一个简单的回声就可以用于演示目的,只是为了在点击时获得回报,这就是我想要的。如果你需要更多信息,我不认为你需要。我不是要求为我编码,我可以返回图像或专辑等。我只想知道如何处理被点击的链接,并在点击专辑或音乐链接时返回简单的回声。非常感谢。
我有点迷路了,但据我所知,你的问题是:我如何在url中传递参数,以便在转到url时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;
}
}
根据您的需要进行调整。。。
注意:请注意,我将参数改为page而不是username,因此您必须更新您的重写mod规则。我还使用/分隔页面"arguments"。如果你喜欢的话,你可以用"?"(我想,不确定)。。。
下面是一个简单的MVC
index.php
// 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;
}
}
文件页/home.php
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;
}
}
}
文件页/user.php
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;
}
}
}
templates/my_template.html
<!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>
我可能会感到困惑(因为那是很多文本),但我认为你正在寻找的是关于htaccess的教程。。。使用重写规则以静默方式转换url:
http://domain/[username]/album
至
http://domain.com?script.php?user=[username]&action=album
或者类似的东西。用户将看到第一个url,但脚本将可以访问$_GET
数组中的user
和action
。。。然后你可以检查
if(isset($_GET['action']) && $_GET['action']=='album'){ //do something...
有很多关于这方面的教程,但这是我找到的第一个。