jamesjmann Posted February 4, 2011 Share Posted February 4, 2011 Before I posted in here about a content management system for posting news. I was wondering how I could make so that when you fill out a form and send all the data to the database, it creates a new page from a template and includes that data within it? I know I asked this before, and some mentioned the _get method, but im using _post to process the form data and have no idea where to integrate the _get method. Here's the form in question: <form action="http://www.djsmiley.net/cms/news/process.php" method="post" id="news"> <h1>Post New Article</h1> <p>Please fill out all of the following fields:</p> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="55" class="cmsNewsformText">Type*:</td> <td><font size="1"> <input name="type" type="text" class="Form1" size="50" /> </font></td> </tr> <tr> <td height="55" class="cmsNewsformText">News Topic/Title*: </td> <td><font size="1"> <input name="title" type="text" class="Form1" size="50" /> </font></td> </tr> <tr> <td height="55" class="cmsNewsformText">Username*:</td> <td><font size="1"> <input name="user" type="text" class="Form1" value="DJ Smiley" size="50" /> </font></td> </tr> <tr> <td height="55" class="cmsNewsformText">Url*:</td> <td><font size="1"> <input name="url" type="text" class="Form1" size="50" /> </font></td> </tr> <tr> <td height="55" class="cmsNewsformText">Message*:</td> <td><font size="1"> <textarea name="message" cols="43" rows="10" class="TextField1"></textarea> </font></td> </tr> <tr> <td height="55" class="cmsNewsformText"> </td> <td><font size="1"> <input name="Submit" type="submit" class="Button1" value="Submit" /> <input name="Submit2" type="reset" class="Button1" value="Reset" /> </font></td> </tr> </table> </form> and here's the code that sends the data from the forms to the database: <?php $user=$_POST['user']; $title=$_POST['title']; $message=$_POST['message']; $type=$_POST['type']; $url=$_POST['url']; mysql_connect("hostname", "username", "password") or die(mysql_error()); mysql_select_db("database") or die(mysql_error()); $sql = sprintf("INSERT INTO mynews (user, title, message, type, url) VALUES ('%s', '%s', '%s', '%s', '%s')", mysql_real_escape_string($user), mysql_real_escape_string($title), mysql_real_escape_string($message), mysql_real_escape_string($type), mysql_real_escape_string($url)); $result = mysql_query($sql); Print "The article has successfully been posted"; ?> can someone help or at least point me in the right direction? Quote Link to comment Share on other sites More sharing options...
trq Posted February 4, 2011 Share Posted February 4, 2011 What exactly is the problem? Quote Link to comment Share on other sites More sharing options...
jamesjmann Posted February 4, 2011 Author Share Posted February 4, 2011 What exactly is the problem? I want to be able to create new php pages every time I post a news article using the cms forms above. I remember you telling me to use the _get method but i'm using _post to capture the data from the forms to send to my database. im really confused. is there a proper way this can be done? Quote Link to comment Share on other sites More sharing options...
applebiz89 Posted February 7, 2011 Share Posted February 7, 2011 on the page you want to display the news, i assume you will have a listing. say the url to link to the news page is news.php?id=1 <- the primary key of the news article. then on that page you will use a query like: select * from news where pkID = $_GET['id'] thats what they mean by using the get. Quote Link to comment Share on other sites More sharing options...
jamesjmann Posted February 7, 2011 Author Share Posted February 7, 2011 on the page you want to display the news, i assume you will have a listing. say the url to link to the news page is news.php?id=1 <- the primary key of the news article. then on that page you will use a query like: select * from news where pkID = $_GET['id'] thats what they mean by using the get. yeah i got it to work yesterday. i looked around on google and found a good script. i got comments enabled for each article too. now im trying to figure out how i can add a rating bar thingy for each one. i suppose it would similar to what i did with the comments: Here's the script altogether: <?php /* user config variables */ $max_items = 5; /* max number of news items to show */ /* make database connection */ $db = mysql_connect ('hostname','username','password'); mysql_select_db ('database name',$db); function displayNews($all = 0) { /* bring in two variables * $db is our database connection * $max_items is the maximum number * of news items we want to display */ global $db, $max_items; /* query for news items */ if ($all == 0) { /* this query is for up to $max_items */ $query = "SELECT id,title,body," . "DATE_FORMAT(date, '%m-%d-%Y') as date " . "FROM news ORDER BY id DESC LIMIT $max_items"; } else { /* this query will get all news */ $query = "SELECT id,title,body," . "DATE_FORMAT(date, '%Y-%m-%d') as date " . "FROM news ORDER BY id DESC"; } $result = mysql_query ($query); while ($row = mysql_fetch_assoc ($result)) { /* display news in a simple table */ echo "<table border=\"0\" width=\"100%\">\n"; /* place table row data in * easier to use variables. * Here we also make sure no * HTML tags, other than the * ones we want are displayed */ $date = $row['date']; $title = htmlentities ($row['title']); $news = nl2br (strip_tags ($row['body'], '<a><b><i><u>')); $url = $row['id']; /* display the data */ echo "<tr><td><strong><h2><a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id={$row['id']}\">$title</a></h2></strong> <em>posted on $date</em> | by <strong>DJ Smiley</strong></td></tr>\n"; echo "<tr><td>"; echo stripslashes(substr($news, 0, 500)); echo "...<a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id={$row['id']}\">read more</a></td></tr>\n"; /* get number of comments */ $comment_query = "SELECT count(*) FROM comments " . "WHERE news_id={$row['id']}"; $comment_result = mysql_query ($comment_query); $comment_row = mysql_fetch_row($comment_result); /* display number of comments with link */ echo "</table>\n<table> <tr> <td><a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id={$row['id']}\">Comments" . "($comment_row[0])</a></td> </tr></table><br> <table><tr><td><a href=\"http://twitter.com/share\" class=\"twitter-share-button\" data-url=\"http://www.djsmiley.net/index.php?action=show&id={$row['id']}\" data-count=\"horizontal\">Tweet</a><script type=\"text/javascript\" src=\"http://platform.twitter.com/widgets.js\"></script></td> <td><script src=\"http://connect.facebook.net/en_US/all.js#xfbml=1\"></script><fb:like href=\"http://www.djsmiley.net/index.php?action=show&id={$row['id']}\" layout=\"button_count\" show_faces=\"true\" width=\"450\" font=\"tahoma\"></fb:like></td><td><!-- AddThis Button BEGIN --> <div class=\"addthis_toolbox addthis_default_style\" addthis:title=\"{$row['title']}\" addthis:url=\"http://www.djsmiley.net/index.php?action=show&id={$row['id']}\"> <a href=\"http://www.addthis.com/bookmark.php?v=250&username=xa-4d4f8d824aabe8c4\" class=\"addthis_button_compact\">Share</a> </div> <script type=\"text/javascript\" src=\"http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4d4f8d824aabe8c4\"></script> <!-- AddThis Button END --></td><td><script type=\"text/javascript\" src=\"http://cdn.widgetserver.com/syndication/subscriber/InsertWidget.js\"></script><script type=\"text/javascript\">if (WIDGETBOX) WIDGETBOX.renderWidget('af708afd-11db-4dd1-a7db-42157d39e7c5');</script><noscript>Get the <a href=\"http://www.widgetbox.com/widget/rating\">Five-Star Ratings Control</a> widget and many other <a href=\"http://www.widgetbox.com/\">great free widgets</a> at <a href=\"http://www.widgetbox.com\">Widgetbox</a>! Not seeing a widget? (<a href=\"http://docs.widgetbox.com/using-widgets/installing-widgets/why-cant-i-see-my-widget/\">More info</a>)</noscript></td></tr></table>"; } /* if we aren't displaying all news, * then give a link to do so */ if ($all == 0) { echo "<p class=\"BottomBorder\"></p></p><a href=\"{$_SERVER['PHP_SELF']}" . "?action=all\">View All Articles</a></p>\n"; } } function displayOneItem($id) { global $db; /* query for item */ $query = "SELECT * FROM news WHERE id=$id"; $result = mysql_query ($query); /* if we get no results back, error out */ if (mysql_num_rows ($result) == 0) { echo "This news article does not exist!\n"; return; } $row = mysql_fetch_assoc($result); echo "<TABLE border=\"0\" width=\"100%\">\n"; /* easier to read variables and * striping out tags */ $title = htmlentities ($row['title']); $news = nl2br (strip_tags ($row['body'], '<a><b><i><u>')); /* display the items */ echo "<tr><td><strong><h2><a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id={$row['id']}\">$title</a></h2></strong> <em>posted on $date</em> | by <strong>DJ Smiley</strong></td></tr>\n"; echo "<tr><td>"; echo stripslashes($news); echo "</td></tr>\n"; echo "</table>\n"; echo "<br>\n"; echo ("<table><tr><td><a href=\"http://twitter.com/share\" class=\"twitter-share-button\" data-url=\"http://www.djsmiley.net/index.php?action=show&id={$row['id']}\" data-count=\"horizontal\">Tweet</a><script type=\"text/javascript\" src=\"http://platform.twitter.com/widgets.js\"></script></td> <td><script src=\"http://connect.facebook.net/en_US/all.js#xfbml=1\"></script><fb:like href=\"http://www.djsmiley.net/index.php?action=show&id={$row['id']}\" layout=\"button_count\" show_faces=\"true\" width=\"450\" font=\"tahoma\"></fb:like></td><td><!-- AddThis Button BEGIN --> <div class=\"addthis_toolbox addthis_default_style\" addthis:title=\"{$row['title']}\" addthis:url=\"http://www.djsmiley.net/index.php?action=show&id={$row['id']}\"> <a href=\"http://www.addthis.com/bookmark.php?v=250&username=xa-4d4f8d824aabe8c4\" class=\"addthis_button_compact\">Share</a> </div> <script type=\"text/javascript\" src=\"http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4d4f8d824aabe8c4\"></script> <!-- AddThis Button END --></td><td></td></tr></table>"); /* now show the comments */ displayComments($id); } function displayComments($id) { /* bring db connection variable into scope */ global $db; /* query for comments */ $query = "SELECT * FROM comments WHERE news_id=$id"; $result = mysql_query ($query); echo "<h2>Comments</h2><br>\n"; /* display the all the comments */ while ($row = mysql_fetch_assoc ($result)) { echo "<table border=\"0\" width=\"25%\">\n"; $name = htmlentities ($row['name']); echo "<tr><td><strong>$name</strong> says:</td></tr>\n"; $comment = strip_tags ($row['comment'], '<a><b&><i><u>'); $comment = nl2br ($comment); echo "<tr><td>$comment</td></tr>\n"; echo "</table>\n"; echo "<br>\n"; } /* add a form where users can enter new comments */ echo "<form action=\"{$_SERVER['PHP_SELF']}" . "?action=addcomment&id=$id\" method=POST>"; echo "<table width=\"37%\" border=\"0\"> <tr> <td width=\12%\">Name:</td> <td width=\"88%\"><label> <input type=\"text\" name=\"name\" id=\"name\" class=\"commentBoxforms\"> </label></td> </tr> <tr> <td>Comment:</td> <td><label> <textarea name=\"comment\" id=\"comment\" cols=\"45\" rows=\"5\" class=\"commentField1\"></textarea> </label></td> </tr> <tr> <td> </td> <td><label> <input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Comment\" class=\"Button1\"> </label></td> </tr> </table> "; echo "</FORM>\n"; } function addComment($id) { global $db; /* insert the comment */ $query = "INSERT INTO comments " . "VALUES('',$id,'{$_POST['name']}'," . "'{$_POST['comment']}')"; mysql_query($query); echo "Your comment has been posted!<br>\n"; echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id=$id\">Return To Previous Page</a>\n"; } /* this is where the script decides what do do */ switch($_GET['action']) { case 'show': displayOneItem($_GET['id']); break; case 'all': displayNews(1); break; case 'addcomment': addComment($_GET['id']); break; default: displayNews(); } ?> This comes with a twitter and facebook like button, as well as an "addthis.com" share button. Just trying to figure out how in the hecks i can put a thumbs up rating button of my own in it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.