Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 39

Thread: PHP+file handling+confused???

  1. #21
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey, it gives a Parse error: parse error, unexpected $ in /var/www/html/shachi/php/test/testup.php on line 38
    Last edited by shachi; 07-13-2006 at 06:55 PM.

  2. #22
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey: Are you around??

  3. #23
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Any help here??

  4. #24
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Any Help Here???

  5. #25
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Whoops, typo. Try this one:
    Code:
    <?php
    define('DATABASE_FILE', 'database_fake.php');
    define('SEPARATOR', ',');
    define('LINEBREAK', "\n");
    
    define('USERNAME', 0);
    define('PASSWORD', 1);
    define('EMAIL', 2);
    define('COUNTRY', 3);
    define('ZIP', 4);
    
    function updateUser() {
      $ret = 0;
      $lines = array_unique([url=http://www.php.net/file]file[/file](DATABASE_FILE));
      $argv = func_get_args();
      $conditions = array();
      $changes = array();
      for($i = 0; $i < count($argv) && $argv[$i] != SEPARATOR; $i += 2)
        $conditions[$argv[$i]] = $argv[$i + 1];
      for(++$i; $i < count($argv); $i += 2)
        $changes[$argv[$i]] = $argv[$i + 1];
      for($i = 0; $i < count($lines); ++$i) {
        $cline = explode(SEPARATOR, $lines[$i]);
        $changeThisLine = false;
        foreach($conditions as $k => $v)
          if($changeThisLine = ($cline[$k] == $v)) continue;
        foreach($changes as $k => $v)
          $cline[$k] = $v;
        ++$ret;
      }
      $f = fopen(DATABASE_FILE, 'w');
      fwrite($f, str_replace(LINEBREAK . LINEBREAK, LINEBREAK, implode("\n", $lines)));
      fclose($f);
      return $ret;
    }
    updateUser(USERNAME, 'Shachi', COUNTRY, 'nepal', SEPARATOR, COUNTRY, 'Nepal', EMAIL, 'some.new@email-address.com');
    ?>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #26
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey, It didn't work.

    But I tried to update the code from liro from the other post and came up with something like this:

    (For test purposes used online.txt with | as separator the original database will have ,)
    Code:
    <?php 
    function replaceLine($f, $etsi, $first, $second) {
    if(!file_exists($f)) {
            die("Sorry the file " . $f . " could not be found.");
        } else {
        $filearr = file($f);
        $sisalto;
    
            for($i = 0; $i < count($filearr); $i++) {
                list($id, $nimi, $passu) = explode("|", $filearr[$i]);
    if($first == ""){
    $first = $nimi;
    } 
    if($second == ""){
    $second = $passu;
    }
                if($id == $etsi) {
                    $sisalto .= $id . "|" . $first . "|" . $second . "\n";
                } else {
                    $sisalto .= $filearr[$i];
                }
            }
            $fi = fopen($f, "w");
            fwrite($fi, $sisalto);
            fclose($fi);
        }
    }
    replaceLine("online.txt", "Test7", "", "");
    ?>
    But it has a problem that if the values are not supplied it replaces the second and third values with the second and third values of the first line. I need a function to loop through every line of the array searching for a key and then give the index number of the array. So far I have made something like this:

    Code:
    <?php
    $loopIndex = 0;
    $test = array('Test0','Test1','Test2','Test3');
    #$test = file("online.txt");
    while($test[$loopIndex] != "Test4|test5|test6" && $loopIndex < 4){
    $loopIndex++;
    }
    if($loopIndex < 4){
    echo "Found supplied value at index ".$loopIndex;
    }
    #echo $test[3];
    ?>
    online.txt looks something like this:

    Code:
    Test1|test2|test3
    Test4|test5|test6
    Test7|test8|test9
    Test10|test11|test12
    But as soon as I uncomment the $test = file("online.txt"); it shows nothing.
    I have tried my best but could not come up with a suitable code. I have almost lost.

    Any ideas??

  7. #27
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Twey, It didn't work.
    You ought to know by now not to say "it didn't work." What went wrong?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #28
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oops!! Sorry. It doesn't update the file. I have been trying to fix it but unforutnately I don't have much brain about PHP.

  9. #29
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This is tested:
    Code:
    <?php
    define('DATABASE_FILE', 'database_fake.php');
    define('SEPARATOR', ',');
    define('LINEBREAK', "\n");
    
    define('USERNAME', 0);
    define('PASSWORD', 1);
    define('EMAIL', 2);
    define('COUNTRY', 3);
    define('ZIP', 4);
    
    function updateUser() {
      $ret = 0;
      $lines = array_unique(file(DATABASE_FILE));
      $argv = func_get_args();
      $conditions = array();
      $changes = array();
      for($i = 0; $i < count($argv) && $argv[$i] !== SEPARATOR; $i += 2)
        $conditions[$argv[$i]] = $argv[$i + 1];
      for(++$i; $i < count($argv); $i += 2)
        $changes[$argv[$i]] = $argv[$i + 1];
      for($i = 0; $i < count($lines); ++$i) {
        $cline = explode(SEPARATOR, $lines[$i]);
        $changeThisLine = false;
        foreach($conditions as $k => $v)
          if(!($changeThisLine = ($cline[$k] == $v))) break;
        foreach($changes as $k => $v)
          $cline[$k] = $v;
        $lines[$i] = implode(SEPARATOR, $cline);
        ++$ret;
      }
      $f = fopen(DATABASE_FILE, 'w');
      fwrite($f, str_replace(LINEBREAK . LINEBREAK, LINEBREAK, implode("\n", $lines)));
      fclose($f);
      return $ret;
    }
    updateUser(USERNAME, 'Shachi', COUNTRY, 'nepal', SEPARATOR, COUNTRY, 'NePaL', EMAIL, 'some.new@email-address.com');
    ?>
    It works, but not with the line of PHP code in the file. You should remove it.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #30
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey, it edits all the user's information. For example if the database_fake.php is like this:

    Code:
    Test,test,test@test.com,sweden
    testuser,test,test@user.com,uk
    Then after executing the PHP script it changes the values to :

    Code:
    Test,test,some.new@email-address.com,NePaL
    testuser,test,some.new@email-address.com,NePaL
    And by the way if I remove the <?php exit()?> lines then anyone would be able to view the database(sure I could encrypt the password with md5 hash but what about the email addresses??They'd be a great source for spambots).

    I am sorry to make you write bunch of codes and take up your time but I need help and I cannot do it myself. You are more experienced than me.

Bookmarks

Posting Permissions

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