Problem Writing to txt File

Discussion in 'PHP' started by adamjblakey, Nov 28, 2008.

  1. #1
    Hi,

    I am trying the following but it is not writing to the file and only writing one line even though there is 20 rows.

    
    //Get Results
    $query = mysql_query("SELECT * FROM planner");
    
    while ($row = mysql_fetch_array($query)) {
    	$yourdata = get_date($row['date']).";".$row['hours'].";".$row['company']."\n";
    }
    
    //Write to the file
    $fp = fopen("test.txt", "w");
    fwrite($fp, $yourdata);
    fclose($fp); 
    
    PHP:
    Any ideas?
    Cheers,
    Adam
     
    adamjblakey, Nov 28, 2008 IP
  2. peeeev

    peeeev Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Change the line
    
    $yourdata = get_date($row['date']).";".$row['hours'].";".$row['company']."\n";
    
    PHP:
    to

    
    $yourdata .= get_date($row['date']).";".$row['hours'].";".$row['company']."\n";
    
    PHP:
     
    peeeev, Nov 28, 2008 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Great that works fine thanks, on another note i want to add up all the hours for every row so it gives me a total hours. Could i do this in the code also or not?
     
    adamjblakey, Nov 28, 2008 IP
  4. peeeev

    peeeev Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If I understand you right then this is the code:
    
    //Get Results
    $query = mysql_query("SELECT * FROM planner");
    $yourdata = '';
    $total_hours = 0;
    
    while ($row = mysql_fetch_array($query)) {
        $yourdata .= get_date($row['date']).";".$row['hours'].";".$row['company']."\n";
        $total_hours += $row['hours'];
    }
    
    //Write to the file
    $fp = fopen("test.txt", "w");
    fwrite($fp, $yourdata);
    fwrite($fp, 'Total Hours: '.$total_hours);
    fclose($fp);
    
    PHP:
     
    peeeev, Nov 28, 2008 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Great it works fine, thank you very much :)
     
    adamjblakey, Nov 28, 2008 IP