Welcome to WebmasterWorld Guest from 23.20.137.66

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

OOP - problem adding data to array in my class

     
4:17 pm on Aug 1, 2007 (gmt 0)

5+ Year Member



Created a new class for retrieving a website navigation from a database which looks like this

class CMStree extends DbConnector{

var $fullNav; // Compile full navigation to return once all data obtained

function ShowPageTree($parent, $level, $item = 0, $theUL){

// Check if we're opening a new sub menu

if (!$parent = $theUL){$this->fullNav[] .= '<ul class="topNav">'; }

// retrieve all children of $parent

$getNav = DbConnector::query("SELECT `ID`, `NAME`, `PARENT`, `EDIT`, `TIER_POS` FROM rcms_pages WHERE `PARENT` = '".$parent."' ORDER BY `TIER_POS` ASC");

// New link actions

while ($gotNav = mysql_fetch_array($getNav)) {


$UL = $parent;

$item ++;


// Does the page have children

$getChild = DbConnector::query("SELECT * FROM rcms_pages WHERE `PARENT` = '".$gotNav['ID']."'");


// Construct the menu item

$link .= '<li>';

if(DbConnector::fetchArray($getChild)){ $link .= '<a title="Expand/Collapse" href="javascript:toggleLayer(\''.$item.'\');"><img src="img/nav-expand-on.gif" width="14" height="14" alt="Expand/Collapse" class="navExpandImg" /></a>' ;}
[code]else{ $link .= '<img src="img/nav-expand-off.gif" width="14" height="14" class="navExpandImg" />' ;}

$link .= '<div class="Fleft">'.$gotNav['NAME'].'</div><img src="img/nav-edit-off.gif" width="14" height="14" alt="Edit Page" class="navIcons" /><img src="img/nav-delete-off.gif" width="14" height="14" alt="Delete Page" class="navIcons" /><div class="navModule"></div><div class="clear"></div></li>'."\n\n";


HERE
// add this item to the navigation

>>>>
$this->fullNav[] .= $link;


// empty current link incase of sub links

$link = "";


// Display any children to this menu item

$this->ShowPageTree($gotNav['ID'], $level+1, $item, $parent);

}


if (!$parent = $theUL){$this->fullNav[] .= '</ul>';}

// echo complete navigation

return implode($this->fullNav);


}

}

The problem is that my new link data is not being added to the $fullNav array, the instruction to pass the link to the array is marked up above (HERE>>>>)

All the gets returned is
<ul class="topNav"></ul>

I know that the link is being constructed ok as I've tried just returning this on its own, and to double check even if I change the instruction mentioned to

// add this item to the navication

$this->fullNav[] .= 'HELLO CAN YOU SEE ME';

I still only get
<ul class="topNav"></ul>

I'm quite new to OOP and functions in general so apologies for any shocking code here

4:51 pm on Aug 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$this->fullNav[] .= $link;

Looks like you're using string techniques while working with arrays.
* If you want to concatenate something to an existing string: use

$string .= $piece;

* If you want to add an item to an array: use
$array[] = $item;

if (!$parent = $theUL)

You probably mean

if (!$parent == $theUL)
6:31 pm on Aug 1, 2007 (gmt 0)

5+ Year Member



thanks that's it!

However..... hehe

now the instruction to empty $link doesn't appear to be working as my menu looks like...

Menu 1
Menu 1
Menu 2
Menu 1
Menu 2
Menu 3

and so on - any suggestions on a better way to clear this?

7:35 am on Aug 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



// Construct the menu item 
$link .= '<li>';

You're using the concatenation operator (.=) where you don't want to concatenate anything. Simply changing that line to

$link = '<li>';

might solve your problem.

9:58 am on Aug 2, 2007 (gmt 0)

5+ Year Member



it did indeed thankyou again

I'm currently looking at

function ShowPageTree($parent, $level, $item, $theUL){

// Check if we're opening a new menu or sub menu

if ($theUL == 'new'){ $fullNav .= '<ul class="topNav">OPEN UL'."\n"; }

if (!$parent == $theUL){ $fullNav .= '<li id="subnav'.$item.'" style="border:none;"><ul class="expandingNav">OPEN SUB UL'."\n"; }

// retrieve all children of $parent

$getNav = DbConnector::query("SELECT `ID`, `NAME`, `PARENT`, `EDIT`, `TIER_POS` FROM rcms_pages WHERE `PARENT` = '".$parent."' ORDER BY `TIER_POS` ASC;");

// begin child actions

while ($gotNav = DbConnector::fetchArray($getNav)) {

// These will be important for opening and closing sub nav UL STILL NEEDS ATTENTION!
 $UL = $parent;

$item ++;

// Does the page have children
$getChild = DbConnector::query("SELECT * FROM rcms_pages WHERE `PARENT` = '".$gotNav['ID']."';");

// Construct the menu item

$link = '<li>';

if(DbConnector::fetchArray($getChild)){ $link .= '<a title="Expand/Collapse" href="javascript:toggleLayer(\''.$item.'\');"><img src="img/nav-expand-on.gif" width="14" height="14" alt="Expand/Collapse" class="navExpandImg" /></a>' ;}

else{ $link .= '<img src="img/nav-expand-off.gif" width="14" height="14" class="navExpandImg" />' ;}

$link .= '<div class="Fleft">'.$gotNav['NAME'].'</div>';

$link .= '<a title="Edit Page" href="editpage.php?page='.$gotNav['ID'].'"><img src="img/nav-edit-on.gif" width="14" height="14" alt="Edit Page" class="navIcons" /></a>';

$link .= '<a title="Delete Page" href="'.$_SERVER['PHP_SELF'].'?delete='.$gotNav['ID'].'"><img src="img/nav-delete-on.gif" width="14" height="14" alt="Delete Page" class="navIcons" /></a>';

$link .= '<div class="navModule"></div>';

$link .= '<div class="clear"></div></li>'."\n";

// add this item to the navigation

$fullNav .= $link;

// Check what's going on

$fullNav .= '[ID:'.$gotNav['ID'].'//tier:'.$level.'//item:'.$item.'//tiername:'.$UL.']';

// Display any children to this menu item

$this->ShowPageTree($gotNav['ID'],$level+1,$item,$UL);

} 

// clost the current UL if all items are retrieved STILL NEEDS ATTENTION!

if ($parent == $theUL){$fullNav .= '</ul>CLOSE'."\n";}

//if (!$parent == $theUL){$fullNav .= '</ul>CLOSE'."\n";}

// return complete navigation

return $fullNav; 

}

My output looks like this:
OPEN UL
(expand button off) Robo Page 3 (other buttons with correct IDs)
[ID:3//tier:0//item:1//tiername:nav]

(expand button off) Robo Page 2(other buttons with correct IDs)
[ID:2//tier:0//item:2//tiername:nav]

(expand button on) Robo Page 1(other buttons with correct IDs)
[ID:1//tier:0//item:3//tiername:nav]
----------------------------------------

which means that the recursive part of the script isn't working correctly as Robo Page 1 should have 2 children

I know they can be picked up ok because if I call the function like this:
$PageTree->ShowPageTree(1,0,0,'new')
they list correctly

I think i'm very nearly there - I've probably been looking at this for too long so I'm beginning to overlook silly mistakes.

5:28 pm on Aug 2, 2007 (gmt 0)

5+ Year Member



CLOSED- figured it out
 

Featured Threads

Hot Threads This Week

Hot Threads This Month