943,860 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1381
  • PHP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 2nd, 2008
0

Re: PHP and XML Question

You know how I am writting this file to my server every day

PHP Syntax (Toggle Plain Text)
  1. #
  2. $file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd

is there any way to delete an old file when the new one is uploaded every day

???

Thank you
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
jencinas69 is offline Offline
68 posts
since Nov 2007
Oct 2nd, 2008
0

Re: PHP and XML Question

Hold on, something still isn't right. I am still working on this.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 2nd, 2008
0

Re: PHP and XML Question

OK that's better, had to add another dimension to the array to loop through the rows, you probably noticed that. Try this.
php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3.  
  4. error_reporting(E_ALL ^ E_NOTICE);
  5. ini_set("display_errors", true);
  6.  
  7. ini_set('memory_limit', '24M');
  8.  
  9.  
  10.  
  11. function csv2xml($file, $container = 'data')
  12. {
  13. $dataarray = array();
  14. $r = "<{$container}>\n";
  15.  
  16. $row = 0;
  17. $cols = 0;
  18. $titles = array();
  19.  
  20. $handle = @fopen($file, 'r');
  21. if (!$handle) return $handle;
  22.  
  23. while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
  24. {
  25.  
  26. if (!$cols) $cols = count($data);
  27. for ($i = 0; $i < $cols; $i++)
  28. {
  29. if ($row == 0)
  30. {
  31. $titles[$i] = $data[$i];
  32. continue;
  33. }
  34.  
  35. $emailsarray = array();
  36. if(strtolower($titles[$i]) == "description")
  37. {
  38. foreach(explode(" ", $data[$i]) as $value)
  39. {
  40. if(eregi("^[a-zA-Z0-9/\._\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9\._\-]+$", $value))
  41. {
  42. $emailsarray[] = $value;
  43. }
  44. }
  45. }
  46.  
  47. $dataarray[$row][$titles[$i]][] = trim($data[$i]) != ""?$data[$i]:"";
  48.  
  49. foreach($emailsarray as $value)
  50. {
  51. $dataarray[$row]["email"][] = $value;
  52. }
  53. }
  54.  
  55. $row++;
  56. }
  57. fclose($handle);
  58.  
  59. $containers = array("email");
  60.  
  61. for($i = 1; $i <= count($dataarray); $i++)
  62. {
  63. foreach($dataarray[$i] as $key=>$value)
  64. {
  65. if(in_array(strtolower($key), $containers))
  66. {
  67. $r .= "\t\t<" . $key . "-container>\n";
  68. }
  69.  
  70. foreach($value as $value2)
  71. {
  72. $tabchars = "\t\t";
  73. if(in_array(strtolower($key), $containers))
  74. {
  75. $tabchars .= "\t\t";
  76. }
  77. $r .= $tabchars . "<$key>$value2</$key>\n";
  78. }
  79.  
  80. if(in_array(strtolower($key), $containers))
  81. {
  82. $r .= "\t\t</" . $key . "-container>\n";
  83. }
  84. }
  85. }
  86.  
  87. $r .= "</{$container}>\n";
  88. return $r;
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
  99. $xml .= csv2xml('dtifeed.csv', 'petad');
  100.  
  101.  
  102. $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');
  103.  
  104. fwrite($xmlfile, $xml) or die('Could not write string to XML file');
  105.  
  106. fclose($xmlfile);
  107.  
  108. echo "Successfully wrote the XML file";
  109.  
  110. ?>
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 2nd, 2008
0

Re: PHP and XML Question

Click to Expand / Collapse  Quote originally posted by jencinas69 ...
You know how I am writting this file to my server every day

PHP Syntax (Toggle Plain Text)
  1. #
  2. $file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd

is there any way to delete an old file when the new one is uploaded every day

???

Thank you
You would want to use the unlink() function to delete a file.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 10th, 2008
0

Re: PHP and XML Question

Hey Rob at the end I did not need the email thing this is the final copy of code

PHP Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3.  
  4.  
  5. error_reporting(E_ALL ^ E_NOTICE);
  6. ini_set("display_errors", true);
  7.  
  8. ini_set('memory_limit', '24M');
  9.  
  10.  
  11. $file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd
  12.  
  13. if (!file_exists($file)) touch($file);
  14.  
  15. $fh = fopen($file, "r");
  16.  
  17. function csv2xml($file, $container = 'data')
  18. {
  19. $r = "<{$container}>\n";
  20. $row = 0;
  21. $cols = 0;
  22. $titles = array();
  23.  
  24. $handle = @fopen($file, 'r');
  25. if (!$handle) return $handle;
  26.  
  27. while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
  28. {
  29.  
  30. if (!$cols) $cols = count($data);
  31. for ($i = 0; $i < $cols; $i++)
  32. {
  33. if ($row == 0)
  34. {
  35. $titles[$i] = $data[$i];
  36. continue;
  37. }
  38.  
  39. $r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":"";
  40. }
  41.  
  42. $row++;
  43. }
  44. fclose($handle);
  45. $r .= "</{$container}>";
  46.  
  47. return $r;
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. $xml = '<?xml version="1.0" encoding="ISO-8859-1" ?> ';
  58. $xml .= csv2xml('petsfeed.csv', 'petad');
  59.  
  60.  
  61.  
  62. $xmlfile = @fopen($file, "wb") or die('Could not open XML file for writing');
  63.  
  64. fwrite($xmlfile, $xml) or die('Could not write string to XML file');
  65.  
  66. fclose($xmlfile);
  67.  
  68. echo "Successfully wrote the XML file";
  69.  
  70. ?>
  71.  
  72. The only problem is that I need the <petad> to open and close every ad instead of opening at the beginning of the file and closing at the end
  73.  
  74. any ideas?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
jencinas69 is offline Offline
68 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Symfony MOdule redirection
Next Thread in PHP Forum Timeline: Multiple SQL Queries in PHP page





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC