[SOLVED] Outputting XML with PHP
Hi guys,
I would like to create a xml document with php. The xml should contain the database fields from the catalog extention.
I have created a php file, but it does not really work. Since I am not a really good php user, any help is appreciated, because I am very willing to learn!
I get the response ‘Het was niet mogelijk de querie uit te voeren’
The url can be found here: http://www.vrtu.nl/tl_files/xml/xml.php
Code:
// Voer een querie uit.
$query = 'SELECT vacature_nummer, vacature, invoerdatum, functieomschrijving, plaats, provincie FROM vacatures';
$rs = mysql_query( $query, $link );
if( !$rs )
{
echo 'Het was niet mogelijk om de querie uit te voeren.';
exit( 1 );
}
I don't really think this is of much use to anyone,
but i can't seem to put more code online, it gives an error...
Re: Outputting XML with PHP
If all you want to do is get an XML output of a catalog, why not just use Contao to do that?
* Copy fe_page.tpl and turn it into your XML shell (declaration, opening/closing XML tags, and the PHP code for the main column is all you need in this).
* Then make a new catalog list module. Choose the table you want to ouptut, and make a custom template for this as well that formats it in the XML you want.
* Then make a new layout that uses your fe_page template, and has the catalog list module in the main column.
* Then make a new page on the site that uses this layout.
Once you do that, you can go to that page and see your XML outputted.
Re: Outputting XML with PHP
HTML is kind of like a subset of XML. That means if you can generate HTML and you can change all templates that you can probably also generate XML. Following the instructions from Medianomoly you should be able to do as you want to.
Re: Outputting XML with PHP
hey Medianomaly,
I did not know that is possible :-)
As said, i am not a very strong php'er. I am not quit sure how to alter fe_page.tpl
I have now entered the xml shell in it:
Code:
<div id="content-vervolg"><?php echo $this->main; ?><jobs version="1.0">
<job>
<jobId></jobId>
<jobAddedDate></jobAddedDate>
<jobTitle></jobTitle>
<jobDescription></jobDescription>
<jobLocation>
<locationPlace></locationPlace>
<locationProvince></locationProvince>
</jobLocation>
<jobCategory></jobCategory>
<jobEducation></jobEducation>
<jobUrl>http://www.vrtu.nl/index.php/vacatures.html</jobUrl>
<contractHours>
<hoursType/>
<hoursMin>40</hoursMin>
<hoursMax>40</hoursMax>
<hoursDescription/>
</contractHours>
<jobContact>
<contactName></contactName>
<contactPhone></contactPhone>
<contactEmail></contactEmail>
<contactUrl>http://www.vrtu.nl/index.php/contact.html</contactUrl>
</jobContact>
<jobBranch>
<branchName>Van Roosendaal technisch uitzendbureau</branchName>
<branchPlace>Sliedrecht</branchPlace>
</jobBranch>
</job>
</jobs></div>
Thing is, this is for a site which displays vacancies for employment agencies. They use a different shell then my catalogdatabase entries.
I have created the following, but i do honestly not know where to put it:
Code:
$xml .= '<job>' . EOL;
$xml .= ' <jobId>' . $row[ 'vacature_nummer' ] . '</jobId>' . EOL;
$xml .= ' <jobAddedDate>' . $row[ 'invoerdatum' ] . '</jobAddedDate>' . EOL;
$xml .= ' <jobTitle>' . htmlspecialchars( $row[ 'vacature' ] ) . '</jobTitle>' . EOL;
$xml .= ' <jobDescription><![CDATA[' . $row[ 'functieomschrijving' ] . ']]></jobDescription>' . EOL;
$xml .= '<jobLocation>' . EOL;
$xml .= ' <locationPlace>' . $row[ 'plaats' ] . '</locationPlace>' . EOL;
$xml .= ' <locationProvince>' . $row[ 'provincie' ] . '</locationProvince>' . EOL;
$xml .= '</jobLocation>' . EOL;
$xml .= ' <jobCategory>' . $row[ 'vakgebied' ] . '</jobCategory>' . EOL;
$xml .= ' <jobEducation>' . $row[ 'vereiste_opleiding' ] . '</jobEducation>' . EOL;
$xml .= ' <jobUrl>' . ( $row[ 'vacature' ] ) . '</jobUrl>' . EOL;
$xml .= '<contractHours>' . EOL;
$xml .= ' <hoursMin>' . $row[ 'uren_per_week' ] . '</hoursMin>' . EOL;
$xml .= ' <hoursMax>' . ( $row[ 'uren_per_week' ] ) . '</hoursMax>' . EOL;
$xml .= '</contractHours>' . EOL;
$xml .= '<jobContact>' . EOL;
$xml .= ' <contactName>' . $row[ 'contactpersoon' ] . '</contactName>' . EOL;
$xml .= ' <contactPhone>' . ( $row[ 'contactpersoon_telefoon' ] ) . '</contactPhone>' . EOL;
$xml .= ' <contactEmail>' . $row[ 'contactpersoon_email' ] . '</contactEmail>' . EOL;
$xml .= ' <contactUrl>' . ( $row[ '' ] ) . '</contactUrl>' . EOL;
$xml .= '</jobContact>' . EOL;
/* $xml .= '<jobBranch>' . EOL;
$xml .= ' <branchName>' . $row[ 'uren_per_week' ] . '</branchName>' . EOL;
$xml .= ' <branchPlace>' . ( $row[ 'uren_per_week' ] ) . '</branchPlace>' . EOL;
$xml .= '</contractHours>' . EOL;
*/
$xml .= '</job>' . EOL;
}
$xml .= '</jobs>';
Re: Outputting XML with PHP
Copy fe_page.tpl to your templates folder and rename it "fe_page_xml.tpl"
This is all that needs to go in it:
Code:
<?xml version="1.0" encoding="utf-8"?>
<?php echo $this->main; ?>
Then you need to make a catalog list template, which requires 2 files, call them "mod_cataloglist_xml.tpl" and "catalog_xml.tpl". The first one is the template for the catalog list module, the second is a "partial", meaning it is a template that is repeated for each row of data that comes through it.
mod_cataloglist_xml.tpl:
Code:
<jobs>
<?php echo $this->catalog; ?>
</jobs>
catalog_xml.tpl:
Code:
<?php foreach ($this->entries as $entry): ?>
<jobId></jobId>
<jobAddedDate></jobAddedDate>
<jobTitle></jobTitle>
<?php endforeach; ?>
Now what goes inside those XML tags you have to do on your own, because it depends on how you've configured the catalog, plus it's a good exercise to learn. So that's your homework.
Most will look something like the following:
<?php echo $entry['data']['someFieldICreated']['value']; ?>
You can paste <?php print_r($entry); ?> into "catalog_xml.tpl" INSIDE THE FOR LOOP and it will spit out all of the available data to you.
There's very little PHP to this, mostly its just changing what's already there, and adding your own XML code.
But in reality, to use the Catalog, you should have at least a little bit of PHP knowledge to write your templates and such. So that excuse only goes so far.
Re: Outputting XML with PHP
Thank you so much for helping me. Without generous people like you it would be impossible to improve our skills!
thanks for your time.
I figured it out! Almost.
I have got the xml output working! That's just fantastic, and so easy to do.
Only one little thing. When i make the fe_page_xml.tpl create xml by entering
Code:
<?xml version="1.0" encoding="utf-8"?>
<?php echo $this->main; ?>
it does not work. my output is blank.
When i use the 'normal' doctype heading it works fine.
I am not quit sure why this is. Is it maybe because contao generates. html and now xml is generated?
Re: Outputting XML with PHP
sorry let me guess blindly, when you say
Quote:
When i make the fe_page_xml.tpl
Did you choose the right template?
Re: Outputting XML with PHP
Re: Outputting XML with PHP
Is there any way you could post something for us to see?
The best thing would be 2 pages -- one with the XML declaration that does not work, and another with the normal doctype that does work.
I'm curious to see what it spits out in both cases -- and if the XML version is actually spitting out code but it's just not rendering in the browser.
Also, do you have error messages turned on, too?
Re: Outputting XML with PHP
Ok, I've turned on error messages.
This is the page http://www.pukt.nl/roosendaal/index.php/xml.html
where i use the fe_xml.tpl with this code:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<jobs version="1.0">
<?php echo $this->main; ?>
</jobs>
Here is use my 'normal' fe_page file whithout <?xml version="1.0" encoding="UTF-8"?> in it.
http://www.pukt.nl/roosendaal/index.php/xml-normal.html
It also gives an error message in here when I place it before <?php echo $this->main; ?>
So I know it is going wrong with <?xml version="1.0" encoding="UTF-8"?> but I do not really know what
Re: Outputting XML with PHP
Try to generate that xml prologue in php string.
Code:
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<jobs version="1.0">
<?php echo $this->main; ?>
</jobs>
Re: Outputting XML with PHP
An "unexpected T-string" means a character is terminating (the T in t-string) your output.
Not sure what in your case, maybe ?>
Often the offending character will need escaping with a backslash eg ?\>
Sometimes ' instead of " (and vice versa) makes a difference, or escaping \"
A backslash tells the php engine that this is just a normal character, not part of the control syntax.
You might need to play around, but ?> seems the likely culprit.
Re: Outputting XML with PHP
I think Ramjet's right...
I think many/most servers are configured to treat "<?" as an opening PHP tag. So it sees that and thinks what's in it should be PHP code.
Let us know if Tsarma's solution works -- I'm curious now myself.
Re: Outputting XML with PHP
Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.....
Indeed! That works!
You guys rock! Thank you so much for helping me out. It is always nice to learn something new!
There is just one thing I wonder....
I made the template mod_cataloglist_xml as Medianomaly suggested. But it still made the connection to the mod_cataloglist.tpl. (spittiign out <div class="mod_cataloglist block"> )
So in system->module -> catalog_ext -> ModuleCatalogList.php I changed mod_cataloglist into mod_cataloglist_xml
expexting it would also affect my catalog with vacancies. But it does not? They are still showing perfect.
The XML file is now showing without the div, but also my vacancylist is still showing like it should? This confuses me a little bit. (Good = good you might say, but i just expected diffrent behaviour)
Re: Outputting XML with PHP
The class is just to label what type of module it is -- it does not reflect the actual name of the template. So it's OK that the template file name is different from the CSS class. Likely when you changed the code, if you did a view source you'd see "mod_cataloglist_xml" as the class instead, but since you're not using it to style anything, you didn't see a difference.
Your jobs XML modules and your vacancies modules are (or should be) configured to use different templates, which you did when you created them. As a result a change to one template will not affect the other.
If you just want to change some code to see what happens (and then change back) -- cool. A general rule you always hear from people is not to change the core code. 95% of the time you can do what you need without it.
Re: Outputting XML with PHP
I agree on not changing the core code.
But let me ask it differently. I made the mod_cataloglist_xml.tpl
with the following code in it
Code:
<jobs>
<?php echo $this->catalog; ?>
</jobs>
When i alter this code, let say for instance <div class="tralalala"> </div> instead of <jobs></jobs>
nothing changes. So how is this template attached to the xml catalog list?
As far as my question before about ModuleCatalogList.php, indeed this also changed my vacancy cataloglist. It just did not alter my layout. Also the div with mod_cataloglist block was deleted.
Thing is, i need the xml output for an external party and they do not want/accept any other code in it. Therefore I need the div gone. I can change it by altering ModuleCatalogList.php, but apparently, this is not what i really want. Is there another way to achieve this?
Re: Outputting XML with PHP
I'm not sure I understand 100%.
But basically, you have 2 catalogs -- jobs and vacancies.
You have 2 catalog list module templates:
mod_cataloglist_xml.tpl
mod_cataloglist_vacancies.tpl (It doesn't have to be named this, just a guess).
And two catalog list "partial" templates:
catalog_xml.tpl
catalog_vacancies.tpl (Also can be anything, this is just a guess).
And you have 2 separate Catalog list modules created in the backend, one for each catalog.
In the module settings, there are 2 separate select dropdowns to choose the templates for this module. They are labeled "Catalog Layout" and "Catalog Template". Make sure you select the correct templates that go with each module.
If you've done that, and you've also got the custom fe_page_xml template done, you should not see a div anywhere in the source code.
Re: Outputting XML with PHP
Quote:
In the module settings, there are 2 separate select dropdowns to choose the templates for this module. They are labeled "Catalog Layout" and "Catalog Template"
This is where my confusion comes from I think. Because in my catalog list module i can only select the catalog layout, so that's catalog_xml.tpl.
The dropdown for the catalog template i do not see, or better said it is truly not there....
?
Re: Outputting XML with PHP
EDIT I just changed the mod_catalog_xml and it does show on the front.
(unlike my post yesterday)
So it does connect to the xml catalog. But where? This is what i would like to know for my own knowledge....
Re: Outputting XML with PHP
You should see the following...
[attachment=0:1go6d2cf]sceen.jpg[/attachment:1go6d2cf]
I'm sorry but I don't understand the question. Do you mean how does it find the file?
Re: Outputting XML with PHP
Now I get it.
When open the catalog list module, i can only choose catalog layout, not catalog template. So, i cannot choose whether to use mod_cataloglist, or mod_cataloglist_xml....
http://www.pukt.nl/tl_files/catalog.jpg
Re: Outputting XML with PHP
Ah -- I think you are using an old version of Contao and/or the Catalog.
I noticed in your screenshot you're seeing the old form layout -- where everything's in one column and there's no green grouping headers.
To be able to do the above, you need to have a separate mod_ template to use for your XML module. It looks like this version does not allow that.
A dirty workaround is to find the ID of your cataloglist module -- you can see it if you hover over the edit icon.
In your mod_cataloglist.tpl file, you'd need an if statement...
Code:
<?php if ($this->id==4): // Assuming ID is 4 ?>
Put the XML code here...
<?php else: ?>
Put the regular template code here...
<?php endif; ?>
Or you can try update Catalog and Contao -- but make sure you backup your files and database first.
[SOLVED] Re: Outputting XML with PHP
Wauw! You are truly an amazing help and a very kind person to help me out so much!
This solved it perfectly!
I tried updating my catalog, but without luck, still no expandable menu's....
Contao version is up to date....
But it works! And slowly I am starting to figure out the if else statements.
Maybe one day I can lend a hand to someone ;-)
Thanks again!!!
Re: [SOLVED] Outputting XML with PHP
No prob. One more thing to figure out and you'll be golden... :)
What's tricky about the Catalog, now that I think about it:
1. It's "Officially" unsupported in Contao 2.9x right now, so it won't show up in the extension catalog.
2. It's technically still in "Beta"
So, if you want to try updating again, go into the extension manager, and click the pencil edit icon for the Catalog. Then make sure everything "Beta" and below is checked.
Then try returning to the extension manager and see if you can install a newer version.