0

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

我想这样做,所以我的 URL 应该是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);
    }
}

?>
4

1 回答 1

0

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

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

可能值得拥有两个功能,一个通过 ID 获取,一个通过标题获取。

于 2013-11-10T22:12:42.190 回答