Atmega32
Atmega32

Reputation: 1

how to manage content using php and mysql

Can someone give me info about how to manage contents like post, in a website. Can I store post contents in mysql and retrieve them, or do I have to do something different.

I am making a website where user can post their articles. So to implement that can I use mysql as my database and php to process the information and show it on website.

One more thing I am confused about how to generate links to those articles dynamically, like http://www.example.com/first-article.php

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

So are these files like first-article.php going to be created in my website folder. If so isn't it going to create mess after some years, when you will be having like 10,000 post so that means 10,000 php files.

I don't want to use content management systems like joomla, drupal or wordpress. Please guide me how to implement those above mentioned.

Upvotes: 0

Views: 1842

Answers (4)

Slam
Slam

Reputation: 3325

"I don't want to use content management systems like joomla, drupal or wordpress. Please guide me how to implement those above mentioned."

What you are describing is essentially writing your own CMS. You need to learn PHP and MySQL at a level higher that you have right now. "How to write my own CMS" is probably too large of a question for SO.

There's a few guides on the web that might be better suited for what you are doing. Try:

[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]

Please vote. =)

Upvotes: 0

Loïc
Loïc

Reputation: 11933

First you'll need to create a table to store all your data.

Let's do something like that to begin :

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

You can create this table easily in your favorite database management tool.

If you don't have any, I highly recommend [mysql workbench][1] which is awesome (You may consider using a good tool from the beginning, not that shitty phpMyAdmin...).

id_article is a number which will help you to identify your articles. You don't have to set it, it starts from 1, and will auto increment.

Then let's focus on the php script. I suggest 3 : articles.php to display all articles, article_detail.php to display one full article, and article_edit.php to edit an article.

Note that I won't give you php code otherwise you may not learn a lot. I'll just give you pseudo-code, let me know if you are stuck and need help.

//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>

There is our first script. You can try it by manually creating some rows in the table 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>

Once you have tested this, you can move on the final script.

This one will take an integer as $_GET parameter : 'id_article' (if there is no parameter then we will have to create a new article using mysql. And it will also optionnaly take a $POST parameter with title, author and content.

//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>

There you go.

A good php function, if I could name a friendly one, is [var_dump()][6], it basically shows you the content of variables. Very good tool.

What to do next ?

-Save the creation date of the article.

-Use include and centralize your common source code (eg : mysql_connect, html headers...)

-Add a login page, and authorize editing only to members.

-Install a text editor like tiny_mce for editing articles.

-...

Have fun.

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

Upvotes: 2

Manish Shrestha
Manish Shrestha

Reputation: 96

Yes you can use php and mysql to implement your desired system.

What you can do is create a table in the mysql database for your posts like tbl_articles that holds all your data relating to an article such as its id, title, content, date, etc. Next you can create a php page such as display_article.php that displays each article based on the id provided in the url. Then your url can be in the form http://example.com/display_article.php/first-article

This is just a suggestion. There can be other better methods to do it.

Hope it helps.

Upvotes: 0

Rohan Peters
Rohan Peters

Reputation: 147

While i agree that the question is quite broad, perhaps a look at a PHP Framework like Laravel may help you http://laravel.com.

Upvotes: 0

Related Questions