Click to See Complete Forum and Search --> : Trying to shorten code...


friendshipping
11-15-2003, 12:27 PM
This is the short version of what I'm trying to do... I'm trying to generate menu buttons for pages that members have built and saved to the database. The database values below would show either a 1 (button should be created) or a 0 (button should not be created).

This script works:

<script language="JavaScript">
<!--
var but0 = (database value of either 1 or 0) // page 1
var but1 = (database value of either 1 or 0) // page 2
var but2 = (database value of either 1 or 0) // page 3
var but3 = (database value of either 1 or 0) // page 4
var but4 = (database value of either 1 or 0) // page 5

if(but0 == 1){
document.write("<div><a href=\"#\">Page 1</a></div>")
}
if(but1 == 1){
document.write("<div><a href=\"#\">Page 2</a></div>")
}
if(but2 == 1){
document.write("<div><a href=\"#\">Page 3</a></div>")
}
if(but3 == 1){
document.write("<div><a href=\"#\">Page 4</a></div>")
}
if(but4 == 1){
document.write("<div><a href=\"#\">Page 5</a></div>")
}
//-->
</script>

My question is this... How can I shorten the code so that I can use only one variable - example:

<script language="JavaScript"><!--
var but0 = 1,1,0,0,1 // These numbers are just examples of database values

if(but0 == 1){
document.write("<div><a href=\"#\">Page 1</a></div>" + "<div><a href=\"#\">Page 1</a></div>" + "<div><a href=\"#\">Page 1</a></div>" + "<div><a href=\"#\">Page 1</a></div>" + "<div><a href=\"#\">Page 1</a></div>")
}
//--></script>

I would appreciate any help you could offer. Thank you in advance :)

Jona
11-15-2003, 12:34 PM
<script type="text/javascript"><!--
var butns = new Array(1,0,0,1,1);

for(i=0; i<butns.length; i++){
&nbsp;if(butns[i] == 0){
&nbsp;&nbsp;document.write("<div><a href=\"#\">Page "+(i+1)+"</a></div>")
&nbsp;}
}
//--></script>


[J]ona

friendshipping
11-17-2003, 11:13 PM
I'm sorry, I must have miscommunicated what I'm trying to do...

Each page is different with a different name and a different URL. I should have stated that in my original question.

Perhaps this will better explain what I'm trying to do:

<script language="JavaScript"><!--
var but0 = 1,1,0,0,1 // These numbers are just examples of database values

if(but0 == 1){
document.write("<div><a href=\"myself.html\">Myself</a></div>" + "<div><a href=\"friends.html\">My Friends</a></div>" + "<div><a href=\"family.html\">My Family</a></div>" + "<div><a href=\"pets.html\">My Pets</a></div>" + "<div><a href=\"faith.html\">My Faith</a></div>")
}
//--></script>

So, can you help me to write the remaining code?

<script type="text/javascript"><!--
var butns = new Array(1,0,0,1,1);

Thank you in advance ;-)

Jona
11-17-2003, 11:15 PM
Actually, I don't understand what you want.

[J]ona

friendshipping
11-17-2003, 11:22 PM
Sorry Jonah, Here's the actual script as it is written right now. I'm trying to shorten up the code:

<script language="JavaScript"><!--
var but0 = 1 // My Home
var but1 = &FTAG_MYSELF_VIEW& // Myself
var but2 = &FTAG_LOVE_VIEW& // My Love
var but3 = &FTAG_CHILDREN_VIEW& // My Children
var but4 = &FTAG_BUSINESS_VIEW& // My Parents
var but5 = &FTAG_SIBLINGS_VIEW& // My Siblings
var but6 = &FTAG_RELATIVES_VIEW& // My Relatives
var but7 = &FTAG_FRIENDS_VIEW& // My Friends
var but8 = &FTAG_PETS_VIEW& // My Pets
var but9 = &FTAG_FAITH_VIEW& // My Faith
var but10 = &FTAG_JOURNAL_VIEW& // My Journal
var but11 = &FTAG_THOUGHTS_VIEW& // My Thoughts
var but12 = &FTAG_INTERESTS_VIEW& // My Interests
var but13 = &FTAG_FAVORITES_VIEW& // My Favorites
var but14 = &FTAG_LIKES_VIEW& // My Likes & Dislikes
var but15 = &FTAG_LINKS_VIEW& // My Links
var but16 = &FTAG_BUSINESS_VIEW& // My Business
var but17 = &FTAG_SERVICE_VIEW& // My Service
var but18 = &FTAG_EDUCATION_VIEW& // My Education
var but19 = &FTAG_EXPERIENCE_VIEW& // My Experiences
var but20 = &FTAG_TEST_VIEW& // My Test
var but21 = &FTAG_CONTACT_VIEW& // My Contact
var but22 = 1 // My Message

if(but0 == 1){
document.write("<div><a href=\"!SCRIPT_URL!?page=welcome&sess_id=!SESS_ID!\">My Home</a></div>")
}
if(but1 == 1){
document.write("<div><a href=\"!SCRIPT_URL!?page=myself&sess_id=!SESS_ID!\">Myself</a></div>")
}
if(but2 == 1){
document.write("<div><a href=\"!SCRIPT_URL!?page=love&sess_id=!SESS_ID!\">My Love</a></div>")
}
if(but3 == 1){
document.write("<div><a href=\"!SCRIPT_URL!?page=children&sess_id=!SESS_ID!\">My Children</a></div>")
}
if(but4 == 1){
document.write("<div><a href=\"!SCRIPT_URL!?page=parents&sess_id=!SESS_ID!\">My Parents</a></div>")
}
if(but5 == 1){
document.write("<div><a href=\"!SCRIPT_URL!?page=siblings&sess_id=!SESS_ID!\">My Siblings</a></div>")
}
//-->
</script>

Up above, I'm pulling the actual values off of the database so please ignore the &FTAG's. (These tags are the values (0 or 1) that tell the script whether or not to create that menu button)

Is there a way to shorten this code... I would like to know because the code will only get longer as I add pages.

Jona
11-17-2003, 11:26 PM
I'd store them in an array, and then use a for() loop... :p Just use an if/else statement inside of the for() loop.

Or you could get the actual values from the database, and set them up to equal their values instead of ones or zeros, and then just use "+value+" in your document.write()'s.

[J]ona

friendshipping
11-17-2003, 11:30 PM
I appreciate your help very much Jonah, but I must be honest with you... I don't know crap about javascript. I simply followed a tutorial site to get as far as I did with the script I just sent you. If you have the time, could you please type out the actual code (just a sample of what the code should look like and I'll do the rest of the buttons)... if you don't mind??? Thank you in advance ;-)

Jona
11-17-2003, 11:38 PM
var butn = new Array("<-- VALUE gotten from database, children for our example -->","<-- VALUE gotten from database, something else such as family would be returned here");

for(i=0; i<butn.length; i++){
&nbsp; document.write("<a href=\"page.pl?page="+butn[i]+"\">"+butn[i]+"</a>");
}


I'm hoping you can get both the values and the actual numbers from the database. If you can do that, we can try using associative arrays rather than numerical ones. Or if you can't, we can set up two different arrays--one with the values and the other with the ones and zeros--and loop through them and print out the right results. Which would be something like this...


var names = [0,1,0,0,1];
var values = ["children","family","house","dog"];

for(i=0; i<names.length; i++){
&nbsp; if(names[i] == 1){
&nbsp; &nbsp;document.write("<a href=\"page.pl?page="+values[i]+"\">"+values[i]+"</a>");
&nbsp; }
}


[J]ona

friendshipping
11-17-2003, 11:41 PM
Thanks alot Jonah... yer alright :D

Jona
11-17-2003, 11:42 PM
Originally posted by friendshipping
Thanks alot Jona... yer alright :D

It's Jona. ;) I'm assuming I've solved your problem and you can take it from here. Am I correct?

[J]ona

friendshipping
11-17-2003, 11:51 PM
Ummm... actually, I can't do it the way you just suggested. Sorry. The only value I am pulling up from the database for the menu is the numbers 0,1. You see, its a self-replicating website and each member is allowed to replicate whatever pages they choose. The &FTAG's are the answers to the question, "Would you like to show this page link in the menu? Yes=1 and No=0.

Does that make better sense to you?

ps: btw, I appreciate your patience with me... I understand how difficult it can be to work with folks who don't have much of a clue (like myself in this case ;-)

Jona
11-17-2003, 11:53 PM
The only way I know you could do that is if you have both the values of them and the ones and zeros. Otherwise you have to write out the whole thing...

[J]ona

friendshipping
11-18-2003, 12:11 AM
Hey Jonah, Thanks for your help anyway. I've only been a member to this site for two days and your quick replies have impressed the heck out of me. As you can tell, I'm just learning all of this stuff, but with people like you willing to help others with your understanding of it all, I know I'm in pretty good hands. Thanks again & God bless :D

Jona
11-18-2003, 12:20 AM
No problem, I'm glad I could be of... well, whatever help I've been. :)

[J]ona