PDA

View Full Version : If statements being skipped in script


elboberto
10-23-2004, 07:48 PM
Here is my problem: I am using php to get a querystring from the url sent to my page, so it's something like index.php?menu=16. The menu integer is storing a bitwise value so that I can break down what menus should be expanded when they go to that page.

As you can see from my constants, a value of 1 means main menu is expanded, 2 means chop menu is expanded. A value of 3 would mean main AND chop menus are expanded.

The IF statements in the initMenuFlags function are my problem. No matter what combination of menus should be expanded, only the FIRST IF statement is being executed. For example, menu passes a value of 12. That means tutorial and beat menus are expanded. Only the IF statement for tutVal is being executed and therefore only that menu is being expanded. The script is completely ignoring the IF statement for beatVal.

What can I do to remedy this so that all IF statements are being evaluated? For those who aren't aware of what I am doing, imagine the menu value being in binary and being 5 bits. Originally it is 00000. Each bit acts as a toggle for a menu. Main menu on would be 00001. Tutorial menu on would be 00100. My if statements are checking if my menuflags variable contains the toggle for each menu.

Thanks for any help, its much appreciated!


<?php if (isset($_GET['menu'])) $menuFlags=$_GET['menu']; else $menuFlags=0; ?>

<script language="javascript1.3">
const mainVal=1;
const chopVal=2;
const tutVal=4;
const beatVal=8;
const linkVal=16;

function redirectURL(myURL) {
//alert("menuflags " + menuFlags);
myURL = myURL + "&menu=" + menuFlags;
window.location=myURL;
}
function initMenuFlags() {
menuFlags=<?php echo $menuFlags ?>;
if(menuFlags & mainVal)
neonixToggle('subMain','mnuMain','',false,'Main', 0);
if(menuFlags & chopVal)
neonixToggle('subChopShop','mnuChopShop','',false,'Chop Shop', 0);
if(menuFlags & tutVal)
neonixToggle('subTutorial','mnuTutorials','',false,'Tutorials', 0);
if(menuFlags & beatVal)
neonixToggle('subBeats','mnuBeats','',false,'Beats', 0);
if(menuFlags & linkVal)
neonixToggle('subLinks','mnuLinks','',false,'Links', 0);

}

function neonixToggle(itemID,linkID,anchorID,tSwitch,tText, menuValue){// Neonix Toggle Ver 3.0.1 - www.neonix.net
if (document.getElementById && navigator.userAgent.indexOf('Opera') == -1){
var itemEL = document.getElementById(itemID);
var linkEL = document.getElementById(linkID);
itemEL.className = itemEL.className == 'TG_visible' ? 'TG_hidden' : 'TG_visible';
if(itemEL.className=='TG_visible') menuFlags+=menuValue;
else menuFlags-= menuValue;
if(!eval(tSwitch)){linkEL.innerHTML = itemEL.className == 'TG_hidden' ? '> ' + tText : '< ' + tText;}
//alert("mv. " + menuValue + " mf. " + menuFlags);
}
if (anchorID.length != 0){;
document.location.href = '#' + anchorID;
}
return;
}
</script>

Basscyst
10-23-2004, 08:04 PM
Welcome to CF:

Gotta double up your &'s.


if(menuFlags && mainVal) // for each statement


Basscyst

elboberto
10-23-2004, 08:13 PM
No, I'm using bitwise operators. Different than regular &&.

elboberto
10-23-2004, 08:21 PM
Ok, I found out something odd. If I put an alert statement between each if statement, everything executes perfectly. Is there a way I can simulate a pause similar to the alert so that my code works?

joh6nn
10-23-2004, 09:39 PM
um. you need a comparison in there, man. otherwise, those if-thens translate to the following:

if (1) { ... }
if (2) { ... }
if (4) { ... }
if (8) { ... }
etc

shouldn't that be something like this:

if (menuFlags = mainVal) { ... }

?

dumpfi
10-24-2004, 12:10 AM
You don't need a comparison. Everything other than "", 0, false and null translates to true.

Back to topic:

Maybe your function neonixToggle() throws an error. I can't think of anything else because something like this works:
<html>
<head>
<script type='text/javascript'>
const mainVal=1;
const chopVal=2;
const tutVal=4;
const beatVal=8;
const linkVal=16;
function initMenuFlags() {
menuFlags=31;
div = document.getElementById('vals');
if(menuFlags & mainVal) div.innerHTML += mainVal + ' ';
if(menuFlags & chopVal) div.innerHTML += chopVal + ' ';
if(menuFlags & tutVal) div.innerHTML += tutVal + ' ';
if(menuFlags & beatVal) div.innerHTML += beatVal + ' ';
if(menuFlags & linkVal) div.innerHTML += linkVal + ' ';
}
window.onload = initMenuFlags;
</script>
</head>
<body>
<div id='vals'></div>
</body>
</html>dumpfi

joh6nn
10-24-2004, 02:17 AM
ok, i missed that menuflags was being set dynamicly by the php. my mistake.


i'm looking at this neonix function, as was suggest by dumpfi, and i'm noticing that you've got an eval() in there, that seems unnecessary. as i recall, eval() can really slow the execution of a function down. i have no idea whether that'll help (reads: probably won't help at all), but it's worth a shot.

try changing this line:

if(!eval(tSwitch)){linkEL.innerHTML = itemEL.className == 'TG_hidden' ? '> ' + tText : '< ' + tText;}

to this:

if(!tSwitch){linkEL.innerHTML = itemEL.className == 'TG_hidden' ? '> ' + tText : '< ' + tText;}