PHP and XML Question

Reply

Join Date: Nov 2007
Posts: 68
Reputation: jencinas69 is an unknown quantity at this point 
Solved Threads: 1
jencinas69's Avatar
jencinas69 jencinas69 is offline Offline
Junior Poster in Training

Re: PHP and XML Question

 
0
  #11
Oct 2nd, 2008
You know how I am writting this file to my server every day

  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: PHP and XML Question

 
0
  #12
Oct 2nd, 2008
Hold on, something still isn't right. I am still working on this.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: PHP and XML Question

 
0
  #13
Oct 2nd, 2008
OK that's better, had to add another dimension to the array to loop through the rows, you probably noticed that. Try this.
  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. ?>
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: PHP and XML Question

 
0
  #14
Oct 2nd, 2008
Originally Posted by jencinas69 View Post
You know how I am writting this file to my server every day

  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.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 68
Reputation: jencinas69 is an unknown quantity at this point 
Solved Threads: 1
jencinas69's Avatar
jencinas69 jencinas69 is offline Offline
Junior Poster in Training

Re: PHP and XML Question

 
0
  #15
Oct 10th, 2008
Hey Rob at the end I did not need the email thing this is the final copy of code

  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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC