...

View Full Version : foreach help on a parser script?



stfc_boy
09-11-2009, 08:22 PM
Hello All,

Iíve put together a script which takes a tab delimited file with surnames in it and grabs those names and puts them in a database table:

Now hereís a snippet of what the tab delimited file looks like

http://www.inspireaway.co.uk/td.jpg

Now as you will see the tab delimited file grabs the values across and then moves onto the next line and works across again etc, and it works nicely.

Now my problem in my script is one thing where I create an $i value and add one to it

$i = '13454'



foreach ( $data as $line ) {
$val = trim ($line);
$arr = explode ("\t", $val);
$season = '20042005'; //Define the season
$i = '13454' //Increment by one after every LINE - This is the problem!
foreach($arr as $val) {
$val_new = str_replace("#", "", $val); //Replace # in excel
$query = "INSERT INTO parser_player_names (player_name,report_id,season) VALUES ('".$val_new."','".$i."','".$season."')";
mysql_query($query)
or die(mysql_error());
$i++;
}
}


Basically here iíve set my value to 1354 and I want it to increment by one (i++ which it does) by ONLY increment by one AFTER EACH LINE, and NOT after each individual record has been parsed into my database which it currently does.

Can anyone help

Thanks

Coyote6
09-11-2009, 08:33 PM
Looks like maybe you have the $i in the wrong spot.



$i = '13454' //Increment by one after every LINE - This is the problem!

foreach ( $data as $line ) {
$val = trim ($line);
$arr = explode ("\t", $val);
$season = '20042005'; //Define the season
foreach($arr as $val) {
$val_new = str_replace("#", "", $val); //Replace # in excel
$query = "INSERT INTO parser_player_names (player_name,report_id,season) VALUES ('".$val_new."','".$i."','".$season."')";
mysql_query($query)
or die(mysql_error());
}
$i++;
}

Jahren
09-11-2009, 09:11 PM
why is that numerical value set in quotes? $i = 13454 is enough



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum