Thanks for the reply, this is just not making sense. Here is my code that works all except the part at the bottom about getting the contents from the XML file:
PHP Code:
<?php
//gets current url
$url = $_SERVER['REQUEST_URI'];
// Removes Application root from URL
if(preg_match(EBOOK_ROOT_REGEX, $url))
{
$url = str_replace(EBOOK_ROOT.DS,'',$url);
}
else if (preg_match(ADD_CART_ROOT_REGEX, $url))
{
$url = str_replace(ADD_CART_ROOT.'?id=','',$url);
$n = $url;
$n = substr($n, 2); // gets id of book
$url = substr($url, 0, 2); // gets category of book
/*
$book_id = substr($url, 2);
$url = str_replace($book_id,'',$url); // assigns $url the two letter code
$book_id = substr($book_id, 1); // gets book name and id
$book_char = strrpos($book_id, '/', -1); // gets position of '/' in $book_id
$book_name = $book_id; // copy value
$book_id = substr($book_id, $book_char + 1); // assigns $book_id the id of book
$book_name = substr($book_name, 0, $book_char); // assigns $book_name name of book
*/
}
// new variable $controller declared
$ebook_home = 'ebook_library';
// $view refers to pages or actually sections of pages to fill in the main body of the page
// The functioning code is found in views/layout/photos.php
switch ($url) {
case "mk":
$layout = MARKETING_XML;
break;
case "bs":
$layout = BUSINESS_XML;
break;
case "eb":
$layout = CREATE_EBOOKS_XML;
break;
default:
//$layout = $url;
}
echo '$url: '.$url.'<br />';
echo '$layout: '.$layout.'<br />';
$xml_file = new SimpleXMLElement(file_get_contents($layout));
$child = $xml_file->children();
$book_id = $child[$n]->id;
echo 'ebook_select: '.$book_id;
$book_name = $child[$n]->title;
?>
The last block of code does not work at all in this file.
Further down the page I included a file which lays out the xml data with html and little snippets of PHP for dynamic data: (this file works perfectly)
PHP Code:
<?php
// file gets included into ebooks.php
?>
<!-- ebook shopping cart menu -->
<div class="ebook_cart_menu">
<a href="#">view cart</a>
<a href="#">clear cart</a>
</div>
<?php
// new variable layout declared
if (isset($layout)) {
//include_once($_SERVER['DOCUMENT_ROOT'].ROOT.'/'.$layout.'.php');
$xml_file = new SimpleXMLElement(file_get_contents($layout));
$child = $xml_file->children();
//$id = $xml_file->ebook->attributes();
//print_r($child);
if(phpversion() >= '5.3.0')
{
$count = $xml_file->count();
}
else
{
$doc = new DOMDocument();
$str = $xml_file->asXML();
$doc->loadXML($str);
$count = $doc->getElementsByTagName("ebook")->length;
}
//print_r($xml_file);
echo '<table border="1"><tr>';
$i = 0;
while($i <= $count)
{
echo '<td class="ebook_cell">';
echo '<img src="'.$child[$i]->bookCover.'" height="150" width="150"/><br />'.
'<h2 class="ebook_title">'.$child[$i]->title.'</h2>'.'<br />'.
'<div class="text">'.$child[$i]->description.'</div>'.'<br />'.
'<div class="ebook_price">'.$child[$i]->price.'</div>'.'<br />'.
'<a href="'.ROOT.'/add_to_cart.php?id='.$child[$i]->url.'" class="ebook_add_to_cart">Add to cart</a>';
echo '</td>';
$i++;
echo '<td class="ebook_cell">';
echo '<img class="ebook_cover" src="'.$child[$i]->bookCover.'" height="150" width="150"/><br />'.
'<h2 class="ebook_title">'.$child[$i]->title.'</h2>'.'<br />'.
'<div class="text">'.$child[$i]->description.'</div>'.'<br />'.
'<div class="ebook_price">'.$child[$i]->price.'</div>'.'<br />'.
'<a href="'.ROOT.'add_to_cart.php?id='.$child[$i]->url.'" class="ebook_add_to_cart">Add to cart</a>';
echo '</td></tr><tr>';
$i++;
}
echo "</tr></table>";
} else {
include_once($_SERVER['DOCUMENT_ROOT'].TEMPLATES.'/ebook_library.php');
}
?>
As you can see they both use the exact same initialization of the simpleXMLElement() function. Both functions use the same exact variable for a parameter ($layout) and $layout is holding the correct values the entire way through as well.
Could my problem lie in the file paths based on where each file came from that was called in correlation to the file to my XML file?
my top file has a path: functions/ebook_select.php
my second file (which works) included here is: templates/show_ebooks.php
Both of these above files are ones called into main page files which are accessed via menu navigation.
The third place I try this XML call which doesnt work is placed directly into a main page file. I did a print_r($child[5]->title) and this returned nothing in my direct call to a main page or root file.
I am trying to identify a pattern, but not seeing anything.
Thanks for the help