PHP5 Dom XML dynamic Table Generation

Given the following XML Doc

<Consumer>
<Programs>
<Program>
<Title>30 Year</Title>
<author>Juan Rodriguez</author>
<publisher>abc</publisher>
</Program>
<Program>
<Title>20 Year</Title>
<author>Albert Rodriguez</author>
<publisher>abc</publisher>
</Program>
<Program>
<Title>10 Year</Title>
<author>Jose Guzman</author>
<publisher>xyz</publisher>
</Program>

How could I construct a table like the following:

Title | Author |

ABC Publishing |
30 Year | Juan Rodriguez |
20 Year | Albert Rodriguez |

XYZ Publishing
10 Year | Jose Guzman |


Currently I can build a dynamic table using the following but I can't figure
out how to only print out the publisher once as a heading for all the
products that match it, and then when a new publisher is detected print a
new header and output all matching published products beneath that.

$file = 'results.xml';

// Loading the results into a new DomDocument
$dom1 = new DOMDocument('1.0', 'ISO-8859-1');
$dom1->load($file, LIBXML_NOBLANKS);

<?php
echo("<TR>");
echo("<TH></TH>");
echo("<TH>Title</TH>");
echo("<TH>Author</TH>");
echo("<TH>Publisher</TH>");

$root = $dom1->documentElement;

$title = $dom1->getElementsByTagName("Title");
$author = $dom1->getElementsByTagName("Author");
$publisher = $dom1->getElementsByTagName("Publisher");

$length = $title->length;
for ($x=0; $x < $length; $x++) {


echo "<TR><TD>".$title->item($x)->nodeValue."</TD>";
echo "<TD>".$author->item($x)->nodeValue."</TD>";
echo "<TD>".$publisher->item($x)->nodeValue."</TD></TR>";

The above code actually outputs

Title | Author | Publisher
Chuy [ Do, 08 Februar 2007 20:15 ] [ ID #1623116 ]

Re: PHP5 Dom XML dynamic Table Generation

| Currently I can build a dynamic table using the following but I can't
figure
| out how to only print out the publisher once as a heading for all the
| products that match it, and then when a new publisher is detected print a
| new header and output all matching published products beneath that.
|
| $file = 'results.xml';
|
| // Loading the results into a new DomDocument
| $dom1 = new DOMDocument('1.0', 'ISO-8859-1');
| $dom1->load($file, LIBXML_NOBLANKS);
|
| <?php
| echo("<TR>");
| echo("<TH></TH>");
| echo("<TH>Title</TH>");
| echo("<TH>Author</TH>");
| echo("<TH>Publisher</TH>");
|
| $root = $dom1->documentElement;
|
| $title = $dom1->getElementsByTagName("Title");
| $author = $dom1->getElementsByTagName("Author");
| $publisher = $dom1->getElementsByTagName("Publisher");

// translate the xml into something more standard
// such as a database array of records from a query
$records = array();
for ($i = 0; $i < $title->length; $i++)
{
$records[$i]['TITLE'] = $title->item($i)->nodeValue;
$records[$i]['AUTHOR'] = $author->item($i)->nodeValue;
$records[$i]['PUBLISHER'] = $publisher->item($i)->nodeValue;
}
// now, this is how you can group by *any* of these fields
// but as you requested, this is by publisher
$group = array();
foreach ($records as $record)
{
$group[$record['PUBLISHER'][] = $record;
}
// now output the results
foreach ($group as $publisher => $records)
{
// echo out your header here for each publisher
foreach ($records as $record)
{
// echo out this publisher's author's works
}
}

hth
Steve [ Do, 08 Februar 2007 20:38 ] [ ID #1623117 ]

Re: PHP5 Dom XML dynamic Table Generation

Thanks for the assistance and I think I see where this is going but I can't
seem to get past the part where you are attempting to group the newly
created array as seen below

$group = array();
foreach ($records as $record)
{
$group[$record['PUBLISHER'][] = $record;
}

I keep getting the following output in my browser when attempting this.

Parse error: syntax error, unexpected ';', expecting ']' in
/usr/local/www/sweep/results/tables.php on line 43

"Steve" <no.one [at] example.com> wrote in message
news:jZKyh.43$Sy7.31 [at] newsfe06.lga...
>| Currently I can build a dynamic table using the following but I can't
> figure
> | out how to only print out the publisher once as a heading for all the
> | products that match it, and then when a new publisher is detected print
> a
> | new header and output all matching published products beneath that.
> |
> | $file = 'results.xml';
> |
> | // Loading the results into a new DomDocument
> | $dom1 = new DOMDocument('1.0', 'ISO-8859-1');
> | $dom1->load($file, LIBXML_NOBLANKS);
> |
> | <?php
> | echo("<TR>");
> | echo("<TH></TH>");
> | echo("<TH>Title</TH>");
> | echo("<TH>Author</TH>");
> | echo("<TH>Publisher</TH>");
> |
> | $root = $dom1->documentElement;
> |
> | $title = $dom1->getElementsByTagName("Title");
> | $author = $dom1->getElementsByTagName("Author");
> | $publisher = $dom1->getElementsByTagName("Publisher");
>
> // translate the xml into something more standard
> // such as a database array of records from a query
> $records = array();
> for ($i = 0; $i < $title->length; $i++)
> {
> $records[$i]['TITLE'] = $title->item($i)->nodeValue;
> $records[$i]['AUTHOR'] = $author->item($i)->nodeValue;
> $records[$i]['PUBLISHER'] = $publisher->item($i)->nodeValue;
> }
> // now, this is how you can group by *any* of these fields
> // but as you requested, this is by publisher
> $group = array();
> foreach ($records as $record)
> {
> $group[$record['PUBLISHER'][] = $record;
> }
> // now output the results
> foreach ($group as $publisher => $records)
> {
> // echo out your header here for each publisher
> foreach ($records as $record)
> {
> // echo out this publisher's author's works
> }
> }
>
> hth
>
>
Chuy [ Fr, 09 Februar 2007 19:27 ] [ ID #1624467 ]

Re: PHP5 Dom XML dynamic Table Generation

> $group = array();
> foreach ($records as $record)
> {
> $group[$record['PUBLISHER'][] = $record;
> }
>
> I keep getting the following output in my browser when attempting this.
>
> Parse error: syntax error, unexpected ';', expecting ']' in
> /usr/local/www/sweep/results/tables.php on line 43

The error message is pretty precise here. you have 1 more opeing [ than
closing ]. Maybe the line should be:-

$group[$record]['PUBLISHER'][] = $record;
Peter [ Fr, 09 Februar 2007 20:07 ] [ ID #1624468 ]

Re: PHP5 Dom XML dynamic Table Generation

"peter" <submit [at] flexiwebhost.com> wrote in message
news:eqigpq$gc7$1 [at] aioe.org...
|> $group = array();
| > foreach ($records as $record)
| > {
| > $group[$record['PUBLISHER'][] = $record;
| > }
| >
| > I keep getting the following output in my browser when attempting this.
| >
| > Parse error: syntax error, unexpected ';', expecting ']' in
| > /usr/local/www/sweep/results/tables.php on line 43
|
| The error message is pretty precise here. you have 1 more opeing [ than
| closing ]. Maybe the line should be:-
|
| $group[$record]['PUBLISHER'][] = $record;

NOPE...that won't work. it should be:

$group[$record['PUBLISHER]][] = $record;
Steve [ Fr, 09 Februar 2007 21:34 ] [ ID #1624473 ]
PHP » alt.php » PHP5 Dom XML dynamic Table Generation

Vorheriges Thema: looking for volunteer to help with website
Nächstes Thema: how to grab info from other websites?