...

View Full Version : Need a variable before it's set



johnnyb
09-24-2004, 07:09 AM
Hi,

I've made this function to create my menu and it works great except for one thing - I need the $opener variable to be set before it is, and I can't really switch it around.

I think it is possible to do what I want by replacing the echo statements by a string variable which appends to itself, but I haven't been able to make that work properly - mostly because I'm not that great working with functions and variable scope.

So, here's the code:



function make_menu( $level,$CNX,$page ){
$theString = NULL;
$opener = NULL;
$toPrint = NULL;
$after = NULL;
$subjectQuery = "SELECT page.PAGE_TITLE, page.PAGE_ID, page.PAGE_FILE_NAME, page.NEXT_PAGE, page.PARENT_PAGE FROM page WHERE PARENT_PAGE = ".$level." ORDER BY PAGE_TITLE";
$subjectResult = mysql_query($subjectQuery,$CNX) or die(mysql_error());
while($subjectRow = mysql_fetch_assoc($subjectResult)){
//check if there are children
$checkQuery = "SELECT page.PAGE_TITLE, page.PAGE_ID, page.PAGE_FILE_NAME, page.NEXT_PAGE, page.PARENT_PAGE FROM page WHERE PARENT_PAGE = ".$subjectRow["PAGE_ID"]." ORDER BY PAGE_TITLE";
$checkResult = mysql_query($checkQuery,$CNX) or die(mysql_error());
if( mysql_num_rows($checkResult) > 0 ){
//make_opener($level,$CNX,$page,&$opener);
echo '<li class="';
echo $opener; // this is where I need to have the variable already set
echo '"><a href="#" class="icon" onclick="return door(this);"></a>'.'<a href="'.$subjectRow["PAGE_FILE_NAME"].'" class="parent">'.$subjectRow["PAGE_TITLE"]."</a>\n";
echo '<ul class="item">'."\n";
make_menu($subjectRow["PAGE_ID"], $CNX, $page, &$opener);
} else {
// $opener shoule be set in this if...else statement
if ($subjectRow["PAGE_ID"] == $page) {
echo '<li><a href="'.$subjectRow["PAGE_FILE_NAME"].'" class="parent">'.$subjectRow["PAGE_TITLE"]."</a>";
} else {
echo '<li><a href="'.$subjectRow["PAGE_FILE_NAME"].'">'.$subjectRow["PAGE_TITLE"]."</a>";
}

}

echo "</li>"."\n";

//reset child checking query and result to nothing
mysql_free_result($checkResult);
$checkQuery = NULL;
} // end the while loop


echo "</ul>\n";
//echo $theString;
$theString = NULL;
return ;
mysql_free_result($subjectResult);
} // end make_menu function

function menu_maker($level,$CNX,$page) {

echo '<ul id="menuTree">'."\n";
if ($page == 53) {
echo '<li class="announceheading">Encyclopedia:</li>'."\n";
} elseif ($page == 10) {
echo '<li class="announceheading">Studio to Stage:</li>'."\n";
}

$theString=NULL;
make_menu($level,$CNX,$page);



} // end menu_maker function

dniwebdesign
09-24-2004, 07:45 AM
So what you're saying is that you need the $opener variable set before the menu is created. If so what do you need it set to?

marek_mar
09-24-2004, 08:07 AM
How about adding


isset($opener) ? $opener : 'Default value';

where it is needed?

johnnyb
09-24-2004, 05:16 PM
$opener is needed on the line that says "echo $opener;" the default value would be "closed"

However, $opener would be set to "open" if the if condition "if ($subjectRow["PAGE_ID"] == $page) { " 6 lines later evaluates to true.

Really, what I need to do is evaluate that if condition before I echo $opener, however this would print my menu elements in the wrong order.

What $opener does in this expanding menu is determine if the page the menu is on is in a certain part of the menu tree, and if it is then the menu tree is opened to show the page title, (which is also bolded).

marek_mar
09-24-2004, 05:57 PM
Well how do you imagine this? The thing you want to do is can be compared to wanting to get on a bus before it arrives.
Moreover the part where trhe opener is neede will never execute if the if query is to be evaluaterd becouse they are in different if/else blocks.
You should copy over the if and set it there.
It can look like this: (if the if makes sense in that block)

$opener = ($subjectRow["PAGE_ID"] == $page) ? 'open' : 'closed';

or the if/else version:


if($subjectRow["PAGE_ID"] == $page)
{
$opener = 'open';
}
else
{
$opener = 'closed';
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum