January 5, 201115 yr Hi all This is my first post in this forum and i am new to php with web designing background. The thing is I want to get this type of output using xml and php, please help me <html> <head></head> <body> <table> <tr> <td>Heading</td> <td>Heading</td> </tr> <tr> <td>function name</td> <td>Shortcut key</td> </tr> </table> I can structure in html but have a large content that i want to show in two columns, so i want to make an xml file and read the data and display in browser. Thanks in advance...
January 5, 201115 yr So you have an html 'template' and you want to loop through an XML file and fill in the function name and shortcut key columns?
January 6, 201115 yr Author So you have an html 'template' and you want to loop through an XML file and fill in the function name and shortcut key columns? Thanks Jock for your response You got it correct and can you tell me how to do this with php. I hope this is possible with simplexml in php but can't figure out.
January 6, 201115 yr It depends really on what your XML schema looks like, here a simple example <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Keyboard shortcuts</title> </head> <body> <?php $xmlstring = <<<XML <?xml version = "1.0" encoding="UTF-8"?> <document> <shortcut> <function>Copy</function> <keystroke>Ctrl + C</keystroke> </shortcut> <shortcut> <function>Cut</function> <keystroke>Ctrl + X</keystroke> </shortcut> <shortcut> <function>Paste</function> <keystroke>Ctrl + V</keystroke> </shortcut> </document> XML; try { $xmli = new SimpleXMLIterator($xmlstring); echo "<table>"; echo "\n\t<caption>List of keyboard shortcuts in Microsoft Excel 97</caption>"; echo "\n\t<thead>"; echo "\n\t\t<tr>"; echo "\n\t\t\t<th scope=\"col\">Function</th>"; echo "\n\t\t\t<th scope=\"col\">Keystrokes</th>"; echo "\n\t\t</tr>"; echo "\n\t</thead>"; echo "\n\t<tbody>"; foreach ( $xmli as $node => $v ) { echo "\n\t\t<tr>"; echo "\n\t\t\t<td>".$v->function."</td>"; echo "\n\t\t\t<td>".$v->keystroke."</td>"; echo "\n\t\t</tr>"; } echo "\n\t</tbody>"; echo "\n</table>"; } catch(Exception $e) { echo $e->getMessage(); } ?> </body> </html> In practice you'd probably want to load the xml data from external files, in which case you'd use http://www.php.net/manual/en/function.simplexml-load-file.php
January 6, 201115 yr Author It depends really on what your XML schema looks like, here a simple example <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Keyboard shortcuts</title> </head> <body> <?php $xmlstring = <<<XML <?xml version = "1.0" encoding="UTF-8"?> <document> <shortcut> <function>Copy</function> <keystroke>Ctrl + C</keystroke> </shortcut> <shortcut> <function>Cut</function> <keystroke>Ctrl + X</keystroke> </shortcut> <shortcut> <function>Paste</function> <keystroke>Ctrl + V</keystroke> </shortcut> </document> XML; try { $xmli = new SimpleXMLIterator($xmlstring); echo "<table>"; echo "\n\t<caption>List of keyboard shortcuts in Microsoft Excel 97</caption>"; echo "\n\t<thead>"; echo "\n\t\t<tr>"; echo "\n\t\t\t<th scope=\"col\">Function</th>"; echo "\n\t\t\t<th scope=\"col\">Keystrokes</th>"; echo "\n\t\t</tr>"; echo "\n\t</thead>"; echo "\n\t<tbody>"; foreach ( $xmli as $node => $v ) { echo "\n\t\t<tr>"; echo "\n\t\t\t<td>".$v->function."</td>"; echo "\n\t\t\t<td>".$v->keystroke."</td>"; echo "\n\t\t</tr>"; } echo "\n\t</tbody>"; echo "\n</table>"; } catch(Exception $e) { echo $e->getMessage(); } ?> </body> </html> In practice you'd probably want to load the xml data from external files, in which case you'd use http://www.php.net/manual/en/function.simplexml-load-file.php Hi Jock good evening, You provided the xml structure exactly the same way i want. xml will be external because my brother will add the content to it and he don't know even html. xml will be easy to him to add content. Your code is working fine. I made an xml file and load it with simplexml_load_file, working fine in local system, I will upload it and check it online. Thanks again,,,
January 6, 201115 yr No problem, if you want to use xml files with that code, just change the $xmli line of code, e.g $xmli = simplexml_load_file('my-file.xml', 'SimpleXMLIterator');
January 25, 201115 yr Author No problem, if you want to use xml files with that code, just change the $xmli line of code, e.g $xmli = simplexml_load_file('my-file.xml', 'SimpleXMLIterator'); Hi Jock, Good afternoon. due to hard disk failure I was unable to access my system for a long time, now every thing is working fine. I applied your code and everything is going right now and I put it on the server also. I am really amazed for your quick responses thanks again for helping me
Create an account or sign in to comment