Jump to content

Recommended Posts

finishing touches for my php project news. please help

the requirements are the following for the news.class.php:

 

1. In index page, it must display 5 contents per page and sorted in

descending oder base on submitDate of the content.

 

2. And the index page, must contain paging/ pagination at the lower

right portion

 

here are the codes:

 

news.class.php

<?php
class news{
var $newsDir = "news";
var $newsList;
var $newsCount = -1;
function getNewsList( ){
$this->newslist = array();
if ($handle = @opendir($this- >newsDir) ) {
while ($file = readdir($handle) ) {
if (!is_dir($file) ) {
$this->newsList[ ] = $file;
}
}
}
rsort($this- >newsList) ;
return $this->newsList;
}
function getNewsCount( ){
}
function displayNews( ){
$list = $this->getNewsList( );
echo "<table class='newsList' >";
foreach($list as $value){
$newsData = file($this-
>newsDir.DIRECTORY_ SEPARATOR. $value);
$newsTitle = $newsData[0] ;
$submitDate = $newsData[1] ;
unset($newsData[ '0']);
unset($newsData[ '1']);
$newsContent = "";
foreach($newsData as $value){
$newsContent. =$value;
}
echo "<tr><th align='left' >$newsTitle< /th>";
echo "<tr><td 
colspan='2'> ".$newsContent. "<br/><td/ ></tr>";
echo "<th 
class='right' >$submitDate< /th></tr> ";
}
}
function displayAddForm( ){
?>
<script language="javascrip t" type="text/javascri pt" 
src="js/tiny_ mce.js">< /script>
<script language="javascrip t" type="text/javascri pt">
tinyMCE.init( {
mode : "textareas",
theme : "advanced",
theme_advanced_ buttons3 : "",
theme_advanced_ toolbar_align : "center",
theme_advanced_ toolbar_location : "top",
});
</script>
<form class="iform" action="<?php echo $_SERVER
['PHP_SELF'] ; ?>" method="post" >
News title: <br/>
<input type="text" name="title" size="40" /><br/><br/>
Content:<br/ >
<textarea name="newstext" rows="15" 
cols="67"></ textarea> <br/>
<center><input type="submit" name="submit" 
value="Save" ></center>
</form>
<?php
}
function insertNews() {
$newsTitle =isset($_POST[ 'title']) ? $_POST
['title'] : 'Untitled';
$submitDate = date('Y-m-d g:i:s A');
$newsContent = isset($_POST[ 'newstext' ])? $_POST
['newstext'] :'No content';
$filename = date('YmdHis' );
if(!file_exists( $this->newsDir) ){
mkdir($this- >newsDir) ;
}
$f = fopen($this-
>newsDir.DIRECTORY_ SEPARATOR. $filename. ".txt","w+ ");
fwrite($f,$newsTitl e."\n");
fwrite($f,$submitDa te."\n");
fwrite($f,$newsCont ent."\n") ;
fclose($f);
header('Location: index.php' );
}
}
?>

 

here is index.php:

<?php
#Front End
?>
<?php
require_once
("news.class.php");
$newsHandler = new news();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>News</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="container">
<div id="header"><div id="header_left"></div>
<div id="header_main">News</div><div id="header_right"></div></div>
<div id="content">
	<?php $newsHandler->displayNews();?>
</div>
</div>
</body>
</html>

here is admin.php

<?php
#News administrator panel
?>
<?php
require_once("news.class.php");
$newsHandler = new news();
if (!isset($_POST['submit'])){
?>

<!DOCTYPE HTML PUBLIC"-//W3C//DTD XHTML 1.0 Transitionla//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>News - Admin panel</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="container">
<div id="header"><div id="header_left"></div>
<div id="header_main">News - Admin panel</div><div id="header_right"></div></div>
<div id="content">
	<?php $newsHandler->displayAddForm();?>
</div>
</div>
</body>
</html>

<?php
}else{
$newsHandler->insertNews();
}
?>

And the question is ?

also.. clean up the code the formatting makes it hard to read

 

cleaned up (formatting)

<?php
class news
{
    var $newsDir = "news";
    var $newsList;
    var $newsCount = - 1;
    function getNewsList ()
    {
        $this->newslist = array();
        if ($handle = @opendir($this->newsDir)) {
            while ($file = readdir($handle)) {
                if (! is_dir($file)) {
                    $this->newsList[] = $file;
                }
            }
        }
        rsort($this->newsList);
        return $this->newsList;
    }
    function getNewsCount ()
    {}
    function displayNews ()
    {
        $list = $this->getNewsList();
        echo "<table class='newsList' >";
        foreach ($list as $value) {
            $newsData = file($this->newsDir . DIRECTORY_SEPARATOR . $value);
            $newsTitle = $newsData[0];
            $submitDate = $newsData[1];
            unset($newsData['0']);
            unset($newsData['1']);
            $newsContent = "";
            foreach ($newsData as $value) {
                $newsContent .= $value;
            }
            echo "<tr><th align='left' >$newsTitle< /th>";
            echo "<tr><td colspan='2'> " . $newsContent . "<br/><td/ ></tr>";
            echo "<th class='right' >$submitDate< /th></tr> ";
        }
    }
    function displayAddForm ()
    {
        ?>
<script language="javascript" type="text/javascript" 	src="js/tiny_ mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init( {
mode : "textareas",
theme : "advanced",
theme_advanced_ buttons3 : "",
theme_advanced_ toolbar_align : "center",
theme_advanced_ toolbar_location : "top",
});
</script>
<form class="iform" action="<?php echo $_SERVER['PHP_SELF'];?>"	method="post">News title: <br />
<input type="text" name="title" size="40" /><br />
<br />
Content:<br />
<textarea name="newstext" rows="15" cols="67">
</textarea> <br />
<center><input type="submit" name="submit" value="Save"></center>
</form>
<?php
    }
    function insertNews ()
    {
        $newsTitle = isset($_POST['title']) ? $_POST['title'] : 'Untitled';
        $submitDate = date('Y-m-d g:i:s A');
        $newsContent = isset($_POST['newstext']) ? $_POST['newstext'] : 'No content';
        $filename = date('YmdHis');
        if (! file_exists($this->newsDir)) {
            mkdir($this->newsDir);
        }
        $f = fopen($this->newsDir . DIRECTORY_SEPARATOR . $filename . ".txt", "w+ ");
        fwrite($f, $newsTitle . "\n");
        fwrite($f, $submitDate . "\n");
        fwrite($f, $newsContent . "\n");
        fclose($f);
        header('Location: index.php');
    }
}
?>

how to code,where in, in index page, it must display 5 contents per page and sorted in

descending order base on submitDate of the content.

And the index page, must contain paging/ pagination at the lower

right portion

 

And the question is ?

also.. clean up the code the formatting makes it hard to read

 

cleaned up (formatting)

<?php
class news
{
    var $newsDir = "news";
    var $newsList;
    var $newsCount = - 1;
    function getNewsList ()
    {
        $this->newslist = array();
        if ($handle = @opendir($this->newsDir)) {
            while ($file = readdir($handle)) {
                if (! is_dir($file)) {
                    $this->newsList[] = $file;
                }
            }
        }
        rsort($this->newsList);
        return $this->newsList;
    }
    function getNewsCount ()
    {}
    function displayNews ()
    {
        $list = $this->getNewsList();
        echo "<table class='newsList' >";
        foreach ($list as $value) {
            $newsData = file($this->newsDir . DIRECTORY_SEPARATOR . $value);
            $newsTitle = $newsData[0];
            $submitDate = $newsData[1];
            unset($newsData['0']);
            unset($newsData['1']);
            $newsContent = "";
            foreach ($newsData as $value) {
                $newsContent .= $value;
            }
            echo "<tr><th align='left' >$newsTitle< /th>";
            echo "<tr><td colspan='2'> " . $newsContent . "<br/><td/ ></tr>";
            echo "<th class='right' >$submitDate< /th></tr> ";
        }
    }
    function displayAddForm ()
    {
        ?>
<script language="javascript" type="text/javascript" 	src="js/tiny_ mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init( {
mode : "textareas",
theme : "advanced",
theme_advanced_ buttons3 : "",
theme_advanced_ toolbar_align : "center",
theme_advanced_ toolbar_location : "top",
});
</script>
<form class="iform" action="<?php echo $_SERVER['PHP_SELF'];?>"	method="post">News title: <br />
<input type="text" name="title" size="40" /><br />
<br />
Content:<br />
<textarea name="newstext" rows="15" cols="67">
</textarea> <br />
<center><input type="submit" name="submit" value="Save"></center>
</form>
<?php
    }
    function insertNews ()
    {
        $newsTitle = isset($_POST['title']) ? $_POST['title'] : 'Untitled';
        $submitDate = date('Y-m-d g:i:s A');
        $newsContent = isset($_POST['newstext']) ? $_POST['newstext'] : 'No content';
        $filename = date('YmdHis');
        if (! file_exists($this->newsDir)) {
            mkdir($this->newsDir);
        }
        $f = fopen($this->newsDir . DIRECTORY_SEPARATOR . $filename . ".txt", "w+ ");
        fwrite($f, $newsTitle . "\n");
        fwrite($f, $submitDate . "\n");
        fwrite($f, $newsContent . "\n");
        fclose($f);
        header('Location: index.php');
    }
}
?>

This will get you sorter for the first five queries orded by date descending:

 

"SELECT contents FROM table ORDER BY submitDate DESC LIMIT 5"

 

Next: pagination isn't something that can be explained in a thread. Google it - there are more tutorials on how to do pagination than you can shake a stick at.

Where will I put this code on my index.php(my previous codes above)?

 

"SELECT contents FROM table ORDER BY submitDate DESC LIMIT 5"

 

 

Next: pagination isn't something that can be explained in a thread. Google it - there are more tutorials on how to do pagination than you can shake a stick at.

You can't use the code I gave you - it was an example. You will have to swap in your own table and column names, and put it in the spot where you are doing your database query. If you were the one who wrote the script, this shouldn't be a problem.

 

And its really hard to understand your posts if you keep including your comments in my quotes. Hint: Put your comments outside the opening and closing quote tags.

  • 1 month later...
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.