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.
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.
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.
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);