Jump to content

Content Management System & Pagination


jamesjmann

Recommended Posts

Hey, all!

 

I posted a thread before about a content management system for my news, and so far the php script works fine. I would like to add additional features to this system, though, and was wondering if anyone could help.

 

Here's the code for all the files needed for the news content management system to work...

 

1.) First, is the mysql table, called "mynews" with the fields:

 

  • "id" - primary key; auto-increment.
  • "title" - the title of the news article.
  • "user" - the person who posted the article (would be me in all cases)
  • "message" - the body of the article; this would contain the actual news.
  • "url" - the url to the full article.
  • "time" - time and date the article was posted, or added to database.
  • "type" - the type of news, like 'update', or 'random'.

 

2.) Next, is the code to connect to the database in mysql, called "dbconnect.php":

 

<?php 
    $username = "your username"; //your username 
    $password = "your password"; //your password 
    $host = "your hostname"; //your mySQL server 
    $database = "your database name"; //The name of the database your table is in 

         mysql_connect($host,$username,$password) or die("Error connecting to Database!<br>" . mysql_error()); 
	 //connect, but if there is a problem, display an error message telling why there is a problem

         mysql_select_db($database) or die("Cannot select database!<br>" . mysql_error()); 
	 //Choose the database from your mySQL server, but if there is a problem, display an error telling why 
?>

 

3.) Third, is the forms for posting new articles, called "postnews.php":

 

<form action="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 class="cmsNewsformText">Type*:</td>
            <td><font size="1">
              <input name="type" type="text" class="cmsNewforms" size="50" />
            </font></td>
          </tr>
          <tr>
            <td class="cmsNewsformText">News Topic/Title*: </td>
            <td><font size="1">
              <input name="title" type="text" class="cmsNewforms" value="enter title of article" size="50" />
            </font></td>
          </tr>
          <tr>
            <td class="cmsNewsformText">Username*:</td>
            <td><font size="1">
              <input name="user" type="text" class="cmsNewforms" value="enter username" size="50" />
            </font></td>
          </tr>
          <tr>
            <td class="cmsNewsformText">Url*:</td>
            <td><font size="1">
              <input name="url" type="text" class="cmsNewforms" value="enter url of news article" size="50" />
            </font></td>
          </tr>
          <tr>
            <td class="cmsNewsformText">Message*:</td>
            <td><font size="1">
              <textarea name="message" cols="43" rows="10" class="cmsNewforms">enter body of article
      </textarea>
            </font></td>
          </tr>
        </table>
        <p> <font size="1">
          <input name="Submit" type="submit" class="Button1" value="Submit" />
        </font> </p>
      </form>

 

4.) Fourth, is process.php. This script sends the data entered by the administrator (me) on "postnews.php" to the database, and displays a message saying that the information was sent successfully.

 

<?php
$user=$_POST['user']; 
$title=$_POST['title']; 
$message=$_POST['message']; 
$type=$_POST['type']; 
$url=$_POST['url']; 
mysql_connect("your hostname", "your database name", "your password") or die(mysql_error()); 
mysql_select_db("cmsnewsacp") 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"; 
?> 

 

5.) And lastly, here's the script for displaying the news on the home page:

 

<? 
include("dbconnect.php"); //include the file to connect to the database 
$getnews = mysql_query("SELECT * FROM mynews ORDER BY id DESC"); //query the database for all of the news 

while($r=mysql_fetch_array($getnews)){ //while there are rows in the table 
extract($r); //remove the $r so its just $variable 

echo("
<span class=NewsID>$type</span> <span class=h2>$title</span><br><br>
<em>posted by <strong>$user</strong> on $time</em><br> 
<span class$message<br><br />
<font color=0099FF><a href=$url>Read more - $url</a></font><br><br><p>"); 

} 

?>

 

Now that you've got all the code, let's take a look back at the table "mynews" with its fields. See "url"? This variable would contain the url I type in the forms on "postnews.php" and would be called to form a "Read more" link at the end of each article displayed on the home page. The question I have here, is how can I modify this script to where everytime a new 'article' is added to the database, a new page is created with the article and the url I specified? Also, I want to limit the amount of characters of the article, so that it'll only display a little bit on the home page, with a "read more" link that takes them to that page.

 

Now last but not least, how in the bloody fudge do I script pagination into this script?

 

Link to comment
Share on other sites

While it might seem a different subject, the logic is the same. This thread should help you.

 

As for pagination, there are many examples on the net, surely we don't need to write another tutorial here?

 

I've looked at some tutorials, but all of them seem too complicated and there wasn't much explanation.

 

And thanks for the link! That definitely answered my question!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.