【问题标题】:how to manage content using php and mysql如何使用 php 和 mysql 管理内容
【发布时间】:2014-03-07 04:10:35
【问题描述】:

谁能告诉我如何管理网站中的帖子等内容。我可以将帖子内容存储在 mysql 中并检索它们,还是我必须做一些不同的事情。

我正在创建一个网站,用户可以在其中发布他们的文章。因此,为了实现这一点,我可以使用 mysql 作为我的数据库和 php 来处理信息并将其显示在网站上。

我对如何动态生成指向这些文章的链接感到困惑的另一件事,例如 http://www.example.com/first-article.php

http://www.example.com/second-article.php

first-article.php 这样的文件也会在我的网站文件夹中创建。如果是这样,几年后它不会造成混乱,那时您将拥有大约 10,000 个帖子,这意味着 10,000 个 php 文件。

我不想使用 joomla、drupal 或 wordpress 等内容管理系统。请指导我如何实现上述那些。

【问题讨论】:

  • 这个问题太宽泛了,基本上是基于意见的。我认为您必须阅读 PHP MySQL 开发的基础知识以及动态链接阅读 Web 服务器的 URL-rewrite。谷歌将是一个更好的地方来提出这个问题。
  • 我认为对于 cms joomla 来说是最好的。去joomla.org/download.html 下载吧。安装它。

标签: php mysql content-management-system


【解决方案1】:

首先,您需要创建一个表来存储所有数据。

让我们开始做类似的事情:

articles
--------
id_article (integer) (auto_increment) (primary key)
title (varchar 250)
author (varchar 250)
date (date)
content (text)

您可以在自己喜欢的数据库管理工具中轻松创建此表。

如果你没有,我强烈推荐 [mysql workbench][1],它很棒(你可以从一开始就考虑使用一个好的工具,而不是那个糟糕的 phpMyAdmin...)。

id_article 是一个号码,可以帮助您识别您的文章。 不用设置,从1开始,自动递增。

然后让我们专注于 php 脚本。 我建议3:articles.php显示所有文章,article_detail.php显示一篇完整文章,article_edit.php编辑一篇文章。

请注意,我不会给你 php 代码,否则你可能学不到很多东西。我只会给你伪代码,如果你遇到困难需要帮助,请告诉我。

//articles.php  //<---this is a comment
//first let's prepare a basic html page
<html>
    <head>
        <title>Article list</title>
    </head>
<body>
<h3>Articles list :</h3>
<ul>
<?php
//connect to mysql server + select database using [mysqli][2]
//execute the following query : "Select * from articles" with [mysqli_query][3]
//loop on your resultset using while and [mysqli_fetch_array()][4]
    //in your loop, echo the <li></li> that will populate your <ul>
    //Note, to create the links use : echo "<a href='./article_detail.php?id=".$row['id_article']."'>".$row['title']."</a>";
?>
</ul>
</body>
</html>

这是我们的第一个脚本。 您可以通过手动在表中创建一些行来尝试articles

//article_detail.php
//Note that there isn't much new in there
//First your html
<html>
    <head>
        <title>Article detail</title>
    </head>
    <body>
<?php
//a new thing are the http variables.
//we will be using [$_GET][5]
$id_article = (int) $_GET['id_article']; //remember to cast your integers as int in order to prevent SQL Injection
//connect to the database.
//use mysqli_query to execute : "Select * from articles where id_article='$id_article'"
//use fetch_array() to get the row.
//display the row the way you want using html.
</html>

测试完后,就可以继续编写最终脚本了。

这将采用整数作为$_GET参数:'id_article'(如果没有参数,那么我们将不得不使用mysql创建一篇新文章。 它还可以选择使用$POST 参数与titleauthorcontent

//article_edit.php
<html><head><title>Edit an article</title></head>
<body>

<?
if(count($_POST)){
//POST request, get the parameters from it, and update the row using $_GET['id_article']
//remember to use htmlentities() and addslashes() on author, title and content, so your script is bullet-proof to XSS and SQL Injection
echo "<h3 style='color:green;text-align:center;'>Article updated!</h3>";
}
if(isset($_GET['id_article'])){
    //We are editing an existing article : gather its content for edition
}else{
    //New article, create an empty row, and select it.
}
//after this if statement you should have '$id_article' and an array '$article' which contains the article data

?>

<form method='post' action='./article_edit.php?id_article=<?=$id_article?>'>
Author : <input size='50' name='article_author' value='<?=$article['author']?>'/><br/>
Title  : <input size='100' name='article_title' value='<?=$article['title']?>'/><br/>
Content : <textarea name='article_content'><?=$article['content']?></textarea><br/>
<input type='submit' value='Send'/>
</form>
</body>
</html>

你去。

[var_dump()][6] 是一个很好的 php 函数,如果我可以友好地命名的话,它基本上可以显示变量的内容。非常好的工具。

接下来要做什么?

-保存文章的创建日期。

-使用包含并集中您的公共源代码(例如:mysql_connect、html headers...)

-添加登录页面,并仅授权会员编辑。

-安装像 tiny_mce 这样的文本编辑器来编辑文章。

-...

玩得开心。

1:http://dev.mysql.com/downloads/tools/workbench/

2:http://php.net/manual/en/mysqli.construct.php

3:http://php.net/mysqli_query

4:http://php.net/manual/en/mysqli-result.fetch-array.php

5:http://php.net/manual/en/reserved.variables.get.php

6:http://php.net/manual/en/function.var-dump.php

【讨论】:

  • Loic:非常感谢,你给了我从这里开始的所有信息,再次感谢,我明白了,我正在寻找什么。你太棒了。
  • 哦别说了,我会脸红的。
【解决方案2】:

虽然我同意这个问题相当广泛,但也许看看像 Laravel 这样的 PHP 框架可能会对你有所帮助http://laravel.com

【讨论】:

    【解决方案3】:

    是的,你可以使用 php 和 mysql 来实现你想要的系统。

    您可以在 mysql 数据库中为您的帖子创建一个表,例如 tbl_articles,其中包含与文章相关的所有数据,例如其 ID、标题、内容、日期等。 接下来,您可以创建一个 php 页面,例如 display_article.php,它根据 url 中提供的 id 显示每篇文章。然后你的网址可以是http://example.com/display_article.php/first-article

    这只是一个建议。可以有其他更好的方法来做到这一点。

    希望对你有帮助。

    【讨论】:

    • 谢谢 Manish,很有帮助
    【解决方案4】:

    “我不想使用 joomla、drupal 或 wordpress 等内容管理系统。请指导我如何实施上述那些。”

    您所描述的本质上是编写自己的 CMS。您需要以比现在更高的水平学习 PHP 和 MySQL。 “如何编写我自己的 CMS”对于 SO 来说可能是一个太大的问题。

    网络上有一些指南可能更适合您的工作。试试:

    [http://css-tricks.com/php-for-beginners-building-your-first-simple-cms/][1]
    [http://www.elated.com/articles/cms-in-an-afternoon-php-mysql/][2]
    

    请投票。 =)

    【讨论】:

    • 另一个教程:[frasq.org/en/manual/write-a-cms-in-php/controlling-actions][3] 或者,如果 Drupal 和 Wordpress 对您的需求来说太大了,您可以下载一个已经存在的超轻量级 CMS:[queness.com/post/14579/6-lightweight-flexible-php-cms][4]
    • 谢谢,我知道 php 和 mysql,会花更多时间学习高级 php,对不起,我不能投票,因为我没有 15+ 的声誉
    • 能否请您提及有关 PHP 和 MySql 的特定主题,我应该学习这些主题以便能够为我自己的网站创建 CMS
    • 我提到的所有三个站点都是特定于 PHP 和 MySQL 用于创建 CMS。获得声望时请点赞。 =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多