Jump to content

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


edrew04

Recommended Posts

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.

:

<?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');
}
}
?>

Link to comment
Share on other sites

I'll give you a quick example...

 

<?php

// news.php

/* beautified with active perl php cleaner 1.04 */

class news
{
var $newsList = array ();

function getNewsList ( $directory, $sort )
{
	$this->newsList = array ();

	/* load the files from the directory */

	$files = glob ( $directory . '/*' );

	foreach ( $files AS $file )
	{
		if ( ! is_dir ( $file ) )
		{
			$this->newsList[] = $file;
		}
	}

	$sort ( $this->newsList );
}


function displayNews ( $url, $directory, $maximum = 0, $sort = 'asort' )
{
	$this->getNewsList ( $directory, $sort );

	$start     = 0;

	$nav       = false;

	$statisics = '';

	if ( $maximum > 0 )
	{
		$total = sizeof ( $this->newsList );

		if ( $total > 0 )
		{
			if ( $total > $maximum )
			{
				$nav = true;

				if ( ! empty ( $_GET['s'] ) )
				{
					$temp = intval ( $_GET['s'] );

					if ( $temp > 0  && $temp <= $total )
					{
						$start = $temp;
					}
				}
			}
		}

		$math = ( ( $start - 1 ) / $maximum ) + 1;

		$root = $maximum * $math;

		$next = $total < $root ? $total : $root;

		$this->newsList = array_slice ( $this->newsList, $start, $maximum, true );

		$statisics = 'Viewing news items: ' .  ( $start + 1 ) . ( $next != $total ? ' - ' . ( $next + 1 ) : '' ) . ' of ' . $total;
	}

	$out = "		<table class='newsList'>";

	if ( empty ( $this->newsList ) )
	{
		$out .= "			<tr>
			<td>
				<hr color='dodgerblue' size='10%'>
					<center>
						<p>
							<b>No News At The Moment.</b>
						</p>
						<p>
							&nbsp
						</p>
					</center>
				<hr color='dodgerblue' size='10%'>
			</td>
		</tr>
";
	}
	else
	{
		if ( $nav )
		{
		$out .= "			<tr>
			<th align='right'>
				" . $statisics . "
			</th>
		</tr>
";
		}

		foreach ( $this->newsList as $value )
		{
			$newsData = file ( $value );

			$newsTitle = $newsData[0];

			$submitDate = $newsData[1];

			$out .= "			<tr>
			<th align='left'>
				" . $newsTitle . "
			</th>
		</tr>
		<tr>
			<td align='left'>
				" . implode ( '', array_slice ( $newsData, 2 ) ) . "
				<br />
			</td>
		</tr>
		<tr>
			<th class='right'>
				" . $submitDate . "
			</th>
		</tr>
";
		}

		if ( $nav )
		{
		$out .= "			<tr>
			<th align='left'>
				" . $this->makeLinks ( $url, $start, $total, $maximum ) . "
			</th>
		</tr>
";
		}
	}

	echo $out . "
		</table>
";
}


function makeLinks ( $url, $start, $total, $maximum )
{
	$links = 1;

	$nav = '';

	for ( $x = 0; $x < $total; $x += $maximum )
	{
		if ( $links != 1 )
		{
			$nav .= ' | ';
		}

		if ( $start != 0 && $start == $x || $start == 0 && $x == 0  )
		{
			$nav .= $links;
		}
		else
		{
			$nav .= "<a class='' href='" . $url . "?s=" . $x . "' title='Go to page: " . $links . "'>" . $links . "</a>";
		}

		$links++;
	}

	return $nav;
}
}

/* test it... */

// the url to this script
$url = 'http://localhost/news.php';

// the news directory to read
$directory = './news';

// number of news items to show per page
$maximum = 4;

// sort news items
$sort = 'rsort';

// load the class
$news = new news ();

// display the results
$news->displayNews ( $url, $directory, $maximum, $sort );

?>

Link to comment
Share on other sites

  • 4 weeks later...

the owner of all the codes that i posted here is not me,the owner is our professor, ms. cabrera, of our class, trend1s-2ndsemester.

 

 

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.

:

<?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');
}
}
?>

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.