Hi i have a while loop which displaying data from the database but i am trying to style but it doesnt seem to be workin help please.


xml script

<?php
// PHP file that renders perfect Dynamic XML for MySQL Database result sets
// Script written by Adam Khoury @ http://www.developphp.com - April 05, 2010
// View the video that is tied to this script for maximum understanding
// -------------------------------------------------------------------
header&#40;"Content-Type: text/xml"&#41;; //set the content type to xml
// Initialize the xmlOutput variable
$xmlBody = &#39;<?xml version="1.0" encoding="ISO-8859-1"?>&#39;;
$xmlBody .= "<XML>";
// Connect to your MySQL database whatever way you like to here
mysql_connect&#40;"localhost","root",""&#41; or die &#40;mysql_error&#40;&#41;&#41;;
mysql_select_db&#40;"admin"&#41; or die &#40;"no database"&#41;;
// Execute the Query on the database to select items&#40;20 in this example&#41;
$sql = mysql_query&#40;"SELECT * FROM news ORDER BY news_date DESC LIMIT 0, 20"&#41;;
while&#40;$row = mysql_fetch_array&#40;$sql&#41;&#41;{
    // Set DB variables into local variables for easier use
    $news_id = $row["news_id"];
    $subject = $row["subject"];  
    $news_date = strftime&#40;"%b %d, %Y", strtotime&#40;$row["news_date"]&#41;&#41;;
    $news_artical = $row["news_artical"];  
    // Start filling the $xmlBody variable with looping content here inside the while loop
    // It will loop through 20 items from the database and render into XML format
       
    $xmlBody .= &#39;
        <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
        <cd>

    <title>&#39; . $news_id . &#39;</title>
    <artist>&#39; . $subject . &#39;</artist>
   <country>&#39; . $news_date . &#39;</country>
   <company>&#39; . $description . &#39;</company>
   
        </cd>

</catalog>&#39;;
} // End while loop
mysql_close&#40;&#41;; // close the mysql database connection
$xmlBody .= "</XML>";
echo $xmlBody; // output the gallery data as XML file for flash
?>

xml style sheet

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method=&#39;html&#39; version=&#39;1.0&#39; encoding=&#39;UTF-8&#39; indent=&#39;yes&#39;/>

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">

        <th align="left">Title</th>
        <th align="left">Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>

      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?> should appear only ONCE in the document. Currently you are appending that within your WHILE construct, so you are generating that multiple times. Move it OUTSIDE you while construct so that it is BEFORE the root/top-most element (basically between lines 8 and 9 in your markup).

at first never use tagname with name xml
is not so good xml tagname are allow for PI nodes

your xml looks like this

<?xml version="1.0"?>
<xml>
	<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
	<catalog>
		<cd>
			<title>table</title>
			<artist>Fred</artist>
			<country>ToonDown</country>
			<company>USA</company>
		</cd>
	</catalog>
	<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
	<catalog>
		<cd>
			<title>table</title>
			<artist>Barny</artist>
			<country>ToonDown</country>
			<company>USA</company>
		</cd>
	</catalog>
</xml>

but you will

<?xml version="1.0"?>
<xml>
	<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
	<catalog>
		<cd>
			<title>Hello again</title>
			<artist>Fred</artist>
			<country>ToonDown</country>
			<company>USA</company>
		</cd>
	</catalog>
	<catalog>
		<cd>
			<title>Jabbja</title>
			<artist>Barny</artist>
			<country>ToonDown</country>
			<company>USA</company>
		</cd>
	</catalog>
</xml>

better

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
	<cd>
		<title>Hello again</title>
		<artist>Fred</artist>
		<country>ToonDown</country>
		<company>USA</company>
	</cd>
	<cd>
		<title>Jabbja</title>
		<artist>Barny</artist>
		<country>ToonDown</country>
		<company>USA</company>
	</cd>
</catalog>

you must php new create

<?php
// PHP file that renders perfect Dynamic XML for MySQL Database result sets
// Script written by Adam Khoury @ http://www.developphp.com - April 05, 2010
// View the video that is tied to this script for maximum understanding
// -------------------------------------------------------------------
header("Content-Type: text/xml"); //set the content type to xml
// Initialize the xmlOutput variable
$xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$xmlBody .= "<catalog>";
$xmlBody .= "<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>";
$xmlBody .= "
// Connect to your MySQL database whatever way you like to here
mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("admin") or die ("no database");
// Execute the Query on the database to select items(20 in this example)
$sql = mysql_query("SELECT * FROM news ORDER BY news_date DESC LIMIT 0, 20");
while($row = mysql_fetch_array($sql)){
    // Set DB variables into local variables for easier use
    $news_id = $row["news_id"];
    $subject = $row["subject"];
    $news_date = strftime("%b %d, %Y", strtotime($row["news_date"]));
    $news_artical = $row["news_artical"];
    // Start filling the $xmlBody variable with looping content here inside the while loop
    // It will loop through 20 items from the database and render into XML format

    $xmlBody .= '
    <cd>

    <title>' . $news_id . '</title>
    <artist>' . $subject . '</artist>
   <country>' . $news_date . '</country>
   <company>' . $description . '</company>

        </cd>';
} // End while loop
mysql_close(); // close the mysql database connection
$xmlBody .= "</catalog>";
echo $xmlBody; // output the gallery data as XML file for flash
?>

so you can create xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
		<html>
			<style type="text/css">
			
table
{
width:100%;
/*background-color:#9acd32;*/
/*border-collapse:collapse;*/

}
tr
{
background-color:#9acd32;
/*border-collapse:collapse;*/

}
table,th,td
{
border: 2px solid black;
margin:2px;
}
td
{
width:50%;
background-color:#e6e6e6;
padding:30px;
text-align:center;
}

			</style>
			<body>
				<h2>My CD Collection</h2>
				<xsl:apply-templates select="catalog"/>
			</body>
		</html>
	</xsl:template>


	<xsl:template match="catalog">
		<table>

			<tr>
				<th>Title</th>
				<th>Artist</th>
			</tr>
			<xsl:apply-templates select="cd"/>
		</table>
	</xsl:template>

	<xsl:template match="cd">
		<tr>

			<td>
				<xsl:value-of select="title"/>
			</td>

			<td>
				<xsl:value-of select="artist"/>
			</td>
		</tr>
	</xsl:template>
</xsl:stylesheet>

result Hmtl

<html>
  <style type="text/css">
			
table
{
width:100%;
/*background-color:#9acd32;*/
/*border-collapse:collapse;*/

}
tr
{
background-color:#9acd32;
/*border-collapse:collapse;*/

}
table,th,td
{
border: 2px solid black;
margin:2px;
}
td
{
width:50%;
background-color:#e6e6e6;
padding:30px;
text-align:center;
}

			
  </style>
  <body>
    <h2>My CD Collection</h2>
    <table>
      <tr>
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <tr>
        <td>Hello again</td>
        <td>Fred</td>
      </tr>
      <tr>
        <td>Jabbja</td>
        <td>Barny</td>
      </tr>
    </table>
  </body>
</html>

Hi thanks for the help unfotunaly one lil problem i keep getting a error higlighted on this line

$xmlBody .= "<?xml-stylesheet type="text/xsl" href="books06.xsl"?> ";

which within the overall code i think i'm missing a php close or open tag somewhere not sure but i cnt seem to find can anyone spot it

<?php

      // PHP file that renders perfect Dynamic XML for MySQL Database result sets

      // Script written by Adam Khoury @ http://www.developphp.com - April 05, 2010

      // View the video that is tied to this script for maximum understanding

      // -------------------------------------------------------------------
      header("Content-Type: text/xml"); //set the content type to xml

      // Initialize the xmlOutput variable

      $xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?> ';

      $xmlBody .= "<catalog> ";

$xmlBody .= "<?xml-stylesheet type="text/xsl" href="books06.xsl"?> ";
?>
      $xmlBody .= "
<?php // Connect to your MySQL database whatever way you like to here

mysql_connect("localhost","root","") or die (mysql_error());

      mysql_select_db("admin") or die ("no database");

      // Execute the Query on the database to select items(20 in this example)

      $sql = mysql_query("SELECT * FROM news ORDER BY news_date DESC LIMIT 0, 20");

      while($row = mysql_fetch_array($sql)){

     // Set DB variables into local variables for easier use

      $news_id = $row["news_id"];

      $subject = $row["subject"];

      $news_date = strftime("%b %d, %Y", strtotime($row["news_date"]));

      $news_artical = $row["news_artical"];
      // Start filling the $xmlBody variable with looping content here inside the while loop
      // It will loop through 20 items from the database and render into XML format

       

      $xmlBody .= '

      <cd>

       

      <title> ' . $news_id . '</title>

      <artist> ' . $subject . '</artist>

      <country> ' . $news_date . '</country>

      <company> ' . $description . '</company>

       

      </cd> ';

      } // End while loop

      mysql_close(); // close the mysql database connection

      $xmlBody .= "</catalog> ";

      echo $xmlBody; // output the gallery data as XML file for flash

      ?>

change it to: $xmlBody .= '<?xml-stylesheet type="text/xsl" href="books06.xsl"?>';

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.