how to append different text

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

how to append different text

Post by slimt_slimt »

this is the section of the code, which should add to a htm file piece of <html> code, based on the fact, if it's the first line or the second line, third line, etc.

Code: Select all

$fp = fopen('names.txt','r');

$id = "ID=\"first-XML-item\"";

 $nf = fopen("c://apache//htdocs//names//start.htm", "a");
   while (!feof($fp)) 
  {
		$line = fgets($fp, 1024); 
		list ($name, $namefile) = split ('\;', $line);
		echo '
		 <table>
		  <tr>
			<td>'.$name.'</td>
			<td>'.$namefile.'</td>
		  </tr>
		</table>';
		if ($line[0] == true) 
		{
		$content2=
		"
		<DIV CLASS=\"button\" ".$id."
           onMouseOver=\"over(this)\"
           onMouseOut=\"out(this)\"
           onClick='changeXML(\"".$namefile."\"); select(\"xml\",this)'>
		  ".$name."<SPAN CLASS=\"arrow\">4</SPAN>
      </DIV>";
	  } 
	  else
	  {
	    $content2=
		"
		<DIV CLASS=\"button\"
           onMouseOver=\"over(this)\"
           onMouseOut=\"out(this)\"
           onClick='changeXML(\"".$namefile."\"); select(\"xml\",this)'>
		  ".$name."<SPAN CLASS=\"arrow\">4</SPAN>
      </DIV>";
	  }
		$fp++;
	fwrite ($nf, $content2);	
  }
  fclose($nf);
  fclose($fp);
and the names.txt looks like this

Code: Select all

name1;name1.xml
name2;name2.xml
name3;name3.xml
the html outcome should end up looking like this:

Code: Select all

	<DIV CLASS="button" ID="first-XML-item"
           onMouseOver="over(this)"
           onMouseOut="out(this)"
           onClick='changeXML("name1.xml"); select("xml",this)'>
		  name1<SPAN CLASS="arrow">4</SPAN>
      </DIV>
	<DIV CLASS="button" 
           onMouseOver="over(this)"
           onMouseOut="out(this)"
           onClick='changeXML("name2.xml"); select("xml",this)'>
		  name2<SPAN CLASS="arrow">4</SPAN>
      </DIV>
	
<DIV CLASS="button" 
           onMouseOver="over(this)"
           onMouseOut="out(this)"
           onClick='changeXML("name3.xml"); select("xml",this)'>
		  name3<SPAN CLASS="arrow">4</SPAN>
      </DIV>

but the html outcome does not come like this.
either it comes with ID="first-XML-item" on all the items or it does not come with ID="..." whatsoever (ID is bolded).

what should I change in order to get the desired html outcome?

:-)
Last edited by slimt_slimt on Wed Nov 08, 2006 2:26 am, edited 2 times in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

And what is the problem?
User avatar
slimt_slimt
Forum Newbie
Posts: 11
Joined: Wed Oct 25, 2006 5:16 am

Post by slimt_slimt »

volka: you're too fast :) I was still writing the post :)

now everything is there :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

$fp = fopen('names.txt','r');
$nf = fopen("c:/apache/htdocs/names/start.htm", "a");

$id = 'ID="first-XML-item"';
while (!feof($fp))
{
	$line = trim(fgets($fp, 1024));
	list ($name, $namefile) = split (';', $line);
		echo '
			<table>
				<tr>
					<td>'.$name.'</td>
					<td>'.$namefile.'</td>
			</tr>
			</table>';
	
	$content='
		<DIV CLASS="button" '.$id.'
			onMouseOver="over(this)"
			onMouseOut="out(this)"
			onClick=\'changeXML("' . $namefile . '"); select("xml",this)\' >
				' . $name . '<SPAN CLASS="arrow">4</SPAN>
		</DIV>';
		$id = '';
	fwrite ($nf, $content);
}
fclose($nf);
fclose($fp);
but since you're appending data to start.htm each run of the script will add a new element with ID="first-XML-item". Then the ID isn't unique anymore and your document is invalid.
User avatar
slimt_slimt
Forum Newbie
Posts: 11
Joined: Wed Oct 25, 2006 5:16 am

Post by slimt_slimt »

volka: tnx. it works
Post Reply