使用PHP模板输出带有数据库值的网页(变量URL)

使用PHP模板输出带有数据库值的网页(变量URL)

问题描述:

I did some searching around, but I'm not very experienced in PHP, so I don't know the correct Terms/Words I am looking for. However, I do have a good idea of what I want to do.

  1. I have a website with many images, each image has a database entry (MySQL).
  2. I would like each image to have its own landing page, based on its database values. (Title, Category, Tags, etc...)
  3. I don't want to make a separate .php file on my server for each image. (Eg: pinkflowers.php, redroses.php, etc...)
  4. There should be 1 PHP template file, that outputs the webpage for ALL images, based on the URL (variable) that the user visits.

So if someone visits "mysite.com/pinkflowers.php", the page should be output with the variables of pinkflowers from my database.

However, the file pinkflowers.php doesn't actually exist, only template.php exists, which would be the "blueprint" for all the images in my database.

  1. I would like the .php to be removed from the URL in the browser.

    "mysite.com/pinkflowers.php" => "mysite.com/pinkflowers"

    I already have code that does this with my existing pages (below); I'm not sure if it will also work with these "imaginary" pages.

    (.htaccess)    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    
  2. If the user tries to visit a page that has no related database entry (eg: "mysite.com/430jfif0ij"), they should be re-directed to the homepage.

I'm not expecting anyone to give me ALL the answers, but please at least guide me in the right direction to begin making such a template. Thanks!

我做了一些搜索,但我对PHP不是很有经验,所以我没有 知道我正在寻找的正确术语/单词。 但是,我确实知道自己想做什么。 em> p>

  1. 我有一个 网站上有很多图像,每个图像都有一个数据库条目(MySQL)。 li>
  2. 我希望每个图像都有自己的登陆页面,基于它的数据库值。 ( Title code>, Category code>, Tags code>等...) li>
  3. 我不想做一个 我的服务器上为每个图像分开.php文件。 (例如: pinkflowers.php code>, redroses.php code>等...) li>
  4. 应该有1个PHP模板文件,输出 所有图片的网页,基于用户访问的网址(变量)。 li> ol>

    因此,如果有人访问“mysite.com/pinkflowers.php”,页面 应该从我的数据库输出 pinkflowers code>的变量。 p>

    但是,文件 pinkflowers.php code>实际上并不存在, 只有 template.php code>存在,这将是我数据库中所有图像的“蓝图”。 p>

    1. 我想要 .php code>将从浏览器中的URL中删除。 p>

      “mysite.com/pinkflowers.php”=> “mysite.com/pinkflowers"

      nn

      我已经有了使用我现有页面(下面) em>执行此操作的代码; 我不确定它是否也适用于这些“虚构”页面。 p>

       (。htaccess)
      RewriteCond%{REQUEST_FILENAME}!-d 
      RewriteCond%{REQUEST_FILENAME}  \ .php -f 
      RewriteRule ^(。*)$ $ 1.php 
        code>  pre>  li> 
       
    2. 如果用户试图访问没有相关的页面 数据库条目(例如:“mysite.com/430jfif0ij”),应将它们重定向到主页。 p> li> ol>

      我' 我不希望任何人给我所有的答案,但请至少指导我正确的方向开始制作这样的模板。 谢谢! em> p> div>

I was able to solve my own question eventually, in the mean time that no Answers were given.

I followed this Tutorial here: http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

My .htaccess contained:

RewriteEngine on    
Options +FollowSymLinks

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  

RewriteRule ^category1/(.*)$ ./template.php?image=$1
RewriteRule ^category2/(.*)$ ./template.php?image=$1
RewriteRule ^category3/(.*)$ ./template.php?image=$1
etc...

category# - being the "folders" that my images are categorized by.

I did this on the template.php to extract the Variable from the URL:

$result = mysql_query('SELECT * FROM image_list WHERE image_name="' . mysql_real_escape_string($_GET['image']) . '"');

To redirect the visitor to the Homepage if they enter an invalid URL/ID, I used this:

if (mysql_num_rows($result)==0) {
echo "<script>window.location = 'http://mysite.com/'</script>";
}

So the combination of all these methods achieved exactly what I was looking for.

Let your .htaccess be something like:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ template.php/$1

Then (on template.php) use $_SERVER["path_info"] to determine the path the user has entered (i.e. /pinkflowers) and query the database accordingly.

The reason I like $_SERVER["path_info"] is because it doesn't use (or pollute) the GET variable space.