dongtiao5094 2011-09-24 05:20
浏览 42
已采纳

如何根据帖子的日期和标题设置博客帖子的永久链接?

I am having this website http://www.finalyearondesk.com . My blogs post link are set like this.. http://www.finalyearondesk.com/index.php?id=28 . I want it to set like this ... finalyearondesk.com/2011/09/22/how-to-recover-ubuntu-after-it-is-crashed/ .

I am using the following function to get these posts...

function get_content($id = '') {

    if($id != ""):
        $id = mysql_real_escape_string($id);
        $sql = "SELECT * from cms_content WHERE id = '$id'";
        $return = '<p><a href="http://www.finalyearondesk.com/">Go back to Home page</a></p>';
        echo $return;

    else:
        $sql = "select * from cms_content ORDER BY id DESC";

    endif;

    $res = mysql_query($sql) or die(mysql_error());

    if(mysql_num_rows($res) != 0):

        while($row = mysql_fetch_assoc($res)) {
            echo '<h1><a href="index.php?id=' . $row['id'] . '">' . $row['title'] . '</a></h1>';
            echo '<p>' . "By: " . '<font color="orange">' . $row['author'] . '</font>' . ", Posted on: " . $row['date'] . '<p>';
            echo '<p>' . $row['body'] . '</p><br />';
        }

    else:

        echo '<p>We are really very sorry, this page does not exist!</p>';

    endif;
}

And I am using this code to dispaly it on my index.php page...

<?php
    if (isset($_GET['id'])) :
        $obj -> get_content($_GET['id']);
    else :
        $obj -> get_content_summary();
    endif;
?>

Any suggestions how to do this? And can we do this by using .htaccess?

  • 写回答

1条回答 默认 最新

  • donglei2022 2011-09-24 06:44
    关注

    The unfortunate thing about using mod_rewrite is that the data you are supplying in the form of a url is not the best way to query a database. But none the less you have year, month, day and title variables so you will need to rewrite your get_content function to query soomething like (depending on how you date is stored in the database.):

    select * from cms_content 
       WHERE date='mktime(0,0,0,$month,$day,$year)'
       AND title='$title'
     ORDER BY id DESC
    

    .htaccess would be something like:

    RewriteRule ^(.*)/(.*)/(.*)/(.*)$  index.php?year=$1&month=$2&day=$3&title=$4
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端