Hello to all,
I am having an issue with a program and was hoping someone could offer some advice. Please keep in mind that I am a novice programmer and that I may not be as fluent as I should be in the lingo of PHP. Everything works fine (mySQL connection, file upload, data extraction). My only issue is when it comes to the loops I am using to print my data and put that data into a database. For some reason, the php page is repeating certain lines dozens of times, and the lines that contain data multiple times. What is expected is that each entry should be displayed once and entered into the database once.
That being said, Here is my code.
[code=php]<?php
/////////////////////////////////////////////////////////////////////////////
////////// CONNECT TO DATABASE AND OPEN FILE //////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Eliminate Error Notice
error_reporting(E_ALL & ~E_NOTICE) ;
// This will get an input file from an html page and deposit it
// into a mySQL database
// Last modified – 10/12/08
// Test if file was uploaded
if(! $_FILES[‘dataFile’][‘tmp_name’])
{
echo “<html><body><h1>No file uploaded</h1></body></html>” ;
exit ;
}
// Test connection to mySQL
// Don’t forget to change the password to ‘*******’ !!!!!
$link = mysql_connect( ‘localhost’, ‘******’, ‘*******’ )
or die( ‘Could not connect to mySQL:’.mysql_error() ) ;
echo “Successful connection !! nn” ;
// Test connectionto database ‘gkoenig’
mysql_select_db( ‘gkoenig’)
or die(‘Could not connect to database!’) ;
echo “Successful selection of database !! nnn” ;
// This is where we input the file into the database
// using mySQL commands
$fh = fopen( $_FILES[‘dataFile’][‘tmp_name’], ‘r’) ;
//////////////////////////////////////////////////////////////////////////
//////////// GET SEQUENCE(S) FROM FLAT FILE ////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Parse through Swiss-Prot file and extract the seq_id (Accession),
// seq_type (PRT,DNA, RNA), and seq_data
// Enumerate different sequences added
$count = 0 ;
$seq_id = Array() ;
$seq_type = Array() ;
$sequence = Array() ; // Data split into array (Buffer for sequence data)
$seq_data = Array() ; // Data stored in array starting
//$token = Array() ;
//$switch = True ;
while($text = fgets($fh))
{
// Grab the seq_id from lines that start with ‘AC’
if(strstr(“$text”, “AC”) AND (strpos(“$text”, “AC”)==0))
{
// $token = strtok($text, ” nt”) ;
// $seq_id = $token[1] ;
// $switch = False ;
$line = str_split($text) ;
$seq_id[$count] = $line[5].$line[6].$line[7].$line[8].$line[9].$line[10] ;
// Don’t stop at anymore “AC” strings that may be in file
//$switch = False ;
// Empty sequence array so that it can accept new data
while ($sequence)
{
array_pop($sequence) ;
}
}
// Stop at sequences and collect
if(strstr(“$text”, “SQ”) AND (strpos(“$text”, “SQ”)==0))
{
// Collect sequence and add to array one at a time
while( ($c = fgetc($fh)) != “/”)
{
array_push($sequence,$c) ;
}
}
// Determine the sequence type
foreach($sequence as $char)
{
if($char != ‘A’ || $char != ‘C’ || $char != ‘G’ || $char != ‘T’)
{
$seq_type[$count] = “PRT” ;
break ;
}
else
{
$seq_type[$count] = “DNA” ;
}
}
// Turn array into long string
$seq_data[$count] = implode($sequence) ;
// Turn switch back on after sequence is collected so that
// if multiple sequences are present, it can begin anew
// The next “AC” after sequence would be from new sequence
// Hopefull no “AC” in first line
//$switch = True ;
// In case of more then one sequence
$count += 1 ;
}
// Get total number of sequences for “for-loop”
$number_of_seq = $count+1 ; // Started $count at 0
?>
<html>
<head>
<title>Sequences in Database</title>
</head>
<body>
<h1>Sequences in Database</h1>
<?php
[SIZE=”6″][COLOR=”Red”]THE DREADED LOOP[/COLOR][/SIZE]
/// TAKE THIS OUT AFTER TEST !!!!!
for ($i = 0 ; $i < $number_of_seq ; $i++)
{
echo “<br/>” ;
echo ” nnHere is the sequence id: $seq_id[$i] nn<br/>” ;
echo ” Here is the sequence type:$seq_type[$i] nn<br/>” ;
echo ” Here is the sequence:$seq_data[$i] ” ;
}
//////////////////////////////////////////////////////////////////
/////////// INSERTION INTO DATABASE ///////////////////////////
//////////////////////////////////////////////////////////////////
[SIZE=”6″][COLOR=”Red”]THE DREADED LOOP, AGAIN[/COLOR][/SIZE]
// Add all sequences to database (DON’T FORGET THAT ARRAY STARTS AT 0)
for($x = 0 ; $x < $number_of_seq ; $x++)
{
// Put data into Database
$query = “INSERT into sequences VALUES (‘”.$seq_id[$x].”‘, ‘”.$seq_type[$x].”‘, ‘”.$seq_data[$x].”‘) ” ;
$result = mysql_query($query)
or die(‘Data insertion failed:’.mysql_error() ) ;
}
// Show data that was input into ‘sequences’ Database
$query2 = ‘SELECT * from sequences’ ;
$result2 = mysql_query($query2)
or die(‘Query failed:’.mysql_error() ) ;
echo “<table border=1> n” ;
while( $row = mysql_fetch_assoc($result2) )
{
echo “t<tr>t” ;
foreach ($row as $col)
{
echo “tt<td>$col</td>n” ;
}
echo “t</tr>n” ;
}
echo “</table>n” ;
?>
</body>
</html>
Any suggestions would be great. Also, I am using an HTML page to input the file to the php action file. I do not this could be the source, but I figured I would mention it.
Thank you