gpt4 book ai didi

php - 查询字符串,从数据库或文件填充页面

转载 作者:行者123 更新时间:2023-11-29 14:40:24 25 4
gpt4 key购买 nike

此 Facebook 链接是否完全从数据库填充?或者,它是一个包含 PHP 的物理文件吗?只是,这个页面怎么称呼?
http://www.facebook.com/profile.php?id=49300915&sk=photos
他们可能会做类似的事情:

if(isset($_GET['id'], $_GET['sk'])) {
mysql_query("SELECT info, photos FROM users WHERE id = '$id'");
}

我想问一下,他们是如何包含这个页面的?是像 Drupal/任何 CMS 一样,PHP 和页面存储在数据库中,还是服务器上的物理文件?如果是后者,获取文件的最佳方式是什么(不区分大小写的 URL)?

最佳答案

我会有一个只有一个方法的类,它读取“sk”并运行另一个方法,具体取决于它的值是什么。

一种方法是“photos”,它会读取“id”并从数据库中获取照片。然后,它将运行另一个方法 displayPage,该方法将显示该数据的页面。

displayPage 方法采用"template"文件名和一组变量来提供给模板。它设置了 smarty object ,提供变量,并指示其显示模板。

在模板内,我将包含另一个用于站点中每个页面上的全局页眉的模板,然后我将拥有 html 页面内容,使用 smarty 插入动态值,然后包含全局页脚。

请注意,我已经大大简化了这个系统。像这样的真实页面需要我一周的时间来编写所有代码,因为一个大网站会做很多事情只是为了显示一个简单的页面(例如:查明登录用户是否确实有权访问该页面。 .我无法访问您提供的示例)。

<?php

// profile.php

class ProfileController
{
public function run()
{
if ($_GET['sk'] == 'photos')
return $this->photosPage();
}

protected function photosPage()
{
$id = (int)$_GET['id'];
$result = mysql_query("select * from photo where id = $id");
$photo = mysql_fetch_object($photo);

$this->displayPage('view-photo.tpl', array('photo' => $photo);
}

protected function displayPage($templateFile, $templateVariables)
{
$smarty = new Smarty();

foreach ($templateVariables as $variableName => $variableValue) {
$smarty->assign($variableName, $variableValue);
}

$smarty->display($templateFile);
}
}

$conntroller = new ProfileController();
$controller->run();

还有 smarty 代码:

<!-- view-photo.tpl -->
{include file='head.tpl'}

<h1>View Photo {$photo->name|escape}</h1>

<img src="{$photo->src|escape}" width="{$photo->width|escape} height="{$photo->height|escape}>

{include file='foot.tpl'}

关于php - 查询字符串,从数据库或文件填充页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8107638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com