/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Loops gone wild!!!!

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>[/code]

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

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@scragarOct 12.2008 — try:
[code=php]
////// THE DREADED LOOP

for ($i = 0 ; $i < $number_of_seq ; $i++)
{
echo "<p>
nHere is the sequence id: {$seq_id[$i]}
n<br/>
Here is the sequence type: {$seq_type[$i]}
n<br/>
Here is the sequence: {$seq_data[$i]} </p>" ;

$query = "INSERT into sequences VALUES ('{$seq_id[$i]}', '{$seq_type[$i]}', '{$seq_data[$i]}')";
$result = mysql_query($query) or die('Data insertion failed:'.mysql_error() ) ;

echo "<p>{$query}</p>";
}[/code]

in place of your current loop, it should give you better data and help you spot your problem.



PS: If your getting testing the start of the string a few times for different starting characters you find it better to use [url=http://php.net/substr]substr($text, 0, 2)[/url] which will return the first 2 characters of the string.
Copy linkTweet thisAlerts:
@TheDragonRebornauthorOct 12.2008 — Still not resolving crazy loops. Here is the output page:

[code=html]Successful connection !! Successful selection of database !!
Sequences in Database

Here is the sequence id:
Here is the sequence type:
Here is the sequence:

Here is the sequence id: Q04917
Here is the sequence type:
Here is the sequence:

Here is the sequence id:
Here is the sequence type:
Here is the sequence:

Here is the sequence id:
Here is the sequence type:
Here is the sequence:

... EVEN MORE REPEATS !!! ELIMINATED THEM TO FIT OUTPUT

Here is the sequence id:
Here is the sequence type:
Here is the sequence:

Here is the sequence id:
Here is the sequence type:
Here is the sequence:

Here is the sequence id:
Here is the sequence type:
Here is the sequence:

Here is the sequence id:
Here is the sequence type: PRT
Here is the sequence: MGDREQLLQR ARLAEQAERY DDMASAMKAV TELNEPLSNE DRNLLSVAYK NVVGARRSSW RVISSIEQKT MADGNEKKLE KVKAYREKIE KELETVCNDV LSLLDKFLIK NCNDFQYESK VFYLKMKGDY YRYLAEVASG EKKNSVVEAS EAAYKEAFEI SKEQMQPTHP IRLGLALNFS VFYYEIQNAP EQACLLAKQA FDDAIAELDT LNEDSYKDST LIMQLLRDNL TLWTSDQQDE EAGEGN

Here is the sequence id:
Here is the sequence type: PRT
Here is the sequence: MGDREQLLQR ARLAEQAERY DDMASAMKAV TELNEPLSNE DRNLLSVAYK NVVGARRSSW RVISSIEQKT MADGNEKKLE KVKAYREKIE KELETVCNDV LSLLDKFLIK NCNDFQYESK VFYLKMKGDY YRYLAEVASG EKKNSVVEAS EAAYKEAFEI SKEQMQPTHP IRLGLALNFS VFYYEIQNAP EQACLLAKQA FDDAIAELDT LNEDSYKDST LIMQLLRDNL TLWTSDQQDE EAGEGN

Here is the sequence id:
Here is the sequence type:
Here is the sequence:
[/code]


What I want is just

[code=html]Here is the sequence id: Q04917
Here is the sequence type: PRT
Here is the sequence: MGDREQLLQR ARLAEQAERY DDMASAMKAV TELNEPLSNE DRNLLSVAYK NVVGARRSSW RVISSIEQKT MADGNEKKLE KVKAYREKIE KELETVCNDV LSLLDKFLIK NCNDFQYESK VFYLKMKGDY YRYLAEVASG EKKNSVVEAS EAAYKEAFEI SKEQMQPTHP IRLGLALNFS VFYYEIQNAP EQACLLAKQA FDDAIAELDT LNEDSYKDST LIMQLLRDNL TLWTSDQQDE EAGEGN[/code]


Any suggestions???
Copy linkTweet thisAlerts:
@scragarOct 12.2008 — ok, then the loop you commented as being bad isn't the nasty one, it's your assignment of the variables, proberly because of your use of an index which isn't exactly well thought out.

TRY:
[code=php]
while($text = fgets($fh))
{
$tmp = substr($text, 0, 2);
if($tmp == "AC")
{
$seq_id[] = substr($text, 5, 5);

// Easy way to empty an array :p
$sequence = Array();

}
// Stop at sequences and collect
if($tmp == "SQ")
{
// 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[] = "PRT" ;
break ;

}
else
{
$seq_type[] = "DNA" ;
}
}


// Turn array into long string
$seq_data[] = implode($sequence);

}

// Get total number of sequences for "for-loop"
$number_of_seq = count($seq_id);
...
[/code]
should work better, let me know.
Copy linkTweet thisAlerts:
@TheDragonRebornauthorOct 12.2008 — Thanks for the tips ( I know my code isn't very professional). Good news is that if I use a file with one entry, it works. However, when I load a file with multiple sequences, it isn't the sequence part; only the seq_id and seq_type.

Here is the code

[code=php]while($text = fgets($fh))
{

$tmp = substr($text, 0, 2) ;

// Grab the seq_id from lines that start with 'AC'
if($tmp == "AC")
{
$seq_id[] = substr($text, 5, 5);

// Easy way to empty an array :p
$sequence = Array();
}

// Stop at sequences and collect
if($tmp == "SQ")
{
// 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[] = "PRT" ;
break ;

}
else
{
$seq_type[] = "DNA" ;
}
}


// Turn array into long string
$seq_data[] = 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 ;

}
[/code]


I'm not sure but the line

[code=php] // Turn array into long string
$seq_data[] = implode($sequence) ;[/code]


seems to be the culprit.


output looks like this

Successful connection !! Successful selection of database !!

Sequences in Database

Here is the sequence id: P0070

Here is the sequence type: PRT

Here is the sequence:

Here is the sequence id: Q8N1E

Here is the sequence type: PRT

Here is the sequence:

Here is the sequence id: Q86SG

Here is the sequence type: PRT

Here is the sequence:

Here is the sequence id: P6162

Here is the sequence type: PRT

Here is the sequence:

Here is the sequence id: Q6UWQ

Here is the sequence type: PRT

Here is the sequence:

Here is the sequence id: Q7Z4W

Here is the sequence type: PRT

Here is the sequence:

Here is the sequence id: Q96KX

Here is the sequence type: PRT

Here is the sequence:

Here is the sequence id: Q96QH

Here is the sequence type: PRT

Here is the sequence:

Here is the sequence id: O7595

Here is the sequence type: PRT

Here is the sequence:

Here is the sequence id: Q8IXA

Here is the sequence type: PRT

Here is the sequence:


???
Copy linkTweet thisAlerts:
@scragarOct 12.2008 — [code=php]while($text = fgets($fh))
{

$tmp = substr($text, 0, 2) ;

// Grab the seq_id from lines that start with 'AC'
if($tmp == "AC")
{
$seq_id[] = substr($text, 5, 5);


// here we set the sequence for the last loop
if(count($seq_id) == 1)
$seq_data[] = implode($sequence) ;


$sequence = Array();
}

// Stop at sequences and collect
if($tmp == "SQ")
{
// Collect sequence and add to array one at a time
while( ($c = fgetc($fh)) != "/")
{
array_push($sequence,$c) ;
}

}

foreach($sequence as $char)
{
if($char != 'A' || $char != 'C' || $char != 'G' || $char != 'T')
{
$seq_type[] = "PRT" ;
break ;

}
else
{
$seq_type[] = "DNA" ;
}
}


// Turn array into long string
}
// and here we set the sequence for the very last loop
$seq_data[] = implode($sequence) ;
[/code]


let me know if that solves the problem(I havn't been able to test it or anything, but it looks right).
Copy linkTweet thisAlerts:
@TheDragonRebornauthorOct 12.2008 — No luck, printing only:

[code=html]Successful connection !! Successful selection of database !!
Sequences in Database

Here is the sequence id: Q0491
Here is the sequence type: PRT
Here is the sequence:[/code]




I appreciate the time your spending to help.
Copy linkTweet thisAlerts:
@scragarOct 12.2008 — Doh!

implode($glue, $peices);

replace where I've got[code=php]$seq_data[] = implode($sequence) ;[/code]
with[code=php]$seq_data[] = implode('', $sequence);[/code]
Copy linkTweet thisAlerts:
@TheDragonRebornauthorOct 12.2008 — Still no luck. Not picking up sequence. Strange because this wasn't an issue before.
Copy linkTweet thisAlerts:
@TheDragonRebornauthorOct 12.2008 — SORRY. I take it back, its working fine. Ignore last post. ?

Thanks again for the help. Greatly appreciated.
Copy linkTweet thisAlerts:
@scragarOct 12.2008 — OK

EDIT: oh, right ?
×

Success!

Help @TheDragonReborn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...