Wow very descriptive. Anyway, for those who don't know, he wants help with programming a news update system in PHP.
So you should have a database structure that looks something like this:
|-----------------------------------| | id | title | author | post | time | |-----------------------------------| | | | | | | | | Your data here | | | | | | | | |-----------------------------------|
id will be a unique identification number for each row. This should be auto increment and the primary key. Whatever you do, don't use the time the post was added as the ID number.
title is will store the title for each row and it should be varchar with a 150-200 character length.
author will store the author of the post. For beginners it's fine to store the text name of the author like "Johnny" for example but you can also store the user's unique identification number and then grab the username from the users database before displaying it on pages. For this tutorial I will consider the text version of the author's name is stored in this column.
post will be the row which contains the body of the news post. This can be a simple text column.
time will contain the timestamp for this post and the column should be integer with 10-11 characters length.
So that's a simple database which could power a news update system. Now some PHP code.
This is an example of a simple page that will display news posts.
<?php //We need the database connection information. require("connect.php");
//We need to query the database for all news updates in the database. To order from newest to oldest, we order by the time they were added decending. $query = mysql_query("SELECT * FROM news ORDER BY time DESC") or die(mysql_error()); while($row = mysql_fetch_array($query)) { $id = $row['id']; $title = htmlspecialchars($row['title']); $author = $row['author']; $post = htmlspecialchars($row['post']); $time = $row['time'];
//We want to add line breaks to the body of the news post. $post = nl2br($post);
//We need to turn the date into a human-readable format. $date = date("M d, g:i a", $time);
//We can now display the news updates. echo "<div>\n"; echo "<h2>$title</h2>\n"; echo $post; echo "<p>Written by $author on $date</p>\n"; echo "</div>"; } ?>
Okay so I left some comments in the code and you may noticed that I never actually did anything with the $id variable but you may need to use that in more advanced news update systems.
Okay so here's a simple page which will allow you to add a news post and it has no styling.
<?php //We need the database connection information. require("connect.php");
//This is a function I am using to add slashes to prevent SQL injection. function make_mysql_safe($var) { if(!get_magic_quotes_gpc()) { $var = mysql_real_escape_string($var); } return $var; }
//If the form was submitted then execute the following. if ($_SERVER['REQUEST_METHOD'] == "POST") { $title = make_mysql_safe(trim($_POST['title'])); $post = make_mysql_safe(trim($_POST['post']));
//We need the current time for the news update posted time. $time = time();
//You can do other stuff here like make sure a field is a certain length, etc. but to keep this simple, I will only check to make sure there is a title. if(strlen($title) < 1) { //The user has not entered a title. We will stop the script and dispaly an error. die("<b>Error:</b> Post requires a title."); }
//All input has been validated to let's insert this into the database and display a succes message. mysql_query("INSERT INTO news (title, author, post, time) VALUES ('$title', '$author', '$post', '$time')") or die(mysql_error()); echo "Your news update has been created!"; } else { //The form has not been submitted so we will display the form to the user.
You may noticed that I never actually set the $author variable so you would need a way to grab the author and insert that into the datebase with the query. Also I never defined an id for the row either. Because it's auto increment, MySQL will automatically do that when you insert the row.
This code is all untested but it should give you a good idea on how to code a news update system.
Are you saying you want the login form to show on every page on your site? It should work if you put the HTML form code on every page then set the form action to your login.php page. Try something like this:
Wow very descriptive. Anyway, for those who don't know, he wants help with programming a news update system in PHP.
So you should have a database structure that looks something like this:
|-----------------------------------|| id | title | author | post | time |
|-----------------------------------|
| | | | | |
| | Your data here | |
| | | | | |
|-----------------------------------|
id will be a unique identification number for each row. This should be auto increment and the primary key. Whatever you do, don't use the time the post was added as the ID number.
title is will store the title for each row and it should be varchar with a 150-200 character length.
author will store the author of the post. For beginners it's fine to store the text name of the author like "Johnny" for example but you can also store the user's unique identification number and then grab the username from the users database before displaying it on pages. For this tutorial I will consider the text version of the author's name is stored in this column.
post will be the row which contains the body of the news post. This can be a simple text column.
time will contain the timestamp for this post and the column should be integer with 10-11 characters length.
So that's a simple database which could power a news update system. Now some PHP code.
This is an example of a simple page that will display news posts.
<?php//We need the database connection information.
require("connect.php"); //We need to query the database for all news updates in the database. To order from newest to oldest, we order by the time they were added decending.
$query = mysql_query("SELECT * FROM news ORDER BY time DESC") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$title = htmlspecialchars($row['title']);
$author = $row['author'];
$post = htmlspecialchars($row['post']);
$time = $row['time'];
//We want to add line breaks to the body of the news post.$post = nl2br($post);
//We need to turn the date into a human-readable format.$date = date("M d, g:i a", $time);
//We can now display the news updates.echo "<div>\n";
echo "<h2>$title</h2>\n";
echo $post;
echo "<p>Written by $author on $date</p>\n";
echo "</div>";
}
?>
Okay so I left some comments in the code and you may noticed that I never actually did anything with the $id variable but you may need to use that in more advanced news update systems.
Okay so here's a simple page which will allow you to add a news post and it has no styling.
<?php
//We need the database connection information.
require("connect.php"); //This is a function I am using to add slashes to prevent SQL injection.
function make_mysql_safe($var) {
if(!get_magic_quotes_gpc()) {
$var = mysql_real_escape_string($var);
}
return $var;
} //If the form was submitted then execute the following.
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$title = make_mysql_safe(trim($_POST['title']));
$post = make_mysql_safe(trim($_POST['post']));
//We need the current time for the news update posted time.
$time = time();
//You can do other stuff here like make sure a field is a certain length, etc. but to keep this simple, I will only check to make sure there is a title.
if(strlen($title) < 1) {
//The user has not entered a title. We will stop the script and dispaly an error.
die("<b>Error:</b> Post requires a title.");
}
//All input has been validated to let's insert this into the database and display a succes message.
mysql_query("INSERT INTO news (title, author, post, time) VALUES ('$title', '$author', '$post', '$time')") or die(mysql_error());
echo "Your news update has been created!";
}
else
{
//The form has not been submitted so we will display the form to the user.
echo<<<form
<h2>Add News</h2>
<form method="post" action="">
Title: <input type="text" name="title">
<br>
Post: <textarea name="post" rows="10" cols="8"></textarea>
<br>
<input type="submit" name="submit" value="Add News">
</form>
form;
}
?>
You may noticed that I never actually set the $author variable so you would need a way to grab the author and insert that into the datebase with the query. Also I never defined an id for the row either. Because it's auto increment, MySQL will automatically do that when you insert the row.
This code is all untested but it should give you a good idea on how to code a news update system.
How would I implement the login into a page with the PHP on another file? Like how the IC and IG do it. Or is the PHP just in the header of the HTML?
Are you saying you want the login form to show on every page on your site? It should work if you put the HTML form code on every page then set the form action to your login.php page. Try something like this:
<form method="post" action="LOGIN_PAGE_URL">Username: <input type="text" name="username"><br>
Password: <input type="password" name="pass"><br>
<input type="submit" name="login" value="Login">
</form>