0

I'd like to write the content of the variable $rows into the file out.txt.

The variable $rows is placed within a while loop and can be constituted by several different rows retrieved from a database.

Unfortunately, the following code just writes one single line (the last one precisely) of the expected output.

$myFile = "/var/www/test/out.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$content = $rows['text'];
fwrite($fh, $content);
fclose($fh);

Here is the PHP code (which is working) that allows me to see the contents of the $rows variable in the browser.

<?
if(isset($_POST['type'], $_POST['topic'], $_POST['tense'], $_POST['polarity']))
{
     $tipo = $_POST['type'];
     $argomento = $_POST['topic'];
     $tempo = $_POST['tense'];
     $segno = $_POST['polarity'];
   foreach ($tipo as $key_tipo => $value_tipo)
   {
         foreach ($argomento as $key_argomento => $value_argomento)
         {
                 foreach ($tempo as $key_tempo => $value_tempo)
                 {
                         foreach ($segno as $key_segno => $value_segno)
                         {
       $q_tip_arg_tem_seg = "SELECT * FROM entries WHERE type = '".$value_tipo."' AND topic = '".$value_argomento."' AND tense = '".$value_tempo."' AND polarity = '".$value_segno."'";  
       $qu_tip_arg_tem_seg = mysql_query($q_tip_arg_tem_seg);
       while ($rows = mysql_fetch_array($qu_tip_arg_tem_seg))
        {
            echo $rows['type']."<br />";
        }
   }
   }
   }
   }
}
else
{
   echo "You forgot to check something."."<br>"."Please select at least one type, topic, tense and polarity value."."<br>";
}
?>

Can you help me out? Thanks.

5
  • Please show the entire code. I see no while loop. Commented Jul 24, 2013 at 9:10
  • You shouldn't put fopen(), fwrite() and fclose() in your loop. And shouldn't you use $row['text'] instead of $rows['text'] ? (We don't have the whole code, but...) Commented Jul 24, 2013 at 9:15
  • Why u don't use file_put_contents() ? Commented Jul 24, 2013 at 9:20
  • @Ale Has anything here helped? Commented Jul 24, 2013 at 11:38
  • @RiggsFolly Actually, I found out that the output of the variable $rows is really made up of a single line. The only reason why in the browser I see all the lines I expect is that the while loop overwrites the variable. As a result, I still haven't figured out how to write onto my txt file what I see in the browser. Commented Jul 24, 2013 at 14:25

3 Answers 3

2

You only write out one line to the file. You probably want something like this:

$myFile = "/var/www/test/out.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
if ($fh) {
    while ($row = getrow()) {
        $content = $rows['text'];
        fwrite($fh, $content); // perhaps add a newline?
    }
    fclose($fh);
}

So, open the file, write all your stuff in it, then close it.

Sign up to request clarification or add additional context in comments.

5 Comments

Wouldn't it be better (on performance) to call fwrite() once after the loop instead of possibly hundreds of times inside it?
No. First, I wouldn't do performance tricks if I have no performance troubles. Second, let the OS handle that (it does).
You wouldn't do performance tricks if you have no performance troubles? How do you know you won't? Sometimes it's safe to assume, but you should always consider performance. Even for the most simplest of things.
On this point I agree with @BartFriederichs. You don't have to care about performance tricks if the best it can do is saving 1ms at the runtime, and losing 30 minutes when debugging.
@Jared, because many assumptions on performance are plain wrong. Also in your case. The OS knows very well how to handle files, it doesn't need your help. Besides, your "solution" would introduce extra memory usage. When programming, write maintainable code and try to think about the order of complexity (big-O) in terms of efficiency. Then, when your program seems to run slow or bloated, find the cause and fix it. Do not optimize prematurely!
0

you can do it in two ways

1.get the details from out.txt file first and then concat it with your $row .now you have the content already present in out.txt and also $row text.finaly write the file with content.

2.collect all rows in a single variable by concating it into a varialble.and do the file write operation.

Comments

0

If you just want to see what the array looks like, try just using print_r() it prints an array in a nice to read format.

If you want a more structured layout this will probably not be enough.

$myFile = "/var/www/test/out.txt";

$fh = fopen($myFile, 'w') or die("can't open file");

fwrite($fh, print_r($rows,TRUE) );

fclose($fh);

7 Comments

According to the PHP manual print_r() will return true and will attempt to output to the page, so you wouldn't be able to write to the file with it. php.net/manual/en/function.print-r.php
Jared: pls. have a look at the manual that you linked to . print_r certainy accepts a 2nd boolean argument which you can set to true to capture its output as result!
@Jared passing a second argument as true in print_r will return the output, in var_export also
@MBaas I use this all the time, param 2 set to true makes any result of print_r be delivered as its returned data and stops it being sent directly to the page.
@RiggsFolly: sure, so do I - but it seemed that Jared did not know that!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.