Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-31-2007, 07:50 AM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
The CMS (#4) PHP

Right so in this one, we are going to create our news reader first, and then our edit/delete news.

As allways i will go through it line by line for you, just so you don't get confuzled!

I am going to give you the entire code of news.php this should be in your root directory.

PHP Code:
<?php
//The following PHP script queries and displays the contents of the MySQL table.

//Connecting to the MySQL database
include("includes/connect.php");

//Should we show a single item or a list?
if($_GET['action'] == "view") {
//Display a single result.
$id $_GET['id'];
//The MySQL query. Select all from the table news where the ID equals the id sent in URL.

$query "SELECT * FROM news WHERE ID='$id'";
//Executing the query.
$result mysql_query($query) or die(mysql_error());
//Displaying the results of the query.
while ($row mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.

extract($row);
//nl2br() translates all newlines ('n') as HTML tags.
$content nl2br($content);
echo 
"<table><tr><td><strong>$title</strong></td></tr>
<tr><td><small>Written by $author on $date</small></td></tr>

<tr><td>$content</td></tr>"
;
}
} else {
//Since we're not displaying a single result,
//we're going to display a list of results.
//The MySQl query. Selects all from the table news with a limit of 5 results.

$query "SELECT * FROM news ORDER BY ID DESC LIMIT 5";
//Execute the query.
$result mysql_query($query) or die(mysql_error());
while (
$row mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.
extract($row);

//nl2br() translates all newlines ('n') as HTML tags.
$content nl2br($content);
echo 
"<table><tr><td><strong><a href="news.php?action=view&id=$ID">$title</a></strong></td></tr>
<tr><td><small>Written by $author on $date</small></td></tr>
<tr><td>$content</td></tr>"
;

}
}

?>
Lets get onto the first part shall we!


PHP Code:
//Should we show a single item or a list?
if($_GET['action'] == "view") {
//Display a single result.
$id $_GET['id'];
//The MySQL query. Select all from the table news where the ID equals the id sent in URL.

$query "SELECT * FROM news WHERE ID='$id'";
//Executing the query.
$result mysql_query($query) or die(mysql_error());
//Displaying the results of the query.
while ($row mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.

extract($row);
//nl2br() translates all newlines ('n') as HTML tags.
$content nl2br($content);
echo 
"<table><tr><td><strong>$title</strong></td></tr>
<tr><td><small>Written by $author on $date</small></td></tr>

<tr><td>$content</td></tr>"
;

Right this code is if a user wants to display a specific news post.

Lets get it line by line

PHP Code:
if($_GET['action'] == "view") { 
Means that it only does the stuff in between the { & } tags, if someone clicks this hyperlink at the bottom of the news:

Code:
<a href="news.php?action=view&id=$ID">$title</a>
Look @ this bit - action=view - then look at this if statement

if($_GET['action'] == "view") - You see whats going on here?

Its saying "With the action view, i want you to display the row with the ID of $id

while ($row = mysql_fetch_array($result)) { - Again we have seen this ebfore, puts $result information into an array.


$id = $_GET['id']; - Gets the stuff inside $id


$query = "SELECT * FROM news WHERE ID='$id'"; - So get all the information inside the row with the id of $id

$result = mysql_query($query) or die(mysql_error()); - You have seen this before, this is the actual query

extract($row); - This is a vaugely complicated bit of code, but supremely simplified it means:

Baisicly does the same job as doing this (which we have done in our previous tutorials)

$row['name'] - Rememer that?

$content = nl2br($content); - This is a little simpler, baisicly for a new line in PHP you do \n so what this does, is make those into <br> tags

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_

So the above code only applys if we are viewing a specific post.

But what if we are not?

Then we need the code below that.

PHP Code:
$query "SELECT * FROM news ORDER BY ID DESC LIMIT 5";
//Execute the query.
$result mysql_query($query) or die(mysql_error());
while (
$row mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.
extract($row);

//nl2br() translates all newlines ('n') as HTML tags.
$content nl2br($content);
echo 
"<table><tr><td><strong><a href="news.php?action=view&id=$ID">$title</a></strong></td></tr>
<tr><td><small>Written by $author on $date</small></td></tr>
<tr><td>$content</td></tr>"
;

}

Now if you look this code is baisicly the same as the previously explained code, so i will merely glance over it.

$query = "SELECT * FROM news ORDER BY ID DESC LIMIT 5"; - Gets all the information from the news table, with the limit of 5.

$result = mysql_query($query) or die(mysql_error()); - Puts the query into a variable.


while ($row = mysql_fetch_array($result)) { - Puts the information into an array.

extract($row); - ^^^^ Explained above ^^^^^


$content = nl2br($content); - Transfers \n into <br>

<a href="news.php?action=view&id=$ID">$title</a> - Same as above really, this is the code that lets us display just a single news post.



-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_


So now we have completely finished "viewing" the news, we are now going to learn how to edit it.

This is much more complicated, so keep your thinking cap on!

Attached Files
File Type: zip PixN.zip (5.8 KB, 11 views)
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #2 (permalink)  
Old 03-31-2007, 07:51 AM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
(Part 2 of the tutorial)

PHP Code:
<?php
session_start
();
if(
$login_username=="") {
Header("Location: login.php");
} else {
//The following PHP script allows you to edit the
//contents of your MySQL table.
//Connecting to the MySQL database
include("../includes/connect.php");

//Should we show a single item or a list?
if($_POST['edit']) {
//Simplifying the variables.

$id $_POST['id'];
$title $_POST['title'];
$author $_POST['author'];
$date $_POST['date'];

$content $_POST['content'];

//htmlspecialchars() converts special characters into HTML entities.
$title htmlspecialchars($title);
$author htmlspecialchars($author);


//The MySQL query which will update the content in the table.
$query "UPDATE news SET title = '$title', author = '$author', date = '$date', content = '$content' WHERE ID = '$id'";
//Execute the query.
$result mysql_query($query) or die(mysql_error());
echo 
"<center><strong>News item modified!</strong></center>";


} elseif(
$_GET['action'] == "edit") {
//Display a single result.
$id $_GET['id'];
//The MySQL query. Select all from the table news where the ID equals the id sent in URL.

$query "SELECT * FROM news WHERE ID='$id'";
//Executing the query.
$result mysql_query($query) or die(mysql_error());
//Displaying the results of the query.
while ($row mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.

extract($row);
?>
<form method="post" action="edit-news.php">
<table align="center">
<tr><td align="right">Title:</td><td><input type="text" name="title" value="<?php echo "$title"?>" maxlength="250" /></td></tr>
<tr><td align="right">Author:</td><td><input type="text" name="author" value="<?php echo "$author"?>" maxlength="250" /></td></tr>
<tr><td align="right">Date:</td><td><input type="text" name="date" value="<?php echo "$date"?>" maxlength="10" /></td></tr>
<tr><td align="right">Content:</td><td><textarea name="content" cols="50" rows="10"><?php echo "$content"?></textarea></td></tr>

<tr><td> </td><td><input type="hidden" name="id" value="<?php echo "$ID"?>" /><input type="submit" name="edit" value="Modify" /><input type="reset" name="reset" value="Reset" /></td></tr>
</table>
</form>

<?php
}
} else {
//Since we're not displaying a single result,
//we're going to display a list of results.

//The MySQl query. Selects all from the table news.
$query "SELECT * FROM news ORDER BY ID DESC";
//Execute the query.
$result mysql_query($query) or die(mysql_error());
while (
$row mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.

extract($row);
echo 
"<table><tr><td><strong><a href="edit-news.php?action=edit&id=$ID">$title</a></strong></td></tr>
<tr><td><small>Written by $author on $date</small></td></tr>

<tr><td><strong><a href="
delete-news.php?id=$ID">DELETE</a></strong></td></tr>"
}

?>

Wow, what allot of code.

So lets get straight into it.

As with the previous page, we start off with some if statements, to see what action the user is doing.

PHP Code:
if($_POST['edit']) {
//Simplifying the variables.

$id $_POST['id'];
$title $_POST['title'];
$author $_POST['author'];
$date $_POST['date'];

$content $_POST['content'];

//htmlspecialchars() converts special characters into HTML entities.
$title htmlspecialchars($title);
$author htmlspecialchars($author);


//The MySQL query which will update the content in the table.
$query "UPDATE news SET title = '$title', author = '$author', date = '$date', content = '$content' WHERE ID = '$id'";
//Execute the query.
$result mysql_query($query) or die(mysql_error());
echo 
"<center><strong>News item modified!</strong></center>"
So lets get into line by line as usual.

if($_POST['edit']) { - If their action is "edit" then it parses the following code.


$id = $_POST['id'];
$title = $_POST['title'];
$author = $_POST['author'];
$date = $_POST['date'];

$content = $_POST['content'];

- Just getting the variable contents.


$title = htmlspecialchars($title);
$author = htmlspecialchars($author); - The same as adding the news, to edit it we need to run it through htmlspecialchars.

$query = "UPDATE news SET title = '$title', author = '$author', date = '$date', content = '$content' WHERE ID = '$id'";

So what this is saying is:

"Update the table news, saying the row "title" is replaced by the contensts of the $title variable, and same wih author, date & content.


$result = mysql_query($query) or die(mysql_error());
echo "<center><strong>News item modified!</strong></center>";

running the result, and telling the user it has been modified.



NEXT!


PHP Code:
} elseif($_GET['action'] == "edit") {
//Display a single result.
$id = $_GET['id'];
//The MySQL query. Select all from the table news where the ID equals the id sent in URL.

$query = "SELECT * FROM news WHERE ID='$id'";
//Executing the query.
$result = mysql_query($query) or die(mysql_error());
//Displaying the results of the query.
while ($row = mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.

extract($row);
?>
<form method="post" action="edit-news.php">
<table align="center">
<tr><td align="right">Title:</td><td><input type="text" name="title" value="<?php echo "$title"?>" maxlength="250" /></td></tr>
<tr><td align="right">Author:</td><td><input type="text" name="author" value="<?php echo "$author"?>" maxlength="250" /></td></tr>
<tr><td align="right">Date:</td><td><input type="text" name="date" value="<?php echo "$date"?>" maxlength="10" /></td></tr>
<tr><td align="right">Content:</td><td><textarea name="content" cols="50" rows="10"><?php echo "$content"?></textarea></td></tr>

<tr><td> </td><td><input type="hidden" name="id" value="<?php echo "$ID"?>" /><input type="submit" name="edit" value="Modify" /><input type="reset" name="reset" value="Reset" /></td></tr>
</table>
</form>
<?php
}
Rightey then!

elseif($_GET['action'] == "edit") - Now saying, if the post isent edit, but the action is edit then its going to do the above code.

PHP Code:
$id $_GET['id']; 
Same as in viewing our news, we are getting the ID, to know which row we are dealing with.

Once again the same kind of querys.

$query = "SELECT * FROM news WHERE ID='$id'";

And once again using extract()

And that ends the explaniation of editing a specific post.

PHP Code:
}
} else {
//Since we're not displaying a single result,
//we're going to display a list of results.

//The MySQl query. Selects all from the table news.
$query "SELECT * FROM news ORDER BY ID DESC";
//Execute the query.
$result mysql_query($query) or die(mysql_error());
while (
$row mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.

extract($row);
echo 
"<table><tr><td><strong><a href="edit-news.php?action=edit&id=$ID">$title</a></strong></td></tr>
<tr><td><small>Written by $author on $date</small></td></tr>

<tr><td><strong><a href="
delete-news.php?id=$ID">DELETE</a></strong></td></tr>"
}

?> 
The above code is the "default" code as such.

I have explained all that happens in that part, in different sections of this tutorial, and its not nearly 6:00AM so i think i won't bother

The only bit worth mentioning is:

Code:
<a href="delete-news.php?id=$ID">
Again tho, i have explained that type of parsing earlier on!

Here endeth this mamoth part of the tutorial.


Next post is delete news, add user & edit user.

Another mamoth one :'(

But i hope you have learned.

I have included a copy of the files at the moment, for reference.


Cheers!

Base - Peace out.
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #3 (permalink)  
Old 03-31-2007, 07:53 AM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
Just to prove how mamoth that was look @ this.


" 1. The text that you have entered is too long (14041 characters). Please shorten it to 10000 characters long."

ROFL

over 10,000 charicters.

And spread over 400 lines.

Don't you just love my dedication!

lol

Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #4 (permalink)  
Old 04-24-2007, 05:28 AM
Junior Member
 
Join Date: Apr 2007
Posts: 4
chixor1 is on a distinguished road
Sorry im having trouble with this too

Code:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/chixor/public_html/freshstart/edit-news.php on line 73
Only changes made were to the session and the include at the top
Reply With Quote
  #5 (permalink)  
Old 05-19-2007, 02:17 PM
Junior Member
 
Join Date: May 2007
Location: Croatia
Posts: 20
Jabba is on a distinguished road
Send a message via MSN to Jabba
thanksss!
Reply With Quote
  #6 (permalink)  
Old 07-02-2007, 04:54 PM
Junior Member
 
Join Date: Jul 2007
Posts: 20
speter is on a distinguished road
I appreciate your work, but It contains a lot of mistakes. Did you checked what you have written here?

here is the correct edit-news.php
PHP Code:
<?php
session_start
();
if(
$_SESSION['login_username']=="") {
Header("Location: login.php");
} else {
//The following PHP script allows you to edit the
//contents of your MySQL table.
//Connecting to the MySQL database
include("../includes/connect.php");

//Should we show a single item or a list?
if($_POST['edit']) {
//Simplifying the variables.

$id $_POST['id'];
$title $_POST['title'];
$author $_POST['author'];
$date $_POST['date'];

$content $_POST['content'];

//htmlspecialchars() converts special characters into HTML entities.
$title htmlspecialchars($title);
$author htmlspecialchars($author);


//The MySQL query which will update the content in the table.
$query "UPDATE news SET title = '$title', author = '$author', date = '$date', content = '$content' WHERE ID = '$id'";
//Execute the query.
$result mysql_query($query) or die(mysql_error());
echo 
"<center><strong>News item modified!</strong></center>";


} elseif(
$_GET['action'] == "edit") {
//Display a single result.
$id $_GET['id'];
//The MySQL query. Select all from the table news where the ID equals the id sent in URL.

$query "SELECT * FROM news WHERE ID='$id'";
//Executing the query.
$result mysql_query($query) or die(mysql_error());
//Displaying the results of the query.
while ($row mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.

extract($row);
?>
<form method="post" action="edit-news.php">
<table align="center">
<tr><td align="right">Title:</td><td><input type="text" name="title" value="<?php echo "$title"?>" maxlength="250" /></td></tr>
<tr><td align="right">Author:</td><td><input type="text" name="author" value="<?php echo "$author"?>" maxlength="250" /></td></tr>
<tr><td align="right">Date:</td><td><input type="text" name="date" value="<?php echo "$date"?>" maxlength="10" /></td></tr>
<tr><td align="right">Content:</td><td><textarea name="content" cols="50" rows="10"><?php echo "$content"?></textarea></td></tr>

<tr><td> </td><td><input type="hidden" name="id" value="<?php echo "$ID"?>" /><input type="submit" name="edit" value="Modify" /><input type="reset" name="reset" value="Reset" /></td></tr>
</table>
</form>

<?php
}
} else {
//Since we're not displaying a single result,
//we're going to display a list of results.

//The MySQl query. Selects all from the table news.
$query "SELECT * FROM news ORDER BY ID DESC";
//Execute the query.
$result mysql_query($query) or die(mysql_error());
while (
$row mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.

extract($row);
echo 
"<table><tr><td><strong><a href=edit-news.php?action=edit&id=$ID>$title</a></strong></td></tr>
<tr><td><small>Written by $author on $date</small></td></tr>

<tr><td><strong><a href=delete-news.php?id=$ID>DELETE</a></strong></td></tr>"
;
}  }  }
?>
Reply With Quote
  #7 (permalink)  
Old 07-02-2007, 08:58 PM
Junior Member
 
Join Date: Jul 2007
Posts: 20
andrepcg is on a distinguished road
very useful!

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On