Member Avatar for kirtan_thakkar

I have a database some short notes in a .txt format file and i have created a php webpage that includes a txt file in html format. Now I have to use "Next" and "Preview" links for that .txt format files.
Can anyone suggest me the php or JavaScript code for that..
I have a suggetion if i name that all short notes files as 1, 2, 3, ... so can i use next and preview by using loop in javascript or php?? Can you suggest me that code?? Please....

specify your problem in detail may be with your code.

Member Avatar for kirtan_thakkar

I have text File like this
Text File 1: Knowledge is meaningful only if it is reflected in action. The human race has found out the hard way that we are what we do, not just what we think. This is true for kids and adults — for schoolrooms and nations. Text File 2: One thing we have endeavoured to observe most scrupulously, namely, never to depart from the strictest facts and, in dealing with the difficult questions that have arisen during the year, we hope that we have used the utmost moderation possible under the circumstances I have used the code as under in my page:

<?php
$filename="1.txt";
$handle=fopen($filename,"r");

$contents=fread
($handle,filesize($filename));

echo($contents);
?>

Than I have added "Next" And "Previous" Buttons. I have to create a code for which when i click on "Next" Button In the php code the file name get change to "2.text". And on that page on clicking on "Next" button the file name should have to change to "3.txt". As like this in "Previous" buton..
Please Help..

Member Avatar for rajarajan2017

Logic:

i) Store the filenames in a Array $fnames=array("1.txt","2.txt","3.txt");
ii) Write the above code in a function with arguments as your filename
iii) on you click on next button call the function by passing the array value

To process the same page again you can use:

<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<input type="button" name="submit" value="Next">
</form>

<?php
if(isset($_POST['submit'])) {
task;
}

Hope you understand the logic.

Member Avatar for kirtan_thakkar

Thanks for the reply.. I have understood.
Sorry but can you tell me how to pass the array..???

Member Avatar for rajarajan2017

Like
process($fname[0]);
process($fname[1]);
process($fname[2]);

Use following script, nothing more to do. Set your text directory path in currdirectory folder.

<?php
	$currdirectory="textfilesfolder";//path or your current directory from where you want to show files. i assume all files are text files
	if($_REQUEST['txtfile']=="")
		$txtfile=$currdirectory."/1.txt";
	else
		$txtfile=$currdirectory."/".$_REQUEST['txtfile'];
	
	
	$files = scandir($currdirectory);				
	$nextfile=$files[2];
	for ($i=0;$i<count($files);$i++)
	{
		$content=$currdirectory."/".$files[$i];
	 	if (is_file($content))
	 	{
	 	 	if($_REQUEST['txtfile']==$files[$i])
	 	 	{
				$prevfile=$files[$i-1];
				//$txtfile=$files[$i];
				$nextfile=$files[$i+1];
				break;
			}
		}

	}
	
	$handle=fopen($txtfile,"r");
	$contents=fread($handle,filesize($txtfile));
	echo($contents);
	fclose($handle);

	if($prevfile!="" && $i>=3)
		print "<br><a class='text17' href='{$_SERVER['PHP_SELF']}?txtfile={$prevfile}'>Previous</a>&nbsp;&nbsp;";
	if($nextfile!="")
		print "&nbsp;<a class='text17' href='{$_SERVER['PHP_SELF']}?txtfile={$nextfile}'>Next</a>";
 
         ?>
Member Avatar for kirtan_thakkar

Hey..
Is there any problem in the script..??
Because when I am adding 10 files there is not the "Next" button in 9.txt file..
When I opened typing 10.txt in the address bar there is a next button on 10.txt file. From which I was back to 2.txt..
Please help..

I amgetting this series after pressing the "next" button on 1.txt
1,1,10,2,3,4,5,6,7,8,9

Thanks for Helping..

Member Avatar for kirtan_thakkar

Hey..
Is there any problem in the script..??
Because when I am adding 10 files there is not the "Next" button in 9.txt file..
When I opened typing 10.txt in the address bar there is a next button on 10.txt file. From which I was back to 2.txt..
Please help..

I am getting this series after pressing the "next" button on 1.txt
1,1,10,2,3,4,5,6,7,8,9

Check it out this prob on this link..
http://tiny.cc/bto6o

Thanks for Helping..

it depends on order in array so its sorts in text mode and not in numeric mode.

Member Avatar for kirtan_thakkar

So can I use a.txt,b.txt,c.txt insted of 1,2,3...???????????

My fisrt script was better. it shows all files in your directory in file order. you may user a.txt... like that.

another version given below based on 1,2,3,4 series, which depends on series of number. if suppose 5.txt is not there then below script will fail.

$currdirectory=".";//path or your current directory from where you want to show files. i assume all files are text files
	if($_REQUEST['txtfile']=="")
		$txtfile=$currdirectory."/1".".txt";
	else
		$txtfile=$currdirectory."/".$_REQUEST['txtfile'].".txt";
	

	if(file_exists($currdirectory."/".($_REQUEST['txtfile']-1).".txt"))
		$prevfile=$_REQUEST['txtfile']-1;
	if(file_exists($currdirectory."/".($_REQUEST['txtfile']+1).".txt"))
		$nextfile=$_REQUEST['txtfile']+1;
	

	$handle=fopen($txtfile,"r");

	$contents=fread($handle,filesize($txtfile));
	echo($contents);
	fclose($handle);
	echo "<br>";
	if($prevfile!="" )
		print "<a class='text17' href='{$_SERVER['PHP_SELF']}?txtfile={$prevfile}'>Previous</a>&nbsp;&nbsp;";
	if($nextfile!="")
		print "&nbsp;<a class='text17' href='{$_SERVER['PHP_SELF']}?txtfile={$nextfile}'>Next</a>";

its your choice what to use

Member Avatar for kirtan_thakkar

Thank you....... So Much............

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.