Can this be done?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken

    Can this be done?

    How do I save the values (not variables) for the variables in a file?

    Example:
    $_SESSION['category'] = "TEST";
    $url_read = readfile.php;
    $url_write = writefile.php;

    $sort = "var ".$_SESSION['category'];

    // read file
    $f_r = file($url_read) ;
    $read_file = fopen($url_read , "r");

    // write to file
    $f_w = file($url_write );
    $write_file = fopen($url_writ e, "wb");

    $key="sort";
    foreach($f_r as $line) {
    $sorte = strstr($line, $key); //look for $key in each line
    fwrite($write_f ile,$sorte);
    }
    fclose($write_f ile);
    fclose($read_fi le);

    The results of the above in writefile.txt is sort = "var
    ".$_SESSION['category'];
    In the writefile.txt, I am looking for: var TEST

    I have tried several approaches with no results.

    Thanks for the help.

    Ken


  • Ken Robinson

    #2
    Re: Can this be done?


    Ken wrote:[color=blue]
    > How do I save the values (not variables) for the variables in a file?
    >
    > Example:
    > $_SESSION['category'] = "TEST";
    > $url_read = readfile.php;
    > $url_write = writefile.php;
    >
    > $sort = "var ".$_SESSION['category'];[/color]

    Why are you setting this? You're not using it later.
    [color=blue]
    >
    > // read file
    > $f_r = file($url_read) ;
    > $read_file = fopen($url_read , "r");[/color]

    Don't need the above line, you've already read the file.[color=blue]
    >
    > // write to file
    > $f_w = file($url_write );[/color]

    Don't need the above line. Why are you reading in the file you're going
    to be writing?
    [color=blue]
    > $write_file = fopen($url_writ e, "wb");
    >
    > $key="sort";
    > foreach($f_r as $line) {
    > $sorte = strstr($line, $key); //look for $key in each line[/color]

    What does the variable $sorte contain here?
    [color=blue]
    > fwrite($write_f ile,$sorte);[/color]

    The above writes the value of $sorte to the file.
    [color=blue]
    > }
    > fclose($write_f ile);
    > fclose($read_fi le);[/color]

    If you remove the fopen for $read_file, remove the above also.
    [color=blue]
    >
    > The results of the above in writefile.txt is sort = "var
    > ".$_SESSION['category'];
    > In the writefile.txt, I am looking for: var TEST
    >
    > I have tried several approaches with no results.[/color]

    What have you tried?

    Tell us or show us what's in your input file. What do you expect to get
    out as a result?

    Ken

    Comment

    Working...