Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 39

Thread: PHP+file handling+confused???

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

    Default

    Code:
    // Defines.
    define('DATABASE_FILE', 'database.php');
    define('SEPARATOR', ',');
    define('LINEBREAK', "\n");
    
    define('USERNAME', 0);
    define('PASSWORD', 1);
    define('EMAIL', 2);
    define('COUNTRY', 3);
    define('ZIP', 4);
    
    function updateUser($ccol, $cval, $col, $val) {
      $ret = 0;
      $lines = file(DATABASE_FILE); // Read DATABASE_FILE into $lines as an array, broken at the linebreaks.
      for($i = 0; $i < count($lines); ++$i) {
        $cline = explode(SEPARATOR, $lines[$i]); // Read $lines[$i] into $cline as an array, broken at SEPARATOR.
        if($cline[$ccol] == $cval) { // If this line matches the condition given,
          $cline[$col] = $val; // set the specified column to the specified value.
          $lines[$i] = implode(SEPARATOR, $cline); // Put $cline back together again, and insert it back into $lines.
          ++$ret;
        }
      }
      $f = fopen(DATABASE_FILE, 'w'); // Open the file.  Truncate to zero length (basically delete the file and create a new one; if memory corruption occurs here, bye-bye database)
      fwrite($f, implode(LINE_BREAK, $lines)); // Put the lines back together again and write them back to the database
      fclose($f);
      return $ret; // Return the number of lines matched and modified
    }
    Then, to modify the user with the username 'Twey' and set his country to "England":
    Code:
    updateUser(USERNAME, 'Twey', COUNTRY, 'England');
    Sorry for the delay, I thought I'd posted, but evidently hadn't.
    Last edited by Twey; 07-11-2006 at 02:27 PM. Reason: Comment and link to docs.
    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!

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

    Default

    Never mind. Thanks for the script!!! I will try this right away. Twey By the way it'd have been a lot better if you posted it with remarks on what one line does. But never mind I'll try to figure it out myself. Thank you again.

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

    Default

    There, all nicely linked and commented.
    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!

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

    Default

    Oh thanks, but I think I have a problem now. Either I made a mistake in installing that script or there is some problem(I think I made a mistake but I am not sure). It doesn't update the name but adds a new line before the first line of the file.

    I mean that if the database.php looks like this at first:

    PHP Code:
    <?php exit()?>
    Shachi,test,shachibista[at]gmail[dot]com,nepal
    After running that script it becomes:

    PHP Code:
    <?php exit()?>

    Shachi,test,shachibista[at]gmail[dot]com,nepal
    I don't know what the problem is but for testing purposes I just made a new file and tried executing that script. The new file looks sth like this:

    PHP 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($ccol$cval$col$val) {
      
    $ret 0;
      
    $lines file(DATABASE_FILE);
      for(
    $i 0$i count($lines); ++$i) {
        
    $cline explode(SEPARATOR$lines[$i]);
        if(
    $cline[$ccol] == $cval) {
          
    $cline[$col] = $val;
          
    $lines[$i] = implode(SEPARATOR$cline);
          ++
    $ret;
        }
      }
      
    $f fopen(DATABASE_FILE'w');
      
    fwrite($fimplode("\n"$lines));
      
    fclose($f);
      return 
    $ret;
    }
    updateUser(USERNAME'Shachi'COUNTRY'Nepal');
    ?>

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

    Default

    Well, first let's make sure you understand what you're doing here -- your code says you does, but your text was a little vague.
    Code:
    updateUser(USERNAME, 'Shachi', COUNTRY, 'Nepal');
    should find all users with the username "Shachi" and set their country to "Nepal." Is this what you intended?

    Anyway, I've actually tested this, and it works:
    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($ccol, $cval, $col, $val) {
      $ret = 0;
      $lines = array_unique(file(DATABASE_FILE));
      for($i = 0; $i < count($lines); ++$i) {
        $cline = explode(SEPARATOR, $lines[$i]);
        if($cline[$ccol] == $cval) {
          $cline[$col] = $val;
          $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');
    ?>
    The empty lines it prepended weren't really a problem, but they were annoying, so I removed them, along with any duplicate rows found in the database.

    However, for me the script worked from the start, albeit with aforementioned prepended lines, so this is no guarantee it'll work for you. Try it and see.
    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. #16
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That is pretty much what I want. But there is a problem that the format of the data should be constant(username,password,email,country,zip) but anyways I think I found out a way but not sure it works right.

    PHP Code:
    $key $_POST['user'];
    $fc=file("database.php");
    $f=fopen("database.php","w");
    foreach(
    $fc as $line)
    {
         if (!
    strstr($line,$key))
               
    fputs($f,$line);
    }
    fclose($f);
    $f=fopen("database.php","a");
    fwrite($f$_POST['user'].','.$_POST['pass'].','.$_POST['email'].','.$_POST['country'].','.$_POST['zip']."\n");
    fclose($f); 
    What this does is finds the line with the username(which is unique through out the file) and then deletes that line and add a line at the end of the file with the username(will be submitted with the update form) password(is a must for updating profile) and country and zip will be also submitted with the form submission. I still need to write a conditional statement to not to let the user if the submitted data matches the one in the file. But what do you think Twey??

  7. #17
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    At first I thought the script I supplied in my earlier post would work but now I have a great problem because if the user doesn't supply anything for a certain value for e.g if a user wants to update just his password then it won't work the file will just append the data the user entered. Twey, or anyone else please help if anyone knows a fix for this problem. HELP!!!!

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

    Default

    But what do you think Twey??
    I think that what you say it would do is exactly the same as what mine does, except for the new line
    But there is a problem that the format of the data should be constant(username,password,email,country,zip)
    Er, it is in my code. If you want to change the order, move the defines around.
    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!

  9. #19
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Er, it is in my code. If you want to change the order, move the defines around.
    Move the defines around?? I couldn't get you. If I place the defines in some other order will it affect the code?? Please forgive me for my silly questions but I am just a newbie. And can you instruct me on how to make the function to accept more arguements(other than username and country).

    EDIT: Never mind, I found out just place two other attribute vars in the function like function($test1,$test2) and place this between the if condition $cline[$test1] = $test2. Thanks Twey for all your help time and support.
    Last edited by shachi; 07-13-2006 at 05:15 PM.

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

    Default

    Try this. I'm not too sure of it, though, since it uses quite a few functions I'm not all that familiar with.
    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');
    ?>
    Note that each argument works as an "or" operand.
    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!

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
  •