how to make a basic paging of text files(no mysql used) ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
edrew04
Forum Newbie
Posts: 1
Joined: Thu Jan 29, 2009 4:22 am

how to make a basic paging of text files(no mysql used) ?

Post by edrew04 »

how to make a basic paging of text files(no mysql used) in php?
for instance: i have this code here and a page will only display 5 text files only per page and there will be a "next>>" button where in , you can go to the other remaining text files and also there is the link "<<previous" where in, you can go back to the first five page you had started please help.
:

news.class.php

Code: Select all

<?php
class news{
    var $newsDir = 'news';
    var $newsList;
    var $newsCount = -1;
function getNewsList(){
    $this->newsList=array();
    //Open the actual directory
    if($handle=@opendir($this->newsDir)){
        //Read all file form the actual directory
        while ($file=readdir($handle)){
            if(!is_dir($file)){
                $this->newsList[]=$file;
            }
        }
    }
    rsort($this->newsList);
    return $this->newsList;
}
/*Well, I will not use the function getNewsCount(){} anymore because i had a better idea of counting the number of news*/
/*function getNewsCount(){}<-trapped in a comment hahahahahaha ^.^*/
 
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="";
        $i= 0;
        foreach($newsData as $value){
            $newsContent.=$value;
                
        }    
                    $numOfNews=(count($list));
                    if($numOfNews>5){
                    $numOfNextNewsPage=$numOfNews-5;
                    //$list=$numOfNextNewsPage;
                    //$NextNewsPage=$list-$numOfNextNewsPage;
                       
                       echo "<tr><th align='left'>$newsTitle</th>";
               echo "<tr><td colspan='2'>".$newsContent."<br></td></tr>";
               echo "<th class='right'>$submitDate</th></tr>";
                       
                       }
                         else
                         {
                         
                         echo "<tr><th align='left'>$newsTitle</th>";
                 echo "<tr><td colspan='2'>".$newsContent."<br></td></tr>";
                 echo "<th class='right'>$submitDate</th></tr>";
                         }
                         
                 
                         
    }
         if($numOfNews>0 && $numOfNews<=5){
         $stringMsgNews="<b>The total number of news for today is:</b> ".$numOfNews." <b>News.</b><br>";
         echo $stringMsgNews;
         echo"</table> ";
         }
           else if($numOfNews>5){
                   $stringMsgNews="<b>The total number of news for today is:</b> ".$numOfNews." <b>News.</b><br>";
                   echo $stringMsgNews;
                   echo "The page exeeded to 5 news. click next>> to see more news"; 
                   echo"</table> ";
                   echo "<a href='http://localhost/xampp/cardano/cardano3/Project_News/index.php'><center><p><b>next>></b></p></center>";
                  }
                  else{
                  echo "";
                  echo"</table> ";
                  }
         
    //echo"</table> ";
   if(sizeOf($list) ==0){
   echo "<hr color='dodgerblue' size='10%'>";
   echo "<center><p><b> No News At The Moment.</b></p><p>&nbsp</p></center> ";
   echo "<hr color='dodgerblue' size='10%'>";
  }
}
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">
    </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');
}
}
?>
;;;;

index.php

Code: Select all

 
<?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>

admin.php

Code: Select all

 
<?php
//News administration panel.
?>
<?php
require_once("news.class.php");
$newsHandler = new news();
if (!isset($_POST['submit'])) {
?>
<html>
<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();
}
?>
Post Reply