<?php
// flat_file_mathematics.php
// RJM Programming
// April, 2017
// Mathematical web applications that use flat_file_engine.php Flat File SQL Engine
$dbname="fibonacci_sequence";
$dbcollist="prevf,thisf,ratio";
$dbvallist="' [0_thisf]',' max([-1_thisf]+[0_thisf],1)',' max([-1_thisf]+[0_thisf],1)/[0_thisf]'";
$datais="";
if (isset($_GET['file'])) {
  $fis=urldecode($_GET['file']);
  $datais=@file_get_contents(urldecode($_GET['file']));
} else if (isset($_POST['file'])) {
  $fis=urldecode($_POST['file']);
  $datais=@file_get_contents(urldecode($_POST['file']));
} else if (file_exists("flat_file.dat")) {
  $fis="flat_file.dat";
  $datais=@file_get_contents("flat_file.dat");
}

$listofothers="";
$inhouse=explode("^", $datais);
for ($inh=1; $inh<sizeof($inhouse); $inh++) {
  if (strpos(explode("|", $inhouse[$inh])[0], "!") !== false && strpos(explode("|", $inhouse[$inh])[0], ";") !== false) {     
      $titlebit=str_replace("_"," ",explode("_mathematics",explode("_poll",str_replace("\n","",explode("|",explode("`", $inhouse[-1 + $inh])[-1 + sizeof(explode("`", $inhouse[-1 + $inh]))])[0]))[0])[0]);
      $themes=explode(" ",$titlebit);
      $htitle="";    
      $hdelim="";    
      for ($ih=0; $ih<sizeof($themes); $ih++) {    
       if (strlen($themes[$ih]) > 1) {      
       $htitle.=$hdelim . strtoupper(substr($themes[$ih],0,1)) . strtolower(substr($themes[$ih],1));     
       } else {    
       $htitle.=$hdelim . $themes[$ih];    
       }    
       $hdelim=" ";
      }   
      $listofothers.=" ppieceit('" . $htitle . "','" . explode("!", explode(";", $inhouse[$inh])[1])[0] . "');   \n";
  }
}

echo "<!doctype html>
<html> 
<head><meta charset='utf-8'><title>Mathematical Polling - RJM Programming - April, 2017</title>
<script type='text/javascript'>
var dbname='" . $dbname . "'; 
var dbcollist='" . $dbcollist . "'; 
var dbvallist=\"" . $dbvallist . "\"; 
var numtimes=0, prefix=\"<form target='myiframe' method='POST' action='./flat_file_engine.php'><input name=listddl id=THREElistddl type=hidden value=''></input><input name=ddl id=THREEddl type=hidden value=''></input><h1>TWO</h1>\";
var midbit=\"<input onclick=\\\" document.getElementById('THREEddl').value=this.title; document.getElementById('myiframe').style.display='block'; document.getElementById('FOUR').click(); \\\" style=background-color:SIX; type=button value=\\\"FIVE\\\"  title=\\\"CREATE TABLE DATABASENAME (DATABASECOLLIST); INSERT INTO DATABASENAME (DATABASECOLLIST) VALUES (DATABASEVALLIST); \\\"></input>\";
var suffix=\"<br><br><input style=display:none; id=FOUR type=submit value='ONE Mathematical Web Application'></input></form>\";
var numo=0, dform='', listddl='', listddldelim='', one='', two='', icyo=1, lastfive='';
var bcols=['red','yellow','green','blue','cyan','magenta','pink','lightblue','lightgreen','pink'];
function dofewtimes() {
 if (numtimes > 0) {
  document.getElementById('myfibonaccis').click();
  numtimes--;
  setTimeout(dofewtimes,2000);
 }
}  
function doothers() {
  " . $listofothers . "
}
function prepieceit() { 
  ppieceit('','');
}
function ppieceit(aone,afive) {
  var ourfive='z', ourfives;  
  if (dform.length == 0) {  
   if (aone != '') {  
    one=aone;
    if (aone == 'Music Genre' || aone.indexOf(' ') == -1) {
    two='Favourite ' + aone + '?';
    } else {
    two=aone + '?';
    }
    pieceit(one.replace(/\ /g,'_').toLowerCase(),'','','');
   } else {  
   while (one == null || one == '') { 
    one=prompt('Enter the Mathematical Web Application word(s) to describe the topic ... eg. Fibonacci Sequence','Fibonacci Seq');
   }
   while (two == null || two == '') { 
    two=prompt('Enter the Mathematical Web Application title','Show your ' + one + '?');
   }
   var ztwos=two.split(' your ');
   if (ztwos.length == 2) {
     one=ztwos[1].replace('?','');
   }
   dbname=prompt('Enter the Mathematical Web Application database (and data table) name','" . str_replace('fibonacci_sequence','fibonacci_seq',$dbname) . "');    
   dbcollist=prompt('Enter the Mathematical Web Application database ' + dbname + ' comma separated column list','" . $dbcollist . "');     
   dbvallist=prompt(\"Enter the Mathematical Web Application database \" + dbname + \" comma separated value list (remembering to prefix [colname] entries by a single space)\",\"" . $dbvallist . "\");     
   pieceit(one.replace(/\ /g,'_').toLowerCase(),'','','');
   var def='Next';
   while (ourfive != null && ourfive != '') {
    ourfive=prompt('Enter the ' + one + ' Mathematical Web Application option ' + eval(1 + numo) + ' that you want to have functionality for, and for no more click Cancel',def); 
    def='';  
    if (ourfive != null) {
     if (ourfive != '') {
      listddl+=listddldelim + ourfive;
      listddldelim=',';
      pieceit(one.replace(/\ /g,'_').toLowerCase(), 'my' + one.replace(/\ /g,'_').toLowerCase() + 's', ourfive, '');
     }
    }
   }
   pieceit(one.replace(/\ /g,'_').toLowerCase(), 'my' + one.replace(/\ /g,'_').toLowerCase() + 's', '', '');
   }
   if (afive != '') {   
   ourfives=afive.split(',');
   for (var iour=0; iour<ourfives.length; iour++) {
      pieceit(one.replace(/\ /g,'_').toLowerCase(), 'my' + one.replace(/\ /g,'_').toLowerCase() + 's', ourfives[iour], '');
   }
   } else {
   pieceit(one.replace(/\ /g,'_').toLowerCase(), 'my' + one.replace(/\ /g,'_').toLowerCase() + 's', ourfive, '');
   while (ourfive != null && ourfive != '') {
    ourfive=prompt('Enter the Mathematical Web Application option ' + eval(1 + numo) + ' that you want to have functionality for, and for no more click Cancel','');
    if (ourfive != null) {
     if (ourfive != '') {
      pieceit(one.replace(/\ /g,'_').toLowerCase(), 'my' + one.replace(/\ /g,'_').toLowerCase() + 's', ourfive, '');
     }
    }
   }
   }
   pieceit(one.replace(/\ /g,'_').toLowerCase(), 'my' + one.replace(/\ /g,'_').toLowerCase() + 's', '', '');
  } 
}
function pieceit(three,four,five,six) {
  if (dform.length == 0 && two != '' && one != '') {
   dform=prefix.replace(/DATABASEVALLIST/g,dbvallist).replace(/DATABASECOLLIST/g,dbcollist).replace(/DATABASENAME/g,dbname).replace(/THREE/g,three.toLowerCase()).replace(/TWO/g,two);  
  } 
  if (five != '') {
   lastfive=five;
   six=bcols[eval(numo % bcols.length)];
   numo++;
   dform+=midbit.replace(/DATABASEVALLIST/g,dbvallist).replace(/DATABASECOLLIST/g,dbcollist).replace(/DATABASENAME/g,dbname).replace(/SIX/g,six).replace(/FIVE/g,five).replace(/FOUR/g,four).replace(/THREE/g,three.toLowerCase()).replace(/ONE/g,one.replace(/\ /g,'_').toLowerCase());
  } else if (four != '' && four != 'mys') {
   dform+=suffix.replace(/DATABASEVALLIST/g,dbvallist).replace(/DATABASECOLLIST/g,dbcollist).replace(/DATABASENAME/g,dbname).replace(/FIVE/g,lastfive).replace(/FOUR/g,four).replace(/THREE/g,three).replace(/ONE Mathematical Web Application/g,one.substring(0,1).toUpperCase() + one.substring(1) + ' Poll').replace(/ONE/g,one.replace(/\ /g,'_').toLowerCase());
   document.getElementById('cyo' + icyo).innerHTML=dform;   
   document.getElementById('cyo0').innerHTML=document.getElementById('cyo0').innerHTML.replace(\"</td></tr>\",\"</td><td><div id=cyo\" + eval(icyo + 1) + \" style=vertical-align:top;display:inline;><a onclick=\\\" document.getElementById('cyo' + icyo).style.display='inline';  prepieceit(); \\\" title='Create Your Own Flat File Database' style='vertical-align:top;display:inline;text-decoration:underline;cursor:pointer;'>Create Your Own Flat File Database</a></div></td></tr>\");
   document.getElementById(three.toLowerCase() + 'listddl').value=listddl + '!';  
   numo=0;
   dform='';
   one='';
   two='';
   listddl=''; 
   listddldelim='';
   icyo++;
  } else if (four != '') {
   numo=0;
   dform='';
   one='';
   two='';
   listddl=''; 
   listddldelim='';
  }
}
function filter(oi) {
 var iois=oi.value;
 var wasi=iois;
 if (wasi.indexOf(\"' \") != -1 && wasi.indexOf('[') != -1) {
  iois=iois.replace(/\ \+\ /g, \"+\");
  iois=iois.replace(/\ \+/g, \"+\");
  iois=iois.replace(/\+\ /g, \"+\");
  iois=iois.replace(/\[\ /g, \"[\");
  iois=iois.replace(/\ \]\ /g, \"]\");
  iois=iois.replace(/\ \]/g, \"]\");
  iois=iois.replace(/\]\ /g, \"]\");
 }
 /" . "/iois=iois.replace(/\+/g, \"~ + char(43) + ~\");
 /" . "/iois=iois.replace(/\~/g, \"' + char(126) + '\");
 /" . "/iois=iois.replace(/\' \+ char\(126\) \+ \' \+ char\(43\) \+ \' \+ char\(126\) \+ \'/g,\"' + char(43) + '\");
 if (iois != oi.value) {
   oi.value=iois;
   oi.title+=' ... ' + was;
 } 
 return false;
}
setTimeout(doothers,1000);
</script>
</head>
<body><h1>Mathematical Flat File Web Applications</h1><h3>RJM Programming - April, 2017</h3>
<form method='POST' action='./flat_file_engine.php'>
Set Up SQL for a Fibonacci Sequence: <input onblur=filter(this); style=width:68%; type=text name=ddl id=ddl value=\"CREATE TABLE fibonacci_sequence (prevf,thisf,ratio); INSERT INTO fibonacci_sequence (prevf,thisf,ratio) VALUES (' [0_thisf]',' max([-1_thisf]+[0_thisf],1)',' max([-1_thisf]+[0_thisf],1)/[0_thisf]'); \"></input>
<input type=submit value='Set Up Flat File Database'></input>
</form>

<!--h4> ... or ... </h4-->

<table id=cyo0 style=width:100%;><tbody><tr><td><div id=cyo1 style='vertical-align:top;display:inline;'><a onclick=\"  document.getElementById('cyo' + icyo).style.display='inline';    prepieceit();  \" title='Create Your Own Flat File Database' style='vertical-align:top;display:inline;text-decoration:underline;cursor:pointer;'>Create Your Own Flat File Database</a></div></td></tr></tbody></table><br>
<table border=5><tbody><tr><td style=background-color:#f0f0f0;>
<form target='myiframe' method='POST' action='./flat_file_engine.php'><input name=ddl id=nameddl type=hidden value=''></input><h2>Click for Next Fibonacci Sequence Number</h2>
<input onclick=\"document.getElementById('nameddl').value=this.title; document.getElementById('myiframe').style.display='block'; document.getElementById('myfibonaccis').click(); \" style=background-color:yellow; type=button value=Next  title=\"CREATE TABLE fibonacci_sequence (prevf,thisf,ratio); INSERT INTO fibonacci_sequence (prevf,thisf,ratio) VALUES (' [0_thisf]',' max([-1_thisf]+[0_thisf],1)',' max([-1_thisf]+[0_thisf],1)/[0_thisf]');  \"></input>
 ... or More Than One?: <input onchange=\"if (this.value.length > 0) { document.getElementById('nameddl').value=this.title.replace('??',this.value); document.getElementById('myiframe').style.display='block'; numtimes=this.value; dofewtimes();  } \" style=background-color:white; min=2 max=10 type=number value=2 title=\"CREATE TABLE fibonacci_sequence (prevf,thisf,ratio); INSERT INTO fibonacci_sequence (prevf,thisf,ratio) VALUES (' [0_thisf]',' max([-1_thisf]+[0_thisf],1)',' max([-1_thisf]+[0_thisf],1)/[0_thisf]');  \"></input>
<br><br><input style=display:none; id=myfibonaccis type=submit value='Fibonacci Sequence Next Number'></input>
</form>
</td></tr></tbody></table>


<iframe name=myiframe id=myiframe style=display:block;width:100%;height:1800px; src='./flat_file_engine.php'></iframe>

</body>
</html>
";

?>
