Jump to content

Simple XML just outputing values of inner-most elements


jcanker

Recommended Posts

I'm working on a way to let parents in my son's cub scout pack volunteer for specific spots in a duty roster via an AJAX page and MySQL db.

 

The duty roster is saved as an array in the db.  My intent is to pull out the array, parse it into XML to send back to the jQuery which asked for it.

The end result right now is that it just displays a bunch of "available" strings in a row, which is the default value for a job slot which hasn't been filled yet. 

 

The xml should look something like:

<data>
<DAY val="DAYVALUE">		
	<TIMEBLOCK val ="TIMEVALUE">
		<JOB title="titlevalue">
			<slot num="x">AVAILABLE or NAME</slot>
			<slot num="x">AVAILABLE or NAME</slot>
			{...}
		</JOB>
	</timeblock>
</DAY>
<data>

 

Where the attribute changes to reflect the different days for the campout, and the TIMEBLOCK val attribute changes for the different timeblocks which have job slots (Breakfast, Lunch, etc).

 

Here is the snippet of code.  The page starts with Header('Content-type: text/xml');  before making the call to the db (I've left that section out for security).  The part where it gets retrieves and traverses the array are working as expected.  It's where the XML gets built that everything goes wonky.  (But if someone can show me a more recursive, efficient way of getting the same effect, I'm all ears)

 

The output of this page shows up at:http://www.pack238.net/campingregistration/retrievedutyroster.php

You'll see at the bottom where it just says "available available available available available available available available...."

The code is:

//start the xml
$xml = new SimpleXMLElement('<data></data>');

//  get the duty roster array from the database
$query = "select dutyroster from ".$eventsTable." where eventID = 1";
$result = mysql_query($query);
if(!$result)
{
}
else{
//put the returned result into a usable array
$returnedRows = mysql_fetch_array($result);
$day = unserialize($returnedRows[0]);
foreach($day as $key => $value) //this gives us each day
{
	echo "<Br/> Key is: $key && the value is $value";
	$day1 = $xml->addChild('DAY');
	$dayValue = $day1->addAttribute('val',$key);
//Here, we know we're at the top level, so parse out the sub arrays
	foreach($value as $key1 => $value1)//this gives us each time block
	{
		$timeblock = "";
		$timeblockValue = "";
		$timeblock = $day1->addChild('TIMEBLOCK');
		$timeblockValue = $timeblock->addAttribute('val',$key1);
		echo "<br/>  Let's see the time blocks for each day:  $key1   $value1";
	//check to see if there's a JOBS sub array yet again or if it's a standalone
		if(is_array($value1))
		{
			foreach($value1 as $key2 => $value2)
			{
				$job = "";
				$title = "";
				$job = $timeblock->addChild('JOB');
		//		$title = $timeblock->addAttribute('title',$key2); //this is the line throwing the attribute error
				echo "<br/>     We're in a sub array, and the values are:  $key2   $value2";
				if(is_array($value2))
				{
					foreach($value2 as $key3 => $value3)
					{
						//for each slot:
						$slot = $job ->addChild('slot',$value3);
						$slotnum = $slot ->addAttribute('num',$key3);
						echo "<br/>         The jobs are: $key3   $value3"; 
					}
				}
				else //we need to account for individual jobs that don't have a subarray
				{
						$slot = $job ->addChild('slot',$value3);
						$slotnum = $slot -> addAttribute('num','0');
				}
			}
		}
	}
//		echo "<br/><br/>The array returned is: ".$usableArray."<br/>";
//		$usableArray = unserialize($usableArray);
//		print_r($usableArray);
}//end of the main foreach
//we got the duty roster array....now just parse out the days, time periods, and jobs then create the xml


$dom = dom_import_simplexml($xml)->ownerDocument; 
$dom->formatOutput = true; 
echo $dom->saveXML(); 

echo "<hr/>";
//print_r($xml);

}//end of else to if(!result)



Link to comment
Share on other sites

OK, nevermind, I guess....

The echo statements and the HR threw a "only one top level error" message when I followed the link I posted...

But even if I click refresh on the page I had been using, it still doesn't show properly....same browser, different tabs...hrm....

 

Anywhoo....

Is there a more recursive way to step through the levels of the array and process it?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.