news systeem

From: Date: Fri, 06 Jul 2001 09:25:06 +0000
Subject: news systeem
Groups: php.test 
Request: Send a blank email to php-test+get-188@lists.php.net to get a copy of this message
============ reactions.php ============ <?php # This file handles the showing and adding of reactions # to articles. It has no own layout because it will be # included by showarticle.php if ($react_title && $react_name && $react_message) { # There already is a connection to the db so that part can be skipped here $message = nl2br ($message); $date = time(); $sql = "INSERT INTO reactions VALUES ('', '$art_id', '$date', '$react_name', '$react_message', '$react_title')"; $add_reaction = mysql_query ($sql); if (!$add_reaction) { echo ("Error while adding reaction"); } } # The following will be executed anyway $get_reactions = mysql_query ("SELECT * FROM reactions WHERE art_id='$art_id' ORDER BY date DESC"); $num_of_reactions = mysql_num_rows ($get_reactions); if ($num_of_reactions != 0) { while ($react_info = mysql_fetch_array ($get_reactions)) { $date = date ("m-d-y H:i", $react_info['date']); $message = htmlspecialchars ($react_info['message']); echo ("<tr><td bgcolor=#aaaaaa><b>".$react_info['title']."</b> by <i>".$react_info['name']."</i> on ".$date."</td></tr> <tr><td bgcolor=#dddddd>$message</td></tr>"); } } else { echo ("<tr><td bgcolor=#aaaaaa>No reactions have been added yet</td></tr>"); } # Now the part to add your own reaction ?> <tr><td bgcolor=#aaaaaa><b>Add your reaction</b></td></tr> <tr><td bgcolor=#dddddd> <form method=post action=showarticle.php> <input type=hidden name=art_id value=<?php echo ($art_id); ?> Your name: <i>(max: 25 chars)</i><BR> <input type=text name='react_name' maxlength=25><BR><BR> Message: <BR> <textarea name='react_message' cols=40 rows=10></textarea><BR><BR> <B>Let Op</B> <br> Alle vormen van scripting zijn niet toegestaan. <BR> <br> <input type=submit value='Add reaction'> </form> </td></tr> <?php ?> This appears by the first vield: ".$react_info['title']." by ".$react_info['name']." on ".$date." $message"); } } else { echo ("No reactions have been added yet"); } # Now the part to add your own reaction ?> Add your reaction ============= template.php ============= <html> <?php # Much used functions are settled here to # be included in every file. Functions for # layout and mysql connection $mysql_connection; # contains link to mysql connection function begin_page ($page_title) { # Function to start layouting. The only argument # is de title for the page } # end begin_page function end_page () { # Function to finish layouting, you can add text # which has to be displayed at every page here. # Like: all material on this site is copyrighted by ... die(); } # end end_page() function connect_with_db () { # Connect with db global $mysql_connection; # Required data to connect with database $host = "***"; $user = "****"; $pass = "*****"; $table = "***"; # Connect with server and select database $mysql_connection = mysql_connect ($host, $user, $pass) or die ("Connection failed"); mysql_select_db ($table) or die ("Selecting database failed"); } # End connect_with_db() function disconnect_with_db() { # Kill connection with db global $mysql_connection; mysql_close ($mysql_connection); # disconnecting } # end disconnect_with_db() ?> <html> Something forgotten? ========== addarticle.php ========== <?php # This file adds articles to the database # but first includes template.php for layout # and db connection include ("template.php"); begin_page ("Add article"); # Call function to start layout, the argument is the title if ($content && $title) { # If the content and title are entered connect_with_db(); # Connect with mysql db $sql = "INSERT INTO articles VALUES ('', '$content', '$title')"; $add_article = mysql_query ($sql); # inserting article if (!$add_article) { # if something went wrong while adding article echo ("Error while adding article. <a href="javascript:history.go(-1)>Back</a>"); } else { echo ("Article succesfully added. <a href="addarticle.php>Add another</a>"); } } # end 'if ($content != 0 && $title != 0)' else { # if nothin' is entered yet, write form } ?> <table border=0 width=100% style='border: 1px black solid' cellspacing=0> <tr> <td bgcolor=#aaaaaa align=center><b>Bericht toevoegen</b></td> </tr> <tr> <td align=center bgcolor=#dddddd> <form method=post> Title: <i>(Max: 50 chars)</i> <BR> <input type="text" name="title" size=50 maxlength=50 value=Titel_Invoeren ?><BR><BR> Content: <i>(Voor layout moet je HTML gebruiken)</i><BR> <textarea name="content" cols=80 rows=14>Hier je nieuws bericht</textarea> <br><br><input type=submit value="Add article"> </form> </td> </tr> </table> <?php } # end else end_page(); # Finish layout ?> Most this script and reactions.php be connected to a database?? ========= showarticle.php ========= <?php include ("template.php"); # Because we want to enter the title of the article # between the title tags we've got to fetch this one # first. So let's connect with db if (!$art_id) { # This wil only be executed when no id is given begin_page("Article does not exist"); # make begin layout echo ("You did not specify an article id"); end_page(); # End page dies also, so script will stop here } # The code which comes now will only be executed when # an art_id is given connect_with_db(); # let's connect $fetch_art_info = mysql_query ("SELECT * FROM articles WHERE id='$art_id'"); # Checking how many articles were found # If it's an existing id, num will be 1, if not it'll be 0 $num = mysql_num_rows ($fetch_art_info); if ($num == 0) { begin_page("Article does not exist"); # make begin layout echo ("Article with id: ". $art_id . " does not exist"); end_page(); # End page dies also, so script will stop here } $art_info = mysql_fetch_array ($fetch_art_info); # Puts all article info into an array begin_page($art_info['title']); # Make begin layout # Draw table with all info ?> <table width=100% style='border: 1px black solid' cellspacing=0> <tr> <td bgcolor=#aaaaaa> <?php echo ($art_info['title']); ?> </td> </tr> <tr> <td bgcolor=#dddddd> <?php echo ($art_info['content']); ?> </td> </tr> <?php include ("reactions.php"); # Include file which handles the reactions echo ("</table>"); disconnect_with_db(); # Disconnect with db end_page(); # End page layout ?> This also wrong: disconnect_with_db(); # Disconnect with db end_page(); # End page layout ?>

« previous php.test (#188) next »