Results 1 to 10 of 10
  1. #1

    Quick PHP question - fopen() fread() fwrite()

    Ok, I have a form set up with the code listed below. What I'm trying to do is when the user submits the form the following happens:

    1) The file with the data is opened. This will contain 2 numbers. One holding the number of votes and one with the vote total (explained in more detail later).
    2) the two numbers are stored to two variables, $voteTotal and $numVotes or something like that.
    3) The new vote is added to $voteTotal (its a rating script so this will be a number between 1 and 10) and $numVotes is increased by one.
    4) Finally, I want to the file saved and closed. The data still stored in $numVotes and $voteTotal will be used to display an avg rating (avg = $voteTotal/$numVotes).

    As you can see I have this planned out I just can't compeltely figure out how the fopen() fread() and fwrite() functions work. Any help would be appreciated. Here is the code for the form:

    PHP Code:
        <form action="process.php" method="get">
        <
    select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
      <
    option>Rate It!</option>
      <
    option>----------</option>
      <
    option value="process.php?rating=1">1</option>
      <
    option value="process.php?rating=2">2</option>
      <
    option value="process.php?rating=3">3</option>
      <
    option value="process.php?rating=4">4</option>
      <
    option value="process.php?rating=5">5</option>
      <
    option value="process.php?rating=6">6</option>
      <
    option value="process.php?rating=7">7</option>
      <
    option value="process.php?rating=8">8</option>
      <
    option value="process.php?rating=9">9</option>
      <
    option value="process.php?rating=10">10</option>
    </
    select>

        </
    form
    Thanks

  2. #2
    Just an FYI. Yes I've read the manual... I read it before posting any quesitons. My problem lies within reading the file properly. The file will be set up in one of two ways.

    1. Number of votes, and then vote total on a new line. Ex:
    34
    212

    2. Number of votes and then vote total seperated by a comma. Ex:
    34, 212

    My problem is being able to parse the file once open to get these numbers in the correct variables and then being able to rewrite them in the correct manner.

  3. #3
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    These snippets should get you going :

    PHP Code:
    <?php

        
    //For your first format
        
    $contents file("nameoffile.txt");
        
    $numVotes $contents[0];
        
    $totalVotes $contents[1];

         
    //For your second format
         
    $contents file("nameoffile.txt");
         
    $bits explode(",",$contents[0]);
         
    $numVotes $bits[0];
         
    $totalVotes $bits[1];
        
    ?>
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  4. #4
    Well here is what I have now and it works as far as I can tell right to the header() function. Then I get a header error (Headers already sent). Last time this was because I had a space at the top of the page before the PHP code started, but this time there is no space, nor is there a space on the index page where this script is embedded. Any help with the code to get this erro dealt with would be much appreciated.

    PHP Code:
    <?php if (!isset($a)) { ?>

    <script language="JavaScript">
    <!--
    function MM_jumpMenu(targ,selObj,restore){ //v3.0
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
    }
    //-->
    </script>

    <table align="center" width="150" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>
        <b>Movie:</b> Troy<br>
        <b>Stars:</b>  B. Pitt, O. Bloom<br>
        <b>Director:</b> W. Peterson
        </td>
      </tr>
      <tr>
        <td align="center">
        <form action="entertainment/rating.php" method="get">
        <select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
      <option>Rate It!</option>
      <option>----------</option>
      <option value="entertainment/rating.php?a=1&rating=1">1</option>
      <option value="entertainment/rating.php?a=1&rating=2">2</option>
      <option value="entertainment/rating.php?a=1&rating=3">3</option>
      <option value="entertainment/rating.php?a=1&rating=4">4</option>
      <option value="entertainment/rating.php?a=1&rating=5">5</option>
      <option value="entertainment/rating.php?a=1&rating=6">6</option>
      <option value="entertainment/rating.php?a=1&rating=7">7</option>
      <option value="entertainment/rating.php?a=1&rating=8">8</option>
      <option value="entertainment/rating.php?a=1&rating=9">9</option>
      <option value="entertainment/rating.php?a=1&rating=10">10</option>
    </select>

        </form>
        </td>
      </tr>
    </table>

    <?php }
        elseif (
    $a 1) { //if the action is set to process
            //open the txt file and get the data
            
    $handle fopen("http://www.foymedia.com/absolute/website/student/website/entertainment/results.txt","r");
            
    $numVotes trim(fgets($handle,80));
            
    $voteTot trim(fgets($handle,80));
            
    fclose($handle);
            
            
    //process the data
            
    $numVotes $numVotes 1;
            
    $voteTot $voteTot $rating;
            
            
    //save the file
            
    $handle fopen("http://www.foymedia.com/absolute/website/student/website/entertainment/results.txt","w");
            
    fwrite($handle,$numvotes);
            
    fwrite($handle,$totvotes);
            
    fclose($handle);
            
            
    //reload the index page with the command to display results
            
    header("Location: [url]http://[/url]".$_SERVER['HTTP_HOST'] . "/absolute/website/student/website/index.php?a=2");
            }
            
        else { 
    //if the action is set to display results
            //open the txt file and get the data
            
    $handle fopen("http://www.foymedia.com/absolute/website/student/website/entertainment/results.txt","r");
            
    $numVotes trim(fgets($handle,80));
            
    $voteTot trim(fgets($handle,80));
            
    $result $votTot $numVotes;
            
    fclose($handle);
            
    //display the data ?>
            
    <script language="JavaScript">
    <!--
    function MM_jumpMenu(targ,selObj,restore){ //v3.0
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
    }
    //-->
    </script>        

    <table align="center" width="150" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>
    <font size="+1">Troy</font>
        </td>
      </tr>
      <tr>
        <td align="center">
    <b>Avg. Rating: <font size="+1"><?php echo $result ?></font></b>
        </td>
      </tr>
    </table>
    <?php ?>

  5. #5
    It seems the header function got parsed funny on the forums. Let me try just that line again:

    header("Location: http://".$_SERVER['HTTP_HOST'] . "/absolute/website/student/website/index.php?a=2");
    }

  6. #6
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    The problem is, you can't set any headers once content has been output to the browser.

    Instead of doing header redirects, consider using javascript. So inplace of your header call, you would echo some script, like so :

    PHP Code:
    echo '<script type="text/javascript">';
    echo 
    'document.location.href = ';
    echo 
    "http://".$_SERVER['HTTP_HOST'] . "/absolute/website/student/website/index.php?a=2";
    echo 
    "'</script>"
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  7. #7
    Ah thats a clever strategy. I tried it and I must be missing something, as its not redirectiing to the page. The URL showing in the browser is:

    http://www.foymedia.com/absolute/web...p?a=1&rating=6

    and when you view the source its:

    <script type="text/javascript">document.location.href = http://www.foymedia.com/absolute/web.../index.php?a=2'</script>

    which is exactly what we wanted printed... Is there something I'm missing as to why the server isn't running this code, but rather just printing it?

  8. #8
    I wrote a mini-forum based on reading and writing a flat(text)file.
    it uses fwrite and fread a whole lot.

    maybe it will help...

    http://www.neohz.com/flatforum.zip

  9. #9
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    Ah there is a missing ' that's why the javascript isn't executing.

    Replace

    echo 'document.location.href = ';

    with

    echo 'document.location.href = \'';

    and it should work
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  10. #10
    Yeah we're getting closer... it seems to be getting stuck in a loop or something when the display results part of the script runs. You can try going to the site again: www.foymedia.com/absolute/website (click on student, then vote using the rating script).

    Any idea as to why its getting stuck on this last part of the script? Is there a line of code I put in to error check this?

Posting Permissions

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