Home arrow ASP arrow Page 4 - Building a Multi-Page Article System With ASP/PHP
ASP

Building a Multi-Page Article System With ASP/PHP


Interested in building your own CMS? In this article Mitchell shows us how to build a multi-page article system that can be used in any web-based CMS...

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 35
January 05, 2003
TABLE OF CONTENTS:
  1. · Building a Multi-Page Article System With ASP/PHP
  2. · How Everything Will Work
  3. · The JavaScript Behind it All
  4. · Saving Articles to the Database
  5. · Displaying a List of Articles
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Building a Multi-Page Article System With ASP/PHP - Saving Articles to the Database
(Page 4 of 6 )

When the form is submitted, we have a number of variables available to us. They are:
  • articleTitle
  • articleSummary
  • pageTitle1 to pageTitle20
  • pageContent1 to pageContent20
If you match these up with the schema of our database, then you will see that we have all of the data we need to create entries in our database for each article. We will have 1 entry in the articles table and 1 or more entries in the pages table for each article. Each page is linked back to the article via the articleId field.

Let's now look at the code to save an article to the database.

ASP Code

sub SaveArticle()

%><!-- #INCLUDE file="db.asp" --><%

dim articleId
dim articleTitle
dim articleSummary
dim arrPages
dim objDict
dim page
dim counter
dim i

set arrPages = Server.CreateObject("Scripting.Dictionary")
counter = 0
articleTitle = Request.Form("articleTitle")
articleSummary = Request.Form("articleSummary")

for i = 1 to 20
if Request.Form("pageTitle" & i) <> "" then
arrPages.Add Request.Form("pageTitle" & i), Request.Form("pageContent" & i)
end if
next

'Add an entry to the article table
objConn.Execute("insert into articles(title, summary) values('" & articleTitle & "', '" & articleSummary & "')")

if objConn.Errors.Count > 0 then
Response.Write "Couldn't add article"
Response.End
end if

objRS.Open("select top 1 articleId from articles order by articleId desc")
articleId = objRS.Fields(0).value

'Add one or more entries to the pages table
for each page in arrPages
objConn.Execute("insert into pages(articleId, title, content) values(" & articleId & ", '" & page & "', '" & arrPages(page) & "')")

if objConn.Errors.Count > 0 then
Response.Write "Couldn't add page"
Response.End
end if
next

%>
<font face="Verdana" size="4"><b>Article Added Successfully!</b></font><br><br>
<font face="Verdana" size="2">
<b>Details</b>
<ul>
<li>Article title: <i>"<% Response.Write articleTitle %>"</i></li>
<li>Article ID: <% Response.Write articleId %></li>
<li>Number of pages: <% Response.Write arrPages.count %></li>
</ul>
</font>
<%



end sub


PHP Code

function SaveArticle()
{
include("db.php");

$articleTitle = $_POST["articleTitle"];
$articleSummary = $_POST["articleSummary"];
$arrPages = array();
$counter = 0;

// Loop to get the pages
for($i = 1; $i < 20; $i++)
{
if($_POST["pageTitle$i"] != "")
{
$arrPages[] = array("title" => $_POST["pageTitle$i"], "content" => $_POST["pageContent$i"]);
}
}

// Add an entry to the article table
@mysql_query("insert into articles(title, summary) values('$articleTitle', '$articleSummary')") or die("Couldn't add article: " . mysql_error());

// Grab the ID of the new article record
$articleId = mysql_insert_id();

// Add one or more entries to the pages table
for($i = 0; $i < sizeof($arrPages); $i++)
{
@mysql_query("insert into pages(articleId, title, content) values($articleId, '" . $arrPages[$i]["title"] . "', '" . $arrPages[$i]["content"] . "')") or die("Couldn't add page: " . mysql_error());
}

?>
<font face="Verdana" size="4"><b>Article Added Successfully!</b></font><br><br>
<font face="Verdana" size="2">
<b>Details</b>
<ul>
<li>Article title: <i>"<?php echo $articleTitle ?>"</i></li>
<li>Article ID: <?php echo $articleId; ?></li>
<li>Number of pages: <?php echo sizeof($arrPages); ?></li>
</ul>
</font>
<?php
}


Now that we can create and save our articles, we need to display them. Let's now look at how to display a list of articles as well as each article on its own.
blog comments powered by Disqus
ASP ARTICLES

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials