using two flat-files to generate new files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
slimt_slimt
Forum Newbie
Posts: 11
Joined: Wed Oct 25, 2006 5:16 am

using two flat-files to generate new files

Post by slimt_slimt »

Hi, this is the following code, that doesn't work the way it should.

Code: Select all

<?php

$fp = fopen('structure_names.txt','r');
 if (!$fp) {echo 'Error; can not open file.'; exit;}

   while (!feof($fp)) 
  {
		$line = fgets($fp, 512); 
		list ($name, $tab, $desc) = split ('\;', $line);
		
		echo '
		 <table>
		  <tr>
			<td>'.$tab.'.</td>
			<td>'.$name.'</td>
		    <td>'.$desc.'</td> 
		  </tr>
		</table>';
		/*
	   	$content_intro=
	  " 
	  <?xml version=\"1.0\"?>
<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">
<xsl:template match=\"/\">
<TABLE STYLE=\"border:1px solid black\"> 
       <tr><b>".$name."</b></tr>";
	    
		$fp = fopen("c://apache//htdocs//xsl//".$ime.".xsl", "w");
		fwrite ($fp, $content_intro);
	   // fclose($fp);
	*/  
	  $fp2 = fopen('files_name.txt','r');
       if (!$fp2) {echo 'Error; can not open the file'; exit;}	 
 	
		while (!feof($fp2))
		 {
		  $line2 = fgets($fp2, 512);
		  list ($namefile, $descfile) = split ('\;',$line2);
		  
		echo '
		 <table>
		  <tr>
			 <td>'.$descfile.'</td>
			 <td>'.$namefile.'</td>
		  </tr>
		</table>';
	
	  $content_head=
	  "<TR>
        <TD STYLE=\"font-size:11pt; font-family:Verdana;\" bgcolor=\"gray\">".$namefile."</TD>
        <TD width=\"1200\" nowrap=\"nowrap\" bgcolor=\"#999999\"><xsl:copy-of select=\"document('".$descfile."')//focus1/".$name."/.\"/></TD>
       </TR>";
		// $fp = fopen("c://apache//htdocs//xsl//".$name.".xsl", "a");
		fwrite ($fp, $content_head); 
		*/
		$fp2++;
		 }
		$fp++;
		fclose($fp2);	
  }
 fclose($fp); 

?>
where as the structure_names.txt looks like

Code: Select all

name1;1;description1
name2;2;description2
and files_names.txt looks like

Code: Select all

xml1.xml;namefile1
xml2.xml;namefile2
xml3.xml;namefile3
It should read from structure_names.txt and generate files with names name1.xsl, name2.xsl, etc.

with the content of each files name1.xsl, name2.xsl, name3.xsl, etc... of files_names.txt.

If anyone can find the problem why both while loops aren't producing the results, let me know!

thank you!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

slimt_slimt wrote: // $fp = fopen("c://apache//htdocs//xsl//".$name.".xsl", "a");
fwrite ($fp, $content_head);
*/
$fp2++;
}
$fp++;
fclose($fp2);
This part is weird.
And I don't understand your description of what you're trying to achieve.
User avatar
slimt_slimt
Forum Newbie
Posts: 11
Joined: Wed Oct 25, 2006 5:16 am

Post by slimt_slimt »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


volka: mainly what this code does is take information from 2 different flat files and make new txt or xml files based on information extracted from both files.

The code looks a bit like spaghetti  and that's why i got confused with opening and closing the files.
Went thoroughly through and now it works just fine.  So what it does is:

based upon user inquiry, it generates appropriate XSL file to navigate through the XML files.
this part is just a piece of the code.

Code: Select all

<?php

$fp = fopen("structure_name.txt","r");
// if (!$fp) {echo "Error, can't open file."; exit;}

   while (!feof($fp)) 
  {
		$line = fgets($fp, 512); 
		list ($name, $toc, $desc) = split ('\;', $line);
		
		echo '
		 <table>
		  <tr>
			<td>'.$toc.'.</td>
			<td>'.$name.'</td>
		    <td>'.$desc.'</td> 
		  </tr>
		</table>';
		
	   	$content_intro="<?xml version=\"1.0\"?>
<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">
<xsl:template match=\"/\">
<TABLE STYLE=\"border:1px solid black\"> 
       <tr><b>".$name."</b></tr>";

		$fp2 = fopen("c://apache//htdocs//xsl//".$name.".xsl", "w");
		fwrite ($fp2, $content_intro);
        fclose($fp2); 
		
	   $fp3 = fopen("files_names.txt","r");
      // if (!$fp3) {echo "Error, can't open the file."; exit;}	 

		while (!feof($fp3))
		  {
		  $line2 = fgets($fp3, 512);
		  list ($namefile, $contentfile) = split ('\;',$line2);
		  
		echo '
		 <table>
		  <tr>
			 <td>'.$contentfile.'</td>
			 <td>'.$namefile.'</td>
		  </tr>
		</table>';

	  $content_head=
	  "<TR>
        <TD STYLE=\"font-size:11pt; font-family:Verdana;\" bgcolor=\"gray\">".$namefile."</TD>
        <TD width=\"1200\" nowrap=\"nowrap\" bgcolor=\"#999999\"><xsl:copy-of select=\"document('".$contentfile."')//focus1/".$name."/.\"/></TD>
       </TR>";
	    $fp4 = fopen("c://apache//htdocs//xsl//".$name.".xsl", "a+");
		fwrite ($fp4, $content_head); 
		fclose($fp4);
		$fp2++;
		
		  } //end while FP3
	    fclose($fp3);	
	$content_foot=
"
   </TABLE>
  </xsl:template>
</xsl:stylesheet>
";
	 $fp5= fopen("c://apache//htdocs//xsl//".$name.".xsl", "a+");
	 fwrite ($fp5, $content_foot);
	 fclose($fp5);
		$fp++;
		// fclose($fp2);
		
		
  } // end while FP2
 fclose($fp); 

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

I'd like to help you but you are not using the php bbcode (php syntax highlighter), but well, it seems you figured it out.

The most confusing and weird thing I've ever seen is that $fp2++ thing you have... I mean, what that all about?
Is that even right?
Post Reply