Jump to content

Creating a hierarchical XML document


delux247

Recommended Posts

Hello all,

 

I am trying to create a hierarchical XML document, I have gotten most of it done, but I need some help with a few more things. What the XML needs to look like is this:

 

<types>

<group name="Identity + Print">
	<type name="Identity System" selected="1" mode="0">
		<aspect id="" />
		<aspect id="" />
	</type>
	<type name="Stationery System" selected="0" mode="0">
		<type name="Production" selected="1" mode="1" >
			<aspect id="" />
		</type>
		<aspect id="" />
		<aspect id="" />
	</type>
	<type name="Print Collateral" selected="0" mode="0">
		<type name="Content" selected="0" mode="1">
			<aspect id="" />
			<aspect id="" />
		</type>
		<type name="Production" selected="1" mode="1">
			<aspect id="" />
		</type>
	</type>
</group>

<group name="Interactive Media">
	<type name="Print Collateral" selected="0" mode="0">
		<type name="Content" selected="0" mode="1">
			<aspect id="" />
			<aspect id="" />
		</type>
		<type name="Production" selected="1" mode="1">
			<aspect id="" />
		</type>
	</type>
</group>

<group name="Motion Graphics">
<type name="Print Collateral" selected="0" mode="0">
		<type name="Content" selected="0" mode="1">
			<aspect id="" />
			<aspect id="" />
		</type>
		<type name="Production" selected="1" mode="1">
			<aspect id="" />
		</type>
	</type>
</group>

</types>

 

and here is the PHP:

<?php
header("Content-Type: application/xml");
echo "<types>";
require_once('php/crud.php');
require_once('php/dBug.php');
ini_set("display_errors","2");
ERROR_REPORTING(E_ALL);

$CRUD = new CRUD('types');
$CRUD->addWhere('type_id = 0');
$rows = $CRUD->read();

// get array of items with parent #0
$groups = array();
foreach($rows as $row)	
{
	$groups[] = $row;
}
//new dbug($groups);
foreach ($groups as $group) 
{
    $SCRUD = new CRUD('types');
$SCRUD->addWhere('type_id = '.$group['id']);
$data = $SCRUD->read();

$FCRUD = new CRUD('types');
$FCRUD->addWhere('type_id = '.$data[0]['id']);
$fdata = $FCRUD->read();

// get array of items with parent $group[iD]
$types = array();
foreach($data as $type)	
{
	$types[] = $type;
}

$types2 = array();
foreach($fdata as $ftype)
{
	$types2[] = $ftype;
}

$types = array_merge($types, $types2);

    if (!$types) {
        // write empty <group />
	echo "<group />";
        //continue;
    }

    // write opening <group>
echo "<group name='".$group['name']."'>";
    foreach ($types as $type) 
{
        	$aspects = array(); // get array of items with parent $type[iD]
	    $TCRUD = new CRUD('aspects_types');
		$TCRUD->addWhere('type_id = '.$type['id']);
		$adata = $TCRUD->read();
		$aspects = array();
		foreach($adata as $aspect)	
		{
			$aspects[] = $aspect;
		}

	//new dbug($aspects);
        if (!$aspects) 
	{
            // write empty <type />
		//echo "<type />";
          //  continue;
        }

        // write opening <type>
	$child_group = ($row['name'] == "radio") ? (0) : (1); 
	echo "<type name='".$type['name']."' id='".$type['id']."' selected='".$type['default_selected']."' mode='".$child_group."'>";
        foreach ($aspects as $aspect) {
           echo "<aspect id='".$aspect['aspect_id']."'></aspect>";
        }
        // write closing </type>
	echo "</type>";

    }
    // write closing </group>
echo "</group>";
}
// write closing </types>
echo "</types>";
?>

 

I have two tables that I'm working with. The first table is types, it looks like this:

 

id :::::::::::: name ::::::::::::: type_id

1 :::::::::: Identity + print::::: 0

2 :::::::::: Interactive ::::::::: 0

3 :::::::::: Motion ::::::::::::: 0

4 :::::::::: Identity ::::::::::: 1

5 :::::::::: Stationery :::::::: 1

6 :::::::::: Print Coll :::::::::: 1

7 :::::::::: Production :::::::: 5

8 :::::::::: Content ::::::::::: 6

9 :::::::::: Production :::::::: 6

etc...

 

I also have a table called aspects_types that looks like this

aspect_id ::::::: type_id

1 :::::::::::::::::4

2 :::::::::::::::::5

2 :::::::::::::::::6

etc...

 

There are 3 records with type_id = 0 these are the groups, in each group there are types, types can have aspects and more types within them. My problem is that I can not get the types that are within higher level types to display. I am at a total loss right now and really need some help. If you need anymore info just ask and I'll get it to you. Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/165841-creating-a-hierarchical-xml-document/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.