by Mkj
4 replies
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";
?>
#fopen #question
  • Profile picture of the author ussher
    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");
    Signature

    "Jamroom is a Profile Centric CMS system suitable as a development framework for building entire communities. Highly modular in concept. Suitable for enterprise level development teams or solo freelancers."

    - jamroom.net
    Download Jamroom free: Download
    {{ DiscussionBoard.errors[3766216].message }}
  • Profile picture of the author SteveJohnson
    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.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[3766792].message }}
    • Profile picture of the author Mkj
      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.
      {{ DiscussionBoard.errors[3767449].message }}
      • Profile picture of the author lordspace
        I'd use something like this.

        Code:
         = fopen("sitemap.txt", "w");
        
        /* Activate the LOCK_NB option on an LOCK_EX operation */
        if ( && flock(, LOCK_EX | LOCK_NB)) {
            fwrite(, );
            fclose();
            print "sitemap text written";
        } else {
            print "sitemap text was NOT written";
        }
        for some reason the forum deletes $file variable !?!
        Signature

        Are you using WordPress? Have you tried qSandbox yet?

        {{ DiscussionBoard.errors[3794478].message }}

Trending Topics