Jump to content

New to PHP please help


unplugged_web
 Share

Recommended Posts

  • Replies 113
  • Created
  • Last Reply

Top Posters In This Topic

Will it work them?
I suppose you mean "Will it work then?". Well, I guess it will, but I'm not completely certain. Still, this is a must for you to actually have a shot with it.
Link to comment
Share on other sites

I want to learn more about PHP and XML, but don't really know where to start. Am I right in thinking that the PHP for the page will get the information from the query string, save it (test2.xml) then get the saved file and use the XSL file (test2.xsl) to extract the data and structure it in the right way before saving it to a new file (Marquee.xml)?Sorry if it seems like a really basic thing to ask, but if I can understand how it's all written I then stand a better chance of learning it.Once again, thank you for all of your helpLucy

Link to comment
Share on other sites

Well, actually the line

$destination = dirname(__FILE__) . "/saved.xml"; //save the xml in a file called saved in the current folder

suggests that the contents will be written in a file called "saved.xml" in the current folder. Not test2.xml. At least that happens when the script runs.You've got the rest correctly though. The script will from then on take an XML file, which is test2.xml as specified further below on this line:

$xmlFile = 'test2.xml';

(you can get the saved.xml file if you wish) and uses the XSL(T) file to create a new XML file, which is then saved over Marquee.xml.Note that the way I've made that stylesheet, the new data will be added to the existing one in Marquee.xml, rather then being completely replaced as it would normally happen. If you want a complete replacement, there are one or two small fixes that need to be done and you'll be set.

Link to comment
Share on other sites

Well, actually the line
$destination = dirname(__FILE__) . "/saved.xml"; //save the xml in a file called saved in the current folder

suggests that the contents will be written in a file called "saved.xml" in the current folder. Not test2.xml. At least that happens when the script runs.You've got the rest correctly though. The script will from then on take an XML file, which is test2.xml as specified further below on this line:

$xmlFile = 'test2.xml';

(you can get the saved.xml file if you wish) and uses the XSL(T) file to create a new XML file, which is then saved over Marquee.xml.Note that the way I've made that stylesheet, the new data will be added to the existing one in Marquee.xml, rather then being completely replaced as it would normally happen. If you want a complete replacement, there are one or two small fixes that need to be done and you'll be set.

Thanks, I'm SLOWLY starting to learn. I guess that if I wanted to overwrite the existing file the I would change:
 <xsl:output indent="yes"/>	<xsl:param name="original" select="Marquee.xml"/>

to:

 <xsl:output indent="no"/>	<xsl:param name="original" select="Marquee.xml"/>

Thanks

Link to comment
Share on other sites

Thanks, I'm SLOWLY starting to learn. I guess that if I wanted to overwrite the existing file the I would change:
 <xsl:output indent="yes"/>	<xsl:param name="original" select="Marquee.xml"/>

to:

 <xsl:output indent="no"/>	<xsl:param name="original" select="Marquee.xml"/>

Thanks

No. To "indent" something in a code sence means to "arrange it in a readable fashion". With indent="yes", the resulting XML tree will be arranged in a way that all of the code will be readable. For example, look at the XSLT code itself. That code is indented. The below code is that EXACT same code which will do the EXACT same job, but it's just harder to find things out in it:
<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output indent="yes"/><xsl:param name="original" select="Marquee.xml"/><xsl:template match="/CasinoInfo"><!--Parameter addded for performance's sake. Otherwise doable with "../CasinoName" within the for-each--><xsl:param name="casino" select="CasinoName"/><PhoneCasino><xsl:copy-of select="document($original)/PhoneCasino/*"/><xsl:for-each select="Games/*"><Casino><CasinoName><xsl:value-of select="$casino"/></CasinoName><Game><xsl:value-of select="title"/></Game> <Winner><xsl:value-of select="lastWinner"/></Winner><won><xsl:value-of select="about"/></won><!--There seem to be some games that don't have a jackpot written. If you want to have empty <Jackpot/> elements, remove this xsl:if element--><xsl:if test="jackpot"><Jackpot><xsl:value-of select="jackpot/progressive/penceInteger"/></Jackpot></xsl:if></Casino></xsl:for-each></PhoneCasino></xsl:template><!--Just to eliminate some of the extra spaces--><xsl:template match="text()"><xsl:value-of select="normalize-space(.)"/></xsl:template></xsl:stylesheet>

That is however what you'll see with indent="no". If the file is not ever going to be edited by a human though, using indent="no" is acceptable, as it reduces file size (since all of the extra space, which doesn't do any work anyway, is gone).If you want a complete replacement, the thing you MUST change is to remove the line:

<xsl:copy-of select="document($original)/PhoneCasino/*"/>

This is the line that copies the whole contents of the original file before processing the new one, adding things after it. Eliminating this means you get only the new stuff, and since PHP overwrites the old file, you'll lose the old data.For better performance though, you MAY also do the following things:

  1. From the XSLT, remove the line
    <xsl:param name="original" select="Marquee.xml"/>

    This was only used in the copying, so it's not needed during the transformation.

  2. Remove the following part from the PHP file:
    //Tell the XSLT stylesheet the name of your new file$xslt->setParameter(null, 'original', $newFile);

    This is what adjusted the value of the xsl:param value in case you moved/renamed your original file. All done to preserve adding things up, rather then removing them. Also, since the xsl:param is removed, specifying this doesn't really do anything.

Now, I have a big urge to say that ever since I was first summoned. Remember that in the very beginning I said I would use DOM's load() function and simply save() the document again (that was in another topic btw)? Well, if it's only save()-ing you want, that's not the best method, but when you want to do an XSLT transformation like you are doing, that IS the best method. Here's a code that will fetch the remote file for you, save a copy of it, do the transformation, save the result and giving you a "OK" message. All with error handlers to ensure everything is OK, comments for you to see what's going on and probably more efficient (fast) then the codes up until now:

<?php//Your files. The saved XSLT file, the remote XML, the location of the local copy and result.$xmlFile = 'http://stats.probability.tv/feed.php?key=mypassword'; //The remote file$xmlFileLocal = 'test2.xml'; //The location of the local copy$xsltFile = 'test2.xsl'; //The XSLT file$newFile = 'Marquee.xml'; //The location of the transformed file//Display any PHP errors found further below (if any)ini_set('display_errors','On');//Verify the file paths.if (!file_exists($xmlFile)) {die('No XML file at "' . $xmlFile . '". Verify the file exists and you have permissions to view it. Also check for spelling mistakes');}if (!file_exists($xsltFile)) {die('No XSLT file at "' . $xsltFile . '". Verify the file exists and you have permissions to view it. Also check for spelling mistakes');}//Prepare the XML file$xml = new DomDocument;if (!$xml->loadXML(trim(file_get_contents($xmlFile)))) {/*I've only used loadXML() because you said the XML has spaces before the prolog.That may cause problems if just load()-ed. I'm not sure though.In general, you should use load() as done below with the XSLT file.*/die('XML file "' . $xmlFile . '" is not well-formed.');}//Indent the file that would be saved$xml->formatOutput = true;//Save a local copy of the remote file before transforming it$xml->save($xmlFileLocal);//Prepare the XSLT file$xsl = new DomDocument;if (!$xsl->load($xsltFile)) {die('XSLT file "' . $xsltFile . '" is not well-formed XML document.');}//Ensure the XSLT processor is availableif (!class_exists('XSLTProcessor')) {die('The PHP XSL extension is not available and it is needed for this to work. Contact your host to enable it.');}//Load the XSLT processor$xslt = new XSLTProcessor;$xslt->importStylesheet($xsl);//Tell the XSLT stylesheet the name of your new file$xslt->setParameter(null, 'original', $newFile);//Execute the transformationif (!$xslt->transformToURI($xml, $newFile)) {die('Error during transformation of "' . $xmlFile . '" with  "' . $xsltFile . '" as stylesheet. Possibly an endless loop or illegal XSLT/XPath construct. Other possibilies include disability to write "' . $newFile . '"');}echo 'The script executed succesfully. A local copy of "' . $xmlFile . '" is available at "' . $xmlFileLocal . '" and a transformed by "' . $xsltFile . '" version is available at "' . $newFile . '".';?>

Note that I have never used class_exists() before. So if this doesn't work and the previous did, try removing the part:

//Ensure the XSLT processor is availableif (!class_exists('XSLTProcessor')) {die('The PHP XSL extension is not available and it is needed for this to work. Contact your host to enable it.');}

And as with the above code, you can do number 2 on the list above here too.Just for the record, here's a version without any error handling (other then the built in PHP error handlers) and most of the comments removed:

<?php//Your files. The saved XSLT file, the remote XML, the location of the local copy and result.$xmlFile = 'http://stats.probability.tv/feed.php?key=mypassword'; //The remote file$xmlFileLocal = 'test2.xml'; //The location of the local copy$xsltFile = 'test2.xsl'; //The XSLT file$newFile = 'Marquee.xml'; //The location of the transformed fileini_set('display_errors','On');$xml = new DomDocument;$xml->loadXML(trim(file_get_contents($xmlFile)));$xml->formatOutput = true;$xml->save($xmlFileLocal);$xsl = new DomDocument;$xsl->load($xsltFile);$xslt = new XSLTProcessor;$xslt->importStylesheet($xsl);$xslt->setParameter(null, 'original', $newFile);$xslt->transformToURI($xml, $newFile);echo 'The script executed succesfully. A local copy of "' . $xmlFile . '" is available at "' . $xmlFileLocal . '" and a transformed by "' . $xsltFile . '" version is available at "' . $newFile . '".';?>

If the code WITH error handlers executes without any problems, this one will too and is even faster, since it doesn't perform any checks on the way. Not to mention it's smaller.

Link to comment
Share on other sites

Thank you for all of your help. I'm just waiting for the account details for the hosting company so that I can get them to enable the php xsl extenstion, then I can test it all.I just wanted to clafify what this bit of code did:

//Your files. The saved XSLT file, the remote XML, the location of the local copy and result.$xmlFile = 'http://stats.probability.tv/feed.php?key=mypassword'; //The remote file$xmlFileLocal = 'test2.xml'; //The location of the local copy$xsltFile = 'test2.xsl'; //The XSLT file$newFile = 'Marquee.xml'; //The location of the transformed file

My understanding of it is that it gets the xml from the query string, takes the xsl and uses that to transform it into Marquee.xml. The only thing I wasn't sure about was the xmlFileLocal bit.Once again thank you.Lucy

Link to comment
Share on other sites

I just tested the php file and got an error message saying that: No XML file at "http://stats.probability.tv/feed.php?key=<my password>". Verify the file exists and you have permissions to view it. Also check for spelling mistakes.Would I be better of going back to the original php file and using that because I got a message with that one telling me that the file had be successfully written?

Link to comment
Share on other sites

And if you type in the adress, it does exist? Well... in that case, try to simply remove the check, that is this code:

//Verify the file paths.if (!file_exists($xmlFile)) {die('No XML file at "' . $xmlFile . '". Verify the file exists and you have permissions to view it. Also check for spelling mistakes');}

It may be that file_exists() doesn't work on remote files.If you get the

'XML file "' . $xmlFile . '" is not well-formed.'

error after doing so, you could try using this code instead:

<?php//Your files. The saved XSLT file, the remote XML, the location of the local copy and result.$xmlFile = 'http://stats.probability.tv/feed.php?key=mypassword'; //The remote file$xmlFileLocal = 'test2.xml'; //The location of the local copy of the remote file$xsltFile = 'test2.xsl'; //The XSLT file$newFile = 'Marquee.xml'; //The location of the transformed file//Display any PHP errors found further below (if any)ini_set('display_errors','On');if (!file_exists($xsltFile)) {die('No XSLT file at "' . $xsltFile . '". Verify the file exists and you have permissions to view it. Also check for spelling mistakes');}//Save a local copy of the remote file without the extra spaces at the beginningfile_put_contents($xmlFileLocal, trim(file_get_contents($xmlFile)));//Prepare the XML file$xml = new DomDocument;if (!$xml->load($xmlFileLocal)) {die('XML file "' . $xmlFile . '" is not well-formed.');}//Prepare the XSLT file$xsl = new DomDocument;if (!$xsl->load($xsltFile)) {die('XSLT file "' . $xsltFile . '" is not well-formed XML document.');}//Ensure the XSLT processor is availableif (!class_exists('XSLTProcessor')) {die('The PHP XSL extension is not available and it is needed for this to work. Contact your host to enable it.');}//Load the XSLT processor$xslt = new XSLTProcessor;$xslt->importStylesheet($xsl);//Tell the XSLT stylesheet the name of your new file$xslt->setParameter(null, 'original', $newFile);//Execute the transformationif (!$xslt->transformToURI($xml, $newFile)) {die('Error during transformation of "' . $xmlFile . '" with  "' . $xsltFile . '" as stylesheet. Possibly an endless loop or illegal XSLT/XPath construct. Other possibilies include disability to write "' . $newFile . '"');}echo 'The script executed succesfully. A local copy of "' . $xmlFile . '" is available at "' . $xmlFileLocal . '" and a transformed by "' . $xsltFile . '" version is available at "' . $newFile . '".';?>

Not reccomended if after the minor corrections suggested above, the previous works.

Link to comment
Share on other sites

I removed the verify bit of the code and got an error mesage saying:Fatal error: Cannot instantiate non-existent class: domdocument in e:\domains\t\thephonecasino.com\user\htdocs\xml_feed.php on line 12Line 12 is

$xml = new DomDocument;

The orginal code:

<?phpif (!defined('FILE_APPEND'))  define('FILE_APPEND', 1);if (!function_exists('file_put_contents')){function file_put_contents($n, $d, $flag = false){  $mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';  $f = @fopen($n, $mode);  if ($f === false)  {	return 0;  }  else  {	if (is_array($d)) $d = implode($d);	$bytes_written = fwrite($f, $d);	fclose($f);	return $bytes_written;  }}}$xml = "http://stats.probability.tv/feed.php?key=<my password>";$destination = dirname(__FILE__) . "/saved.xml"; //save the xml in a file called saved in the current folder$data = file_get_contents($xml);if (!$bytes = file_put_contents($destination, trim($data))) //trim removes top blank lines  echo "Writing to {$destination} failed";else  echo "Succesfully wrote {$destination}, saved {$bytes} bytes";//Your files. The saved XSLT file and the two XMLs to be more precise. This sample code assumed the XSLT is saved as test2.xsl$xsltFile = 'test2.xsl';$xmlFile = 'saved.xml';$newFile = 'Marquee2.xml';//Verify the file paths.if (!file_exists($xmlFile)) {die('No XML file at "' . $xmlFile . '". Verify the file exists and check for spelling mistakes');}if (!file_exists($xsltFile)) {die('No XSLT file at "' . $xsltFile . '". Verify the file exists and check for spelling mistakes');}//Prepare the XML file$xml = new DomDocument;if (!$xml->load($xmlFile)) {die('XML file "' . $xmlFile . '" is not well-formed.');}//Prepare the XSLT file$xsl = new DomDocument;if (!$xsl->load($xsltFile)) {die('XSLT file "' . $xsltFile . '" is not well-formed XML document.');}//Load the XSLT processor$xslt = new Xsltprocessor;$xslt->importStylesheet($xsl);//Tell the XSLT stylesheet the name of your new file$xslt->setParameter(null, 'original', $newFile);//Execute the transformationif (!$xslt->transformToURI($xml, $newFile)) {die('Error during transformation of "' . $xmlFile . '" with  "' . $xsltFile . '" as stylesheet. Possibly an endless loop or illegal XSLT/XPath construct. Other possibilies include disability to write "' . $newFile . '"');}?>

Seems to work okay (at the moment I can't test it with the xsl file) so should I just leave it like that?

Link to comment
Share on other sites

Oh no :) .... no no no no... dear god, please don't tell me you have PHP4... PLEASE!!!If you don't know that, create a new PHP file with the following contents:

<?php phpinfo() ?>

View the file and tell me what is the version of PHP (it's written at the very top). Is it at least 5.0.0? If you have 4.*.*, this will require a totally different code for the transformations and most of the error handlers you see now won't be available.

Link to comment
Share on other sites

Oh no :) .... no no no no... dear god, please don't tell me you have PHP4... PLEASE!!!If you don't know that, create a new PHP file with the following contents:
<?php phpinfo() ?>

View the file and tell me what is the version of PHP (it's written at the very top). Is it at least 5.0.0? If you have 4.*.*, this will require a totally different code for the transformations and most of the error handlers you see now won't be available.

I'm afraid it said PHP 4.4.4 Does that mean I can't do it?
Link to comment
Share on other sites

I'm afraid it said PHP 4.4.4 Does that mean I can't do it?
You can, but the code for it is almost totally different and much less... safe. I'll check it out and give you the full code later today.
Link to comment
Share on other sites

OK. Here's the new old PHP4 version, again with lots of error handlers, though it misses a few, such as well formness checks.

<?php//Your files. The saved XSLT file, the remote XML, the location of the local copy and result.$xmlFile = 'http://stats.probability.tv/feed.php?key=mypassword'; //The remote file$xmlFileLocal = 'test2.xml'; //The location of the local copy of the remote file$xsltFile = 'test2.xsl'; //The XSLT file$newFile = 'Marquee.xml'; //The location of the transformed file//Display any PHP errors found further below (if any)ini_set('display_errors','On');//The definition of file_put_contents() in case it's not available which is most likely as that function was added in PHP5.0.0.if (!function_exists('file_put_contents')){if (!defined('FILE_APPEND')) {  define('FILE_APPEND', 1);}function file_put_contents($n, $d, $flag = false){  $mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';  $f = @fopen($n, $mode);  if ($f === false)  {	return 0;  }  else  {	if (is_array($d)) $d = implode($d);	$bytes_written = fwrite($f, $d);	fclose($f);	return $bytes_written;  }}}//Save a local copy of the remote file at the specified location for a local copy//This will also stript out extra spaces at the beginning and the end of the documentfile_put_contents($xmlFileLocal, trim(file_get_contents($xmlFile)));//Prepare the XSLT processor.$xh = xslt_create() or die('The XSLT exntesion is not enabled. Contact your host to enable it');//Execute the transformationif (!xslt_process($xh, $xmlFileLocal, $xsltFile, $newFile)) {die('Error during transformation of "' . $xmlFile . '" with  "' . $xsltFile . '" as stylesheet. Possibly an endless loop or illegal XSLT/XPath construct. Other possibilies include disability to write "' . $newFile . '"');}//Free the XSLT processor.xslt_free($xh);echo 'The script executed succesfully. A local copy of "' . $xmlFile . '" is available at "' . $xmlFileLocal . '" and a transformed by "' . $xsltFile . '" version is available at "' . $newFile . '".';?>

Link to comment
Share on other sites

Sorry to cause you so many problems, but I tried the code you said and got an error which said: Fatal error: Call to undefined function: xslt_create() in e:\domains\t\thephonecasino.com\user\htdocs\xml_feed.php on line 40Line 40 is:

$xh = xslt_create() or die('The XSLT exntesion is not enabled. Contact your host to enable it');

Thanks

Link to comment
Share on other sites

Sorry to cause you so many problems, but I tried the code you said and got an error which said: Fatal error: Call to undefined function: xslt_create() in e:\domains\t\thephonecasino.com\user\htdocs\xml_feed.php on line 40Line 40 is:
$xh = xslt_create() or die('The XSLT exntesion is not enabled. Contact your host to enable it');

Thanks

Could it be because php xsl isn't enabled on the server yet?
Link to comment
Share on other sites

Could it be because php xsl isn't enabled on the server yet?
That's exactly the reason. Only... in PHP4 it's the XSLT extension. Your host should be smart enough to figure that out though. I mean... there's no reason to contact them again.For better error handling in the future, replace
//Prepare the XSLT processor.$xh = xslt_create() or die('The XSLT exntesion is not enabled. Contact your host to enable it');

with

//Check if the XSLT extension is availableif (!function_exists('xslt_create')) {die('The XSLT exntesion is not enabled. Contact your host to enable it.');}//Prepare the XSLT processor.$xh = xslt_create();

Link to comment
Share on other sites

  • 2 weeks later...

Sorry I'm back again. The host has finally enabled the XSLT extension and I've tested the file, but it came up with an error that said:

Warning: Sablotron error on line none: cannot open file 'c:/windows/system32/inetsrv/structure.xsl' in e:\domains\t\thephonecasino.com\user\htdocs\xml_transform.php on line 47Error during transformation of "http://stats.probability.tv/feed.php?key=mypassword" with "structure.xsl" as stylesheet. Possibly an endless loop or illegal XSLT/XPath construct. Other possibilies include disability to write "Marquee.xml"
I didn't know what it meant or how to correct it. Line 47 is:
if (!xslt_process($xh, $xmlFileLocal, $xsltFile, $newFile)) {

Thanks

Link to comment
Share on other sites

First stop, on the next line (48), replace

$xmlFile

with

$xmlFileLocal

You should set all files to be in the same folder to avoid this kind of problems. It seems PHP is getting the wrong path to the file. Is the XSLT actually saved as "structure.xsl" in the same folder as the PHP file?Good news btw, you're almost there. Once this error is over with, the script is finished.

Link to comment
Share on other sites

First stop, on the next line (48), replace
$xmlFile

with

$xmlFileLocal

You should set all files to be in the same folder to avoid this kind of problems. It seems PHP is getting the wrong path to the file. Is the XSLT actually saved as "structure.xsl" in the same folder as the PHP file?Good news btw, you're almost there. Once this error is over with, the script is finished.

It's good news to know that this is nearly sorted. In answer to your question yes they are both in the same folder (htdocs) and I copied the file name and pasted it straight into the php just to make sure I hadn't made a spelling mistake. Before the XSLT was enabled though the php file was able to create the local xml file (which I called remote).Would it help if I added http://etc to the paths?
Link to comment
Share on other sites

It's good news to know that this is nearly sorted. In answer to your question yes they are both in the same folder (htdocs) and I copied the file name and pasted it straight into the php just to make sure I hadn't made a spelling mistake. Before the XSLT was enabled though the php file was able to create the local xml file (which I called remote).Would it help if I added http://etc to the paths?
Nope I tried adding http to the paths, but that didn't work.
Link to comment
Share on other sites

It's good news to know that this is nearly sorted. In answer to your question yes they are both in the same folder (htdocs) and I copied the file name and pasted it straight into the php just to make sure I hadn't made a spelling mistake. Before the XSLT was enabled though the php file was able to create the local xml file (which I called remote).Would it help if I added http://etc to the paths?
I don't think PHP4 supports fetching remote files for transformation. That's why I used $xmlFileLocal in the transformation call, rather then $xmlFile. I know PHP5 does support this, but I'm not sure for 4. Try it anyways.Do you mean now the local copy is not created? Huh, odd... if it wasn't, PHP was probably going to throw a warning at it too. What's the complete code now?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×
×
  • Create New...