2

我正在寻找的是当运行以下代码并单击专辑或音乐链接时,它会发布到同一页面,对吗?因此 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?albums 时,如何调用 php 函数或语句?

所以一个简单的回声就可以用于演示目的,只是为了获得点击的回报是我正在寻找的。如果您需要更多信息,我认为您不需要我不是要求为我编写代码我可以返回图像或专辑等。我只是想了解如何处理该链接被单击并返回一个简单的回声单击专辑或音乐链接时。谢谢你。

4

2 回答 2

2

我有点迷茫,但据我了解,您的问题是:如何在 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;
        }
    }

根据您的需要调整它...

注意:请注意,我将参数更改为页面而不是用户名,因此您必须更新重写 mod 规则。我还使用 / 来分隔页面“参数”。您可以使用 ”?” (我想,不确定)如果你喜欢......


这遵循一个简单的 MVC

索引.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; 
        }
    }
}

模板/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>
于 2012-07-31T23:16:07.620 回答
1

我可能会感到困惑(因为那是很多文字),但我认为您正在寻找的是关于 htaccess 的教程......使用重写规则静默转换 url:

http://domain/[username]/album

http://domain.com?script.php?user=[username]&action=album

... 或类似的东西。用户将看到第一个 url,但脚本将有权访问useraction$_GET数组中......然后你可以检查

if(isset($_GET['action']) && $_GET['action']=='album'){ //do something...

那里有大量的教程,但这是我找到的第一个。

于 2012-07-31T23:02:31.930 回答