fopen question

by 4 replies
5
Hi

How do I post the data created with this code:

<?php

require('config.php');
require_once('include/functions.php');

$result = mysql_query("SELECT ProductID, ProductName FROM ura_items WHERE CategoryID != 0 AND CategoryID != 1 AND CategoryID != 2 ORDER BY Add_Date DESC")
or die(mysql_error());

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {

echo "".seo_links('detail', $row['ProductID'], $row['ProductName'], 1, _SEO)."<br>";

}

?>

into another text file?

I can get a single line to print to another file using the code below but I can't get the code above to run and print out a long list.

Code:
<?php
$file= fopen("sitemap.txt", "w");
$data = "testing";
fwrite($file, $data);
fclose($file);
print "data posted";
?>
#programming #fopen #question
  • The issue is that you are using fopen in 'write' mode. Try using it in 'append' mode if you want it to add on to the bottom:
    PHP Tutorial - File Append
    PHP: fopen - Manual
    Code:
    = fopen("sitemap.txt", "a");
  • The main issue is that you're echoing the results of the query instead of capturing it in an array or variable.

    Instead of what you have, you'd use something like this:

    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {

    $data .= " " . seo_links('detail', $row['ProductID'], $row['ProductName'], 1, _SEO) . "<br>\r\n";

    }

    Exact usage would depend on what, exactly, you're trying to write to the file.
    • [1] reply
    • Thanks for the above. I have it working with this code setup:

      <?php

      require('config.php');
      require_once('include/functions.php');

      $result = mysql_query("SELECT ProductID, ProductName FROM ura_items WHERE CategoryID != 0 AND CategoryID != 1 AND CategoryID != 2 ORDER BY Add_Date DESC")
      or die(mysql_error());

      // keeps getting the next row until there are no more to get
      while($row = mysql_fetch_array( $result )) {

      $data .= " " . seo_links('detail', $row['ProductID'], $row['ProductName'], 1, _SEO) . "\r\n";

      } ;

      $file= fopen("sitemap.txt", "w");
      fwrite($file, $data);
      fclose($file);
      print "sitemap text written";
      ?>

      Works really well too and prints out a sitemap text file with over 30,000 urls in seconds . Google webmaster tools accepts it too.
      • [1] reply

Next Topics on Trending Feed