Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    foreach help on a parser script?

    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



    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'

    PHP Code:
    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

  • #2
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    Looks like maybe you have the $i in the wrong spot.

    PHP Code:
    $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++;


  • Users who have thanked Coyote6 for this post:

    stfc_boy (09-11-2009)

  • #3
    Regular Coder
    Join Date
    May 2009
    Posts
    143
    Thanks
    14
    Thanked 1 Time in 1 Post
    why is that numerical value set in quotes? $i = 13454 is enough


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •