Flash Database Step By Step Tutorial - Part 4: After create read and saving data, we make a switching. The switching is controlled by GET parameter, action.
The code like this:
switch($_GET['action']){
case 'saveData':
saveData();
break;
default:
readData();
break;
}
Below, complete code for polling.php:
<?
$file_data = "polling.txt";
$cookie_duration = 360;
$cookie_title = 'text_poll_1';
// prepare no cache
header("Expires: Mon, 01 Jan 1990 00:00:00 GMT");
header("Last-Modified: ". gmdate("D, d M Y h:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0; pre-check=0", false);
header("Pragma: no-cache");
function readData(){
global $file_data;
$fp = file($file_data);
while(list($line, $value) = each($fp)){
print "&" . eregi_replace("\r\n","",$value);
}
}
function saveData(){
global $file_data;
if(number_format($_GET['num']) < 1) die("registered=false");
// open file
$fp_array = file($file_data);
$fp = fopen($file_data,"r+");
$temp_string = "";
// read file
foreach($fp_array as $key => $value){
// extract each lines
$line = explode('=', $value);
$name = $line[0];
$val = eregi_replace("\r\n","",$line[1]);
// if as votes
if(substr($name,0,5)=='votes'){
$val = (int)eregi_replace("\r\n","",$line[1]);
$match= (int)(substr($name,5,2));
// if the index same as choice, add 1
if($match == $_GET['num']){
$val++;
}
}
// write this line
$temp_string .= $name."=".$val."\r\n";
}
fwrite($fp, $temp_string);
fclose($fp);
}
switch($_GET['action']){
case 'saveData':
saveData();
break;
default:
readData();
break;
}
?>