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 10 of 10
  1. #1
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Finding in a file, then editing part of the file.

    I currently have
    PHP Code:
     $strPagetitle 'Grand Prix of Australia 2004'
    as part of a page, I need a script which will find a line which starts with
    PHP Code:
     $strPagetitle =  
    (note the space in the beginning) and will then put what is inside the 's in a TextField, and then you can submit a form and it will change what is inside the 's, how can this be done? It is in a file called australia.php

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I know that it sounds like a PHP question, but I think it's really a PERL question (even though you're using PHP to implement it..). You may want to ask over there cuz they'll know better (I think).. like how to open the file and edit a particular part of a file. I'm assuming you can build the form and pass/accept the needed data from the form..

    Sadiq.

  • #3
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by sad69
    I know that it sounds like a PHP question, but I think it's really a PERL question (even though you're using PHP to implement it..). You may want to ask over there cuz they'll know better (I think).. like how to open the file and edit a particular part of a file. I'm assuming you can build the form and pass/accept the needed data from the form..

    Sadiq.
    OK, but by saying pass/accept I take it you mean check that what's been put in valid?
    Last edited by gsnedders; 05-27-2004 at 12:23 AM.

  • #4
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Found this: http://uk2.php.net/manual/en/functio...tr-replace.php
    Now I can do it!

    How do I do this to another file?
    Last edited by gsnedders; 05-29-2004 at 11:40 PM.

  • #5
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First it's hard to visualize but after you realize everytime you do something to a text file you iterates through the file rows and buffers the content then it becomes easy.

    I am tired from work now. I will post some code later today after I sleep a bit.
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials

  • #6
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by carl_mcdade
    I am tired from work now. I will post some code later today after I sleep a bit.
    This is 2 days later, and still no code...

  • #7
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Acck sorry!!!!!!

    I was tired and forgot I even posted.

    Okay let's take a step at a time then I'll test the code. First you need some data.

    Code:
    ============= australia.php=================
    $strPagetitle = 'Grand Prix of Australia 2004';
    $strPageAuthor = 'Alan Henry';
    $strPagebook = 'hardcover';
    $strPageReview = 'exciting stuff';
    $strPageScore = '4 starts';
    $strPageAmazonUrl = 'http://www.amazon.com';
    ============= australia.php=================
    
    ============= updateaustralia.php=================
    <?php //Load the array with the file rows
    $getData = file("ua.php");
    $numLines = count( $getData );
    $rowKey = "strPageAuthor";
    $rowValueString = "Charlie Finlay";
    
    
    // Look for the right line and set marker
    foreach ($getData as $key => $value) {
       
       if(ereg($rowKey,$value)){
        $rowMarker = $key;
       }
    echo "<br>Key: $key; Value:". htmlentities($value)."<br>\n"; 
    
    }
    echo "Update row key is: ".$rowMarker;
    
    // Make changes in the temp variable using the mark
    $getData[$rowMarker] = "$".$rowKey. " = '" . $rowValueString."';\n";
    er
    
    //Write new file
    $putData = fopen ("ua.php", "w");
    
    for($i=0; $i<$numLines; $i++) {
    
          fwrite($putData, $getData[$i]);
    
       }
    
    fclose($putData);
    ?>
    ============= updateaustralia.php=================
    CODE TESTED NOW!
    Last edited by carl_mcdade; 06-01-2004 at 09:10 PM.
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials

  • #8
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A tip: I would do this in a seperate file and just run through every line regardless. So the form would harvest the entire file and show textfields then a submit sould update every line. Because finding a $ is ***** because it plays havok with all the search function of PHP.
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials

  • #9
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by carl_mcdade
    A tip: I would do this in a seperate file and just run through every line regardless.
    Why? and it's no problem taking so long, I just needed to be able to work on it today.

  • #10
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well it just become more efficient if you are going to do multiple updates. rather than stopping and doing just that one row update, which could lead to some complicated code later, you just cascade through the lot. You could not do this if the variables are part of some other code. A line break or a code change would mess up your file when you cascaded through the array.

    So a seperate file would be a database that could be manipulated without doing harm or giving consideration to other code. I usually do my configuration files like this. I have one update script that can be used on multiple configuartion files.
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials


  •  

    Posting Permissions

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