|
|
 |
Re: FN-FORUM: PHP/XML code issue
date posted 27th April 2007 16:15
If $name_array is an array, the $url_array will *always* be and array,
with an equal number of elements.
Each array will contain the name/url of the current node, and the
name/url of each parent node back to root level.
- deanoj
Dave Fennell wrote:
>
>
>
> Deano J wrote:
>> Hi all
>>
>> Can someone offer some advice on the following?
>>
>> When viewing my site very occasionally PHP gives me the following
>> notices...
>>
>> ============
>> Notice: Undefined offset: 0 in /var/www/dj2/classes/nav.class.php on
>> line 87
>> Notice: Undefined offset: 1 in /var/www/dj2/classes/nav.class.php on
>> line 87
>> Notice: Undefined offset: 2 in /var/www/dj2/classes/nav.class.php on
>> line 87
>> ...
>> ============ continues up to xml total entries
>>
>> The code this references is...
>> ============
>> public function breadcrumbs($node) {
>> $name_array =
>> $this->xml->xpath("//siteMapNode[name='$node']/ancestor::*/name");
>> $url_array =
>> $this->xml->xpath("//siteMapNode[name='$node']/ancestor::*/url");
>> if ($node == "Home") {
>> $this->output = "> ";
>> } else {
>> $this->output = "Home >\n";
>> }
>> if (is_array($name_array)) {
>> foreach($name_array as $key=>$value) {
>> **87** $link = $url_array[$key];
>> $link = str_replace("~","",$link);
>> $this->output .= "$value > \n";
>> }
>> } else {
>> $this->output .= "";
>> }
>> $this->output .= "$node \n\n";
>> return $this->output;
>> }
>> =============
>> Normally I have no problems and the code works as expected giving a nice
>> formatted
>>
>> Home > Link1 > Link2
>>
>> but say every 1 in 20 refreshes I get the php notice messages, and the
>> navigation links go nuts showing every link in the file regardless of
>> structure. (about 25)
>>
>> Even stranger when I look at the source in firefox this is not shown. In
>> fact none of the links are in the source apart from the first static
>> one!!!
>>
>> I have not yet managed to mimic this behavior in internet explorer.
>>
>> The error seems to be indicating some kind of XPATH failure to pass the
>> correct values to the 'name' and 'url' arrays. Anyone have any clue why?
>>
>> - thanks - deanoj
>>
>>
>>
> In the code:
>
> if (is_array($name_array)) {
>
> foreach($name_array as $key=>$value) {
> **87** $link = $url_array[$key];
> $link = str_replace("~","",$link);
> $this->output .= "$value > \n";
> }
> } else {
> $this->output .= "";
> }
>
> You only check that name_array is an array, but in the loop you access
> "url_array" assuming it too is an array and has the same number of
> elements as "name_array".
>
> An obvious fix would be :
>
> if (is_array($name_array) && is_array($url_array)) {
>
>
> That would remove the error, but are you saying that they should
> *always* be the same?
>
> Dave
>
>
|
 |
|