It seems to me that maybe now I can post a question and someone will help (I have this problem with posting questions and they dont get answered, its a family curse or something
Its probably a very simple problem that I have overlooked because I am trying to just get this done, it has been a real pain in the rear getting it working.
I will post the code, and hopefully someone can help me out, I just dont get it, because it has worked every other time i have implemented it on a system.
but the data is very squirley. The data is output from spss (you dont really need to know what that is to help). and then i have a script that takes out all the crap that spss puts into its files, and then it turns the file into a sectioned comma delimited file.
then this script allows you to select how many files you want to open, asks you what database you are uploading to (currently only supports mysql right now). and then it cuts one file into strings using the /n as the explode seperator.
the file is then sectioned into arrays, with peices of the file in each section of the array.
each string looks like this until you hit the Data in this case the data starts at F (F is the data identifier), followed by the data, then another data id'er M, and ect... till the end of the string.
SEX, Frequency, Percent, ValidPercent, CumulativePercent, Valid, F, 627, 60.5, 60.5, 60.5, M, 409, 39.4, 39.4, 99.9, uk, 1, .1, .1, 100.0, Total, 1037, 100.0, 100.0,
each string contains a table name, the first word and then a comma, then the field names, then a data identifier then the data, followed by another data id, then more data ect.
now the program loops after the last field name, and loops through the data storing each variable in an array.
array(4) { [0]=> string(3) "627" [1]=> string(3) "409" [2]=> string(1) "1" [3]=> string(4) "1037" }
then the data is looped into a mysql database like so:
Code: Select all
$l = 0;
$cnnt = count($data12);
while($l < $cnnt)
{
echo "<br> INSERT INTO ".$tablename.
" <br> SET dataset =".$dataset2ї$l].
"<br>".$field1."=".$data12ї$l].
"<br> ".$field2."=".$data22ї$l].
"<br> ".$field3."=".$data32ї$l].
"<br> ".$field4."=".$dataname42ї$l]."<br>";
$query = "INSERT INTO $tablename SET dataset='$dataset2ї$l]', $field1='$data12ї$l]', $field2='$data22ї$l]', $field3='$data32ї$l]',$field4='$dataname42ї$l]'";
$result = mysql_query($query) or die(mysql_error());
++$l;
echo "<br> cnnt's value: ".$cnnt."<br>";
}here is what it returns if it helps. just one string.
Code: Select all
SEX, Frequency, Percent, ValidPercent, CumulativePercent, Valid, F, 627, 60.5, 60.5, 60.5, M, 409, 39.4, 39.4, 99.9, uk, 1, .1, .1, 100.0, Total, 1037, 100.0, 100.0,
instances count line:25
instances count -5:20
instances of floor /5:4
location of star str:58
stars new position:79
cnt's value:79
stars new position:100
cnt's value:100
stars new position:117
cnt's value:117
stars new position:
cnt's value:
SEX table successfully created!
Data count:
array(4) { ї0]=> string(3) "627" ї1]=> string(3) "409" ї2]=> string(1) "1" ї3]=> string(4) "1037" }
INSERT INTO SEX
SET dataset =F
Frequency=627
Percent=60.5
ValidPercent=60.5
CumulativePercent=79
cnnt's value: 4
INSERT INTO SEX
SET dataset =M
Frequency=409
Percent=39.4
ValidPercent=39.4
CumulativePercent=100
cnnt's value: 4
INSERT INTO SEX
SET dataset =uk
Frequency=1
Percent=.1
ValidPercent=.1
CumulativePercent=117
cnnt's value: 4
INSERT INTO SEX
SET dataset =Total
Frequency=1037
Percent=100.0
ValidPercent=100.0
CumulativePercent=
cnnt's value: 4here is my code:
Code: Select all
if($database == "mysql" && $yes == "yes")
{
echo "please input your servers address.
<input type="text" name="serveraddy"><br>";
echo"<font color=red>Must set Database name even with Default checked</font><br>please input your Database's name.
<input type="text" name="databasename"><br>";
echo"please input your username.
<input type="text" name="username"><br>";
echo"please input your password.
<input type="password" name="passwd"><br>";
echo"or Use default settings<br>"."<input name="default" type="checkbox" id="default" value="1"><br>";
for ($i = 0; $i < $manyfiles; $i++)
{
echo"<input type="file" name="filename".$i.""><br>";
}
$dir_name = "./data/";
$dir = opendir($dir_name);
echo "<font color="red"> here is a list of files in the directory <br></font>";
while ($file_name = readdir($dir))
{
if (($file_name != "." && $file_name != ".."))
{
print $file_name."<br>";
}
}
closedir($dir);
echo " <input type="submit" name="Submit" value="Submit">";
if($default == 1)
{
$serveraddy = "";
$databasename = "";
$username = "";
$passwd = "";
}
//stores the filenames into an array
$filearray1 = array("$filename0", "$filename1", "$filename2", "$filename3", "$filename4", "$filename5", "$filename6", "$filename7", "$filename8", "$filename9", "$filename10");//end of filenames into array
//
//Function to import the data into the database pain in my ass.
if(isset($serveraddy) && isset($databasename) && isset($username) && isset($passwd))
{
$h = 1;
while($h <= $manyfiles)
{
$i = 0; //Counter
for($p = 0; $p < $manyfiles; $p++)
{
if(isset($filearray1ї$p]))
{
echo "<br>is set ".$filearray1ї$p]."<br>";
}else{
echo "<br>is not set<br>";
exit;
}
}
for ($i = 0; $i < $manyfiles; $i++)
{
$fd = fopen($filearray1ї$i], "r+b");
$xfsize = filesize($filearray1ї$i]);
if(!isset($fd)){echo"no filename";}
while(!feof($fd))
{
$linez = fread($fd, $xfsize);
$lined = explode("/n", $linez);
$countt = count($lined);
for($t = 0; $t < $countt ; $t++)
{
$line = $linedї$t];
echo "<br>2:".$countt."<br>";
echo "<br>3:".$linedї$t]."<br>";
//if(isset($lined)) {echo $lined;} else {echo "no data from lined";)
$len_line = strlen($line); // Lenght of line
//get table name
$tablenamecomma = strpos($line, ",", 0); //Position first comma
$tablename = substr($line, 0, $tablenamecomma); //Cut string, name of table
//get field names
$fieldname1 = strpos($line, ",", $tablenamecomma + 1); //Position second comma
$fieldname2 = strpos($line, ",", $fieldname1 + 1); //Position third comma
$fieldname3 = strpos($line, ",", $fieldname2 + 1); //Position fourth comma
$fieldname4 = strpos($line, ",", $fieldname3 + 1); //Position fourth comma
$skip = strpos($line, ",", $fieldname4 + 1); //Position fourth comma
$datanameset =strpos($line, ",", $skip + 1);
$dataname1 = strpos($line, ",", $datanameset + 1);
$dataname2 = strpos($line, ",", $dataname1 + 1);
$dataname3 = strpos($line, ",", $dataname2 + 1);
$dataname4 = strpos($line, ",", $dataname3 + 1);
$field1 = substr($line, $tablenamecomma + 1, $fieldname1 - $tablenamecomma -1); //Cut the first fieldname
$field2 = substr($line, $fieldname1 + 1, $fieldname2 - $fieldname1 - 1); //Cut the second fieldname
$field3 = substr($line, $fieldname2 + 1, $fieldname3 - $fieldname2 - 1); //Cut the third fieldname
$field4 = substr($line, $fieldname3 + 1, $fieldname4 - $fieldname3 - 1); //Cut the fourth fieldname
/*
$dataset = substr($line, $skip + 1, $datanameset - $skip -1);
$data1 = substr($line, $datanameset + 1, $dataname1 - $datanameset -2);
$data2 = substr($line, $dataname1 + 1, $dataname2 - $dataname1 -1);
$data3 = substr($line, $dataname2 + 1, $dataname3 - $dataname2 -1);
$data4 = substr($line, $dataname3 + 1, $dataname4 - $dataname3 -1);
*/
$ctn = substr_count($line, ",");
$cont = ($ctn-5);
$cnt = floor($cont/5);
echo "<div align="left">instances count line:".$ctn."<br>";
echo "instances count -5:".$cont."<br>";
echo "instances of floor /5:".$cnt."<br>";
$g = 0;
$star = $skip;
echo "location of star str:".$star."<br>";
while($g < $cnt)
{
$datanameset2ї$g] =strpos($line, ",", $star + 1);
$dataname12ї$g] = strpos($line, ",", $datanameset2ї$g] + 1);
$dataname22ї$g] = strpos($line, ",", $dataname12ї$g] + 1);
$dataname32ї$g] = strpos($line, ",", $dataname22ї$g] + 1);
$dataname42ї$g] = strpos($line, ",", $dataname32ї$g] + 1);
$dataset2ї$g] = substr($line, $star + 1, $datanameset2ї$g] - $star -1);
$data12ї$g] = substr($line, $datanameset2ї$g] + 1, $dataname12ї$g] - $datanameset2ї$g] -1);
$data22ї$g] = substr($line, $dataname12ї$g] + 1, $dataname22ї$g] - $dataname12ї$g] -1);
$data32ї$g] = substr($line, $dataname22ї$g] + 1, $dataname32ї$g] - $dataname22ї$g] -1);
$data42ї$g] = substr($line, $dataname32ї$g] + 1, $dataname42ї$g]- $dataname32ї$g] -1);
$star = $dataname42ї$g];
echo "<br>stars new position:".$star;
echo "<br>cnt's value:".$star;
++$g;
}
$star = 0;
//Function to create Database, Tables, and fields if they dont exist. fun stuff eh?....no...its not.
$global_db = mysql_connect($serveraddy, $username, $passwd);
//Database function !database ? create one : fail
$db_list = mysql_list_dbs();
$cnt = mysql_num_rows($db_list);
if($t < $cnt)
{
if(mysql_db_name($db_list, $t) == $databasename)
{
$databasenametrue = 1;
mysql_select_db($databasename, $global_db);
}
if($databasenametrue !== 1)
{
mysql_create_db($databasename);
mysql_select_db($databasename, $global_db);
$databasenametrue = 1;
$createtable = 1;
}
}
//end of database function
//table function !table ? create one :fail
$sql = "CREATE TABLE ".$tablename." (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, dataset TEXT, ".$field1." TEXT, ".$field2." TEXT, ".$field3." TEXT, ".$field4." TEXT)";
if(mysql_query($sql)) { echo("<P>".$tablename." table successfully created!</P>");} else { echo("<P>Error creating ".$tablename." table: ".mysql_error()."</P>");}
//end of table function
//echo "<br><center></center> INSERT INTO ".$tablename." <br> SET dataset =".$dataset."<br>".$field1."=".$data1."<br> ".$field2."=".$data2."<br> ".$field3."=".$data3."<br> ".$field4."=".$data4."<br>";
//$query = "INSERT INTO $tablename SET dataset='$dataset',$field1='$data1', $field2='$data2', $field3='$data3',$field4='$data4'";
//$result = mysql_query($query) or die(mysql_error());
$l = 0;
$cnnt = count($data12);
echo "<br><font color=blue> Data count:<br></font><font color=red>";
var_dump($data12);
echo "<br></font>";
/*
echo "<br><font color=red> Number of loops:<br></font><font color=blue>";
var_dump($cnnt);
echo "<br></font>";
*/
while($l < $cnnt)
{
echo "<br> INSERT INTO ".$tablename." <br> SET dataset =".$dataset2ї$l]."<br>".$field1."=".$data12ї$l]."<br> ".$field2."=".$data22ї$l]."<br> ".$field3."=".$data32ї$l]."<br> ".$field4."=".$dataname42ї$l]."<br>";
$query = "INSERT INTO $tablename SET dataset='$dataset2ї$l]',$field1='$data12ї$l]', $field2='$data22ї$l]', $field3='$data32ї$l]',$field4='$dataname42ї$l]'";
$result = mysql_query($query) or die(mysql_error());
++$l;
echo "<br> cnnt's value: ".$cnnt."<br>";
}
$i = $i + 1;
}
}
}
fclose($fd); //Close the file
}
++$h;
print("".$i." records have been imported succesfully!</div>");
}
}This is not a hard script, its just that I have been staring at it for days, and for some reasone everything starts to blend together and... well im sure you can relate.
Thanks for any help,
-=Levi=-