按标题加载不按ID加载

时间:2013-11-10 22:05:11

标签: php url url-rewriting

如何让我的PHP脚本按标题而不是id加载图像。

我想这样做,所以我的网址应该是http://picxeto.com/wallpapers/“壁纸标题”,而不是http://picxeto.com/wallpapers/“壁纸ID”。

这是我到目前为止所拥有的:

<?php 

class wallpaper
{
    private $properties;
    var $db;

    function __construct($id, $dbase)
    {
        $this->db = $dbase;

        if (is_numeric($id))
        {
            $sql = sprintf(
                "SELECT * FROM wallpapers 
                WHERE ID=%d",
                $this->db->clean($id)
            );

            $result = $this->db->query($sql);
            $fields = $this->db->fetch_array($result);

            foreach ($fields as $k => $v)
            {
                $this->properties[$k] = $v; 
            }

            $this->sizes = unserialize($this->sizes);
        }
    }

    function __get($k)
    {
        return $this->properties[$k];
    }

    function __set($k, $v)
    {
        $this->properties[$k] = $v;
    }

    function update()
    {
        $sql = sprintf(
            "UPDATE wallpapers SET 
            title='%s', 
            copyright='%s', 
            sizes='%s', 
            category='%s',
            filename='%s' 
            WHERE ID=%d
            )",
            $this->db->clean($this->title),
            $this->db->clean($this->copyright),
            serialize($this->sizes),
            $this->db->clean($this->category),
            $this->db->clean($this->filename),
            $this->ID
        );

        $this->db->query($sql);
    }

    function create()
    {
        $sql = sprintf(
            "INSERT INTO wallpapers (title, copyright, sizes, category, filename) 
            VALUES('%s', '%s', '%s', '%s', '%s')",
            $this->db->clean($this->title),
            $this->db->clean($this->copyright),
            serialize($this->sizes),
            $this->db->clean($this->category),
            $this->db->clean($this->filename)
        );

        $this->db->query($sql);
    }

    function delete()
    {
        $sql = sprintf(
            "DELETE FROM wallpapers  
            WHERE ID=%d",
            $this->ID
        );

        $this->db->query($sql);
    }
}

?>

1 个答案:

答案 0 :(得分:0)

您只需要更改SQL查询,以便使用查询。一个简单的版本是:

SELECT * FROM wallpapers WHERE title LIKE '%some title%'

值得拥有两个函数,一个是按ID获取,另一个是按标题获取。