douxi8119 2017-06-02 14:36
浏览 32
已采纳

使用我的自定义CMS创建页面

I have started creating a CMS today for my own personal use, for management purporses and also to teach myself a few things.

I have a section in my admin area where I can create/update/delete pages or posts which works great.

My question is how do I generate pages, and URL's from the information I have stored in my database?

Let's say for example my table "posts" has a few rows which contain "post_title", "post_content", "post_slug", etc. How do I generate a page from this information in my database (because it's obviously not creating pages based on PHP files), so I would have http://mywebsite.com/cool-new-post-slug.php (then I can get content by ID, etc)?

If someone can point me in the right direction that would be great. Thanks for your answer.

  • 写回答

1条回答 默认 最新

  • dspld86684 2017-06-02 15:03
    关注

    If I understand your question correctly, you'll need to retrieve all the slugs from the database first, then use them to construct your url.

    Assuming you're using mysqli:

    $query = "SELECT post_slug,post_title FROM posts";
    $posts = mysqli_query($con,$query);
    while($post = mysqli_fetch_assoc($posts)){
    
        echo "<a href=\"single.php?slug=" . $post["post_slug"] . "\">";
        echo $post["post_title"];
        echo "</a><br/>";
    
    }
    
    mysqli_free_result($posts);
    

    Then on single.php you'll use the GET variable post_slug to retrieve your data.

    in your .htaccess file you can use mod_rewrite to get pretty URLS as follows:

    RewriteEngine On
    RewriteBase /
    RewriteRule posts/(.+?)/?$ single.php?slug=$1
    

    this would show the url http://mywebsite.com/posts/cool-new-post/ your browser, but what it will really be accessing is single.php?slug=cool-new-post

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能