Forum Moderators: coopster

Message Too Old, No Replies

read particular character at one time

php file reading

         

Silent Miracle

8:53 am on Jun 13, 2011 (gmt 0)

10+ Year Member



i have written a code for file reading in php ... here is my code
<?php
include ('GphpChart.class.php');
echo "<META HTTP-EQUIV=Refresh CONTENT='2; URL=graph.php'>";
$l=fopen('new.txt',"r");

$lines = fgets($l);
$s=explode(',',$lines);


$arrval=$s;

$lc1 = $arrval;
$GphpChart = new GphpChart("lc");
$GphpChart->title = "My chart";
$GphpChart->add_data($lc1);
echo $GphpChart->get_Image_String();
echo "</META>";
?>
it reads data from the text file and displays graph from it .
what i am unable to sort out is i want to read 30 characters at one time from the file then displays graph of those 30 characters and when the page refreshes it reads the next 30 characters and then displays the graph of these 60 characters and so on. please help !

rocknbil

5:11 pm on Jun 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No time to code it ATM but what I'd do is combine substr [php.net] with a session variable to "keep track" of the current index in the string. This is a bit of overhead though, reading the file in on each refresh. Maybe you should store that in a session variable too, so you just read it one time.


if (isset($_SESSION['mystring'])) {
$string = $_SESSION['mystring'];
}
else {
// Read file and store in $string
// Be sure to set the session variable the first time
}

$current_index = (isset($_SESSION['ind']))?$_SESSION['ind']:0;

$current = substr($string,$current_index,30);

so if you're on the second page refresh, $current_index should be 31. Increment it by 30 and save in $_SESSION['ind'] for the next one.

After it goes all the way through be sure to unset the session variables.