|
| Web Hosting Deals | Holiday Logo Design | Webcam Chat | Website Header Templates | Register domain | Search Engine Optimisation | Web Hosting |
|
|||||||
| 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. |
![]() |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
||||
|
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 Code:
Lets get it line by line PHP Code:
Code:
<a href="news.php?action=view&id=$ID">$title</a> 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"; - 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!
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png PointServ.co.uk - One of the UK's cheapest webhosting companies. |
|
||||
|
(Part 2 of the tutorial)
PHP Code:
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']) { - 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") - Now saying, if the post isent edit, but the action is edit then its going to do the above code. PHP Code:
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:
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"> 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. |
|
||||
|
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. |
|
|||
|
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 |
|
|||
|
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:
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|