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

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

报告相同问题?

悬赏问题

  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景