homepage Welcome to WebmasterWorld Guest from 23.22.179.210
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
OOP - problem adding data to array in my class
el_roboto




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

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

 

RonPK




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

$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)

el_roboto




msg:3410786
 6:31 pm on Aug 1, 2007 (gmt 0)

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?

RonPK




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

// 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.

el_roboto




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

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.

el_roboto




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

CLOSED- figured it out

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved