查询字符串,从DB或文件填充页面

时间:2011-11-12 21:19:38

标签: php mysql html

此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)?

1 个答案:

答案 0 :(得分:1)

我会有一个带有单个方法的类,它会读取'sk'并运行另一个方法,具体取决于它的值。

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

displayPage方法采用“模板”文件名和一组变量来提供给模板。它设置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();

聪明的代码:

<!-- 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'}