php - 如何使用 PHP 和 MySQL 将 URL 类别编号更改为单词?

标签 php mysql sql .htaccess seo

我一年前买了一个 iPhone 壁纸库 PHP 脚本,它运行得很好。我一直在慢慢地学习 PHP 和 MySQL,通过在这里和那里做一些小调整。

要查看类别,URL 是 www.example.com/index.php?catid=27,我想将其更改为 www.example.com/index。 php?catid=apple 出于 SEO 原因。我知道我可以使用 .htaccess 进一步整理 URL,但我只想以这个单词 slug 开头。

我认为这是生成类别页面的代码(在 index.php 中):

if (($_REQUEST['catid']) && ($_REQUEST['catid'] > 1)) {
        $catid = $_REQUEST['catid'];
        if (is_numeric($catid)) {
            $tempitemarray = $myitems->getItemsByCategory($catid);
            $catnav = "&catid=" . $catid;
            $tempcat = new Category();
            $tempcat->getCategory($catid);
            $maintitle = "<h1>" . $tempcat->getCategoryName() . " " . $itemplural . "</h1>";
        }

该代码调用另一个名为 itemList.php 的 PHP 页面,在该页面中我找到了这段代码:

public function getItemsByCategory($catid) {
        $this->retrieveSQL = "select * from items i, item_category_lookup l where livedate < '" . $this->currentdate . "' and  i.active = " . $this->showApproved . " and l.categoryid = $catid and i.id = l.itemid order by i.livedate desc limit " . $this->startRow . "," . $this->numRows;
        $this->countSQL = "select * from items i, item_category_lookup l where livedate < '" . $this->currentdate . "' and  i.active = " . $this->showApproved . " and l.categoryid = $catid and i.id = l.itemid";
        $this->retrieveItems();
        return $this->items;
    }

这是“项目”表:

    id | itemname | itemdec | itemkeywords | createdate | viewcount | active | rating | livedate | sourcedesc | sourceurl
-----------------------------------
    64 | Apple Logo

这是“item_category_lookup”表:

categoryid | itemid
-------------------------
27         | 64

这是下面的“类别”表格。它没有在上面的查询中调用,但确实有实际的类别名称。我将如何实现它?

id | categoryid
------------------
27 | Apple

最佳答案

您可以将 $this->retrieveSQL = ... 的 SQL 更改为

if (!is_number($catid)) {
        $this->retrieveSQL = "select * from items i, item_category_lookup l, category j where livedate < '" . $this->currentdate . "' and  i.active = " . $this->showApproved . " and i.id = l.itemid and j.id = l.categroyid and j.categoryid = "$catid" order by i.livedate desc limit " . $this->startRow . "," . $this->numRows;
}

关于php - 如何使用 PHP 和 MySQL 将 URL 类别编号更改为单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16489219/

相关文章:

php - 执行多个查询时的 Doctrine (DBAL) 错误处理

php - 为什么我的 PHP Blowfish 加密会产生如此奇怪的结果?

php - 没有 ProfilePage 索引,但我有 LoginAndSignupPage

javascript - Laravel AJAX 到 Controller 并返回数据

mysql - 如何检查 SQL 表中是否存在一对。约束

sql - A left outer join B 如何返回比 A 中更多的行?

php - MYSQL PHP JSON 返回每个产品的所有图像

mysql - 我究竟做错了什么?分钟(日期),按用户分组

PHP SQL 仅每隔两个条目

java - SQL查询: incorrect work with entity