PDA

View Full Version : Printing This Chunk Here, This Chunk There, and This One Over There.


Carl
11-11-2002, 06:31 AM
How do I go about printing one section here and the other section there. Its hard to explain, but if I want the code to print out everything in chunks, if they match. So if array1 has the following values.

menu1
menu1
menu2

It would print it out like this.

menu1 menu1


menu2.

My problem though is the values in array1 could be anything. Follow me? I could change the values of array1 to

booboobahbah
hehehe
joe
hi
hehehe
hi
booboobahbah
booboobahbah

Then it would print it out like this.

booboobahbah booboobahbah booboobahbah

hehehe hehehe

joe

hi hi.

See the picture?

Its hard to explain.

My code.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>CMenu</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script>
/////////////////////////////////////////
// Copywrited 2002 By Carl Vondrick. //
// //
// You may use this script as long as //
// this copywrite notice is here. //
// //
// All rights reserved. //
// //
// Please report all bugs to //
// carl@carlsoft.net //
/////////////////////////////////////////

var ctop=88; // How many pixils from the top margin.
var cleft=96; // How many pixils from the left margin.
var height=22; // The height of the menu.
var bgcolor="blue"; // The background of your menu.
var bordercolor="black"; // The border color of your menu.
var borderwidth=1; // The width of the border.
var textcolor="white"; // The font color.

var mainmenu=new Array();
mainmenu[0]="Home | getURL=http://www.carlsoft.net"
mainmenu[1]="WebXpertz | getURL=http://www.webxpertz.net/forums/index.php3?s="
mainmenu[2]="Google! | getURL=http://www.google.com"
mainmenu[3]="Scripts | showMenu=scripts"
mainmenu[4]="Fun and Games | showMenu=games"

submenus=new Array();
submenus[0]="Javascript Kit | getURL=http://www.javascriptkit.com | fromMenu=scripts"
submenus[1]="Dynamic Drive | getURL=http://dynamicdrive.com | fromMenu=scripts"
submenus[2]="MSN Zone | getURL=http://www.zone.com | fromMenu=games"

//////////////////////////////////////////
//// NO NEED TO EDIT BELOW THIS LINE /////
//////////////////////////////////////////

// Print Main Menu \\
var thref,tmouseover,text,action;
document.write('<div id="menu" style="position: absolute; top:'+ctop+'; left:'+cleft+'; height:'+height+';">');
document.write('<table cellspacing=0 cellpadding=4 border='+borderwidth+' bordercolor='+bordercolor+'><tr>');

for (i=0; i<mainmenu.length; i++) {
menuarray=mainmenu[i].split(" | ");
if (menuarray[1].substr(0,6)=="getURL") {
action=menuarray[1].replace("getURL=","");
thref=action;
tmouseover="return false;";
}
else if (menuarray[1].substr(0,8)=="showMenu") {
action=menuarray[1].replace("showMenu=","");
tmouseover="showMenu('"+action+"');";
thref="javascript:void(0);";
}
text=menuarray[0];
document.write("<td bgcolor="+bgcolor+" onmouseover="+tmouseover+" onmouseout=hidemenu();><div id=menu"+i+"><b><a href='"+thref+"' style='color:"+textcolor+"; text-decoration:none;'>"+text+"</a></b></div></td>");
}

document.write('</tr></table>');
document.write('</div>');

// Print Sub-Menus \\

subtext=new Array(submenus.length);
subthref=new Array(submenus.length);
subfromMenu=new Array(submenus.length);

for (j=0;j<submenus.length;j++) {
submenuarray=submenus[j].split(" | ");
subtext[j]=submenuarray[0];
subthref[j]=submenuarray[1].replace("getURL=","");
subfromMenu[j]=submenuarray[2].replace("fromMenu=","");
}

place=height+5;
document.write("<div id=submenus style='top:"+place+";'>");

// this part there, that part here, the other part over there.

document.write("</div>");

function showMenu(which) {

}

function hidemenu() {

}
</script>
</body>
</html>


Let me know if you need if I didn't explain it well.

glenngv
11-11-2002, 07:31 AM
you can just sort the array

arrayName.sort();

once sorted, as you loop through the elements, you just compare the current element with the previous element to know when to display another set of elements in another line

Carl
11-11-2002, 04:44 PM
Yes, but that doesn't help solve my problem. For an if statement to work, I need two values. I can my first value from subFromMenu but where the second?

Carl
11-11-2002, 05:52 PM
Got a lead, but still doesn't work. Anyone?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>CMenu</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script>
/////////////////////////////////////////
// Copywrited 2002 By Carl Vondrick. //
// //
// You may use this script as long as //
// this copywrite notice is here. //
// //
// All rights reserved. //
// //
// Please report all bugs to //
// carl@carlsoft.net //
/////////////////////////////////////////

var ctop=88; // How many pixils from the top margin.
var cleft=96; // How many pixils from the left margin.
var height=22; // The height of the menu.
var bgcolor="blue"; // The background of your menu.
var bordercolor="black"; // The border color of your menu.
var borderwidth=1; // The width of the border.
var textcolor="white"; // The font color.

var mainmenu=new Array();
mainmenu[0]="Home | getURL=http://www.carlsoft.net"
mainmenu[1]="WebXpertz | getURL=http://www.webxpertz.net/forums/index.php3?s="
mainmenu[2]="Google! | getURL=http://www.google.com"
mainmenu[3]="Scripts | showMenu=scripts"
mainmenu[4]="Fun and Games | showMenu=games"

submenus=new Array();
submenus[0]="Javascript Kit | getURL=http://www.javascriptkit.com | fromMenu=scripts"
submenus[1]="Dynamic Drive | getURL=http://dynamicdrive.com | fromMenu=scripts"
submenus[2]="MSN Zone | getURL=http://www.zone.com | fromMenu=games"

//////////////////////////////////////////
//// NO NEED TO EDIT BELOW THIS LINE /////
//////////////////////////////////////////

// Print Main Menu \\
var thref,tmouseover,text,action;
document.write('<div id="menu" style="position: absolute; top:'+ctop+'; left:'+cleft+'; height:'+height+';">');
document.write('<table cellspacing=0 cellpadding=4 border='+borderwidth+' bordercolor='+bordercolor+'><tr>');

for (i=0; i<mainmenu.length; i++) {
menuarray=mainmenu[i].split(" | ");
if (menuarray[1].substr(0,6)=="getURL") {
action=menuarray[1].replace("getURL=","");
thref=action;
tmouseover="return false;";
}
else if (menuarray[1].substr(0,8)=="showMenu") {
action=menuarray[1].replace("showMenu=","");
tmouseover="showMenu('"+action+"');";
thref="javascript:void(0);";
}
text=menuarray[0];
document.write("<td bgcolor="+bgcolor+" onmouseover="+tmouseover+" onmouseout=hidemenu();><div id=menu"+i+"><b><a href='"+thref+"' style='color:"+textcolor+"; text-decoration:none;'>"+text+"</a></b></div></td>");
}

document.write('</tr></table>');
document.write('</div>');

// Print Sub-Menus \\

subtext=new Array(submenus.length);
subthref=new Array(submenus.length);
subfromMenu=new Array(submenus.length);

for (j=0;j<submenus.length;j++) {
submenuarray=submenus[j].split(" | ");
subtext[j]=submenuarray[0];
subthref[j]=submenuarray[1].replace("getURL=","");
subfromMenu[j]=submenuarray[2].replace("fromMenu=","");
}

place=height+10+ctop+(borderwidth*1);
document.write("<div id=submenus style='position:absolute; top:"+place+"; left:"+cleft+"'>");

var z=0;
var k=0;

document.write('<table cellspacing=0 cellpadding=4 border='+borderwidth+' bordercolor='+bordercolor+'>');

for (k=0;k<subfromMenu.length;k++) {
if (subfromMenu[k]==subfromMenu[z]) {
document.write("<div id=sub"+subfromMenu[k]+" style='visibility:hidden;'>");
document.write("<tr>");
document.write("<td bgcolor="+bgcolor+"><a href='"+subthref[k]+"' style='color:"+textcolor+"; text-decoration:none;'>"+subtext[k]+"</a></td>");
document.write("</tr>");
document.write("</div>");

subtext.splice(k,1);
subthref.splice(k,1);
subfromMenu.splice(k,1);
}
else if (subfromMenu[k]!=subfromMenu[z]) {
z++;
}
}

document.write("</table");
document.write("</div>");

function showMenu(which) {

}

function hidemenu() {

}
</script>
</body>
</html>

glenngv
11-12-2002, 01:09 AM
can you please highlight the changes or better just post that particular code?

whammy
11-12-2002, 01:16 AM
I'd agree... posting the changes you've made with "That doesn't work... anyone?" isn't going to get you very far...

Carl
11-12-2002, 11:07 PM
Sorry, no one knows the answers and I've gotten much more help on other forums :)

Anyways, heres what I have now.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>CMenu</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script>
/////////////////////////////////////////
// Copywrited 2002 By Carl Vondrick. //
// //
// You may use this script as long as //
// this copywrite notice is here. //
// //
// All rights reserved. //
// //
// Please report all bugs to //
// carl@carlsoft.net //
/////////////////////////////////////////

var ctop=88; // How many pixils from the top margin.
var cleft=96; // How many pixils from the left margin.
var height=22; // The height of the menu.
var bgcolor="blue"; // The background of your menu.
var bordercolor="black"; // The border color of your menu.
var borderwidth=1; // The width of the border.
var textcolor="white"; // The font color.

var mainmenu=new Array();
mainmenu[0]="Home | getURL=http://www.carlsoft.net"
mainmenu[1]="WebXpertz | getURL=http://www.webxpertz.net/forums/index.php3?s="
mainmenu[2]="Google! | getURL=http://www.google.com"
mainmenu[3]="Scripts | showMenu=scripts"
mainmenu[4]="Fun and Games | showMenu=games"

submenus=new Array();
submenus[0]="Javascript Kit | getURL=http://www.javascriptkit.com | fromMenu=scripts"
submenus[1]="Dynamic Drive | getURL=http://dynamicdrive.com | fromMenu=scripts"
submenus[2]="MSN Zone | getURL=http://www.zone.com | fromMenu=games"
submenus[3]="joe | getURL=joe.com | fromMenu=games"
submenus[4]="hi | getURL=hi.com | fromMenu=games"

//////////////////////////////////////////
//// NO NEED TO EDIT BELOW THIS LINE /////
//////////////////////////////////////////

// Print Main Menu \\
var thref,tmouseover,text,action;
document.write('<div id="menu" style="position: absolute; top:'+ctop+'; left:'+cleft+'; height:'+height+';">');
document.write('<table cellspacing=0 cellpadding=4 border='+borderwidth+' bordercolor='+bordercolor+'><tr>');

for (i=0; i<mainmenu.length; i++) {
menuarray=mainmenu[i].split(" | ");
if (menuarray[1].substr(0,6)=="getURL") {
action=menuarray[1].replace("getURL=","");
thref=action;
tmouseover="return false;";
}
else if (menuarray[1].substr(0,8)=="showMenu") {
action=menuarray[1].replace("showMenu=","");
tmouseover="showMenu('"+action+"');";
thref="javascript:void(0);";
}
text=menuarray[0];
document.write("<td bgcolor="+bgcolor+" onmouseover="+tmouseover+" onmouseout=hidemenu();><div id=menu"+i+"><b><a href='"+thref+"' style='color:"+textcolor+"; text-decoration:none;'>"+text+"</a></b></div></td>");
}

document.write('</tr></table>');
document.write('</div>');

// Print Sub-Menus \\

subtext=new Array(submenus.length);
subthref=new Array(submenus.length);
subfromMenu=new Array(submenus.length);

for (j=0;j<submenus.length;j++) {
submenuarray=submenus[j].split(" | ");
subtext[j]=submenuarray[0];
subthref[j]=submenuarray[1].replace("getURL=","");
subfromMenu[j]=submenuarray[2].replace("fromMenu=","");
}

place=height+10+ctop+(borderwidth*1);
document.write("<div id=submenus style='position:absolute; top:"+place+"; left:"+cleft+"'>");

var z=0;
var k=0;

document.write('<table cellspacing=0 cellpadding=4 border='+borderwidth+' bordercolor='+bordercolor+'>');

for (k=0;k<subfromMenu.length;k++) {
if (subfromMenu[k]==subfromMenu[z]) {
document.write("<div id=sub"+subfromMenu[k]+k+" style='visibility:hidden;'>");
document.write("<tr>");
document.write("<td bgcolor="+bgcolor+"><a href='"+subthref[k]+"' style='color:"+textcolor+"; text-decoration:none;'>"+subtext[k]+"</a></td>");
document.write("</tr>");
document.write("</div>");

subtext.splice(k,1);
subthref.splice(k,1);
subfromMenu.splice(k,1);
}
else {
document.write("</table><br>")
document.write('<table cellspacing=0 cellpadding=4 border='+borderwidth+' bordercolor='+bordercolor+'>');
z++;
k--;
}
}

document.write("</table");
document.write("</div>");

function showMenu(which) {

}

function hidemenu() {

}
</script>
</body>
</html>

The problem is it only prints the even values in the arrays, basically every other array value.

glenngv
11-13-2002, 01:02 AM
i'm just guessing here, coz you didn't highlight the code in question.

try changing these lines:

z++;
k--;

to:

z = k;

is this what you want?

Carl
11-13-2002, 02:34 AM
No, heres what it does.

z++, moves to next checking if the condition is false.
k--, removes one from the for loop.

z=k wouldn't do what I want. Just create an infinant loop.

glenngv
11-13-2002, 03:16 AM
no, z=k would not make an infinite loop!

it's bad programming practice to change the counter variable in a for loop like this:

for (k=0;k<subfromMenu.length;k++) {
//codes here
k--;
}

i have modified the code.
hope this is what you want :)

var z=0;
var k=0;

document.write('<table cellspacing=0 cellpadding=4 border='+borderwidth+' bordercolor='+bordercolor+'>');

for (k=0;k<subfromMenu.length;k++) {
if (k==0) {
document.write("<div id=sub"+subfromMenu[k]+k+" style='visibility:hidden;'>");
document.write("<tr>");
document.write("<td bgcolor="+bgcolor+"><a href='"+subthref[k]+"' style='color:"+textcolor+"; text-decoration:none;'>"+subtext[k]+"</a>");
}
else if (subfromMenu[k]==subfromMenu[z]){
document.write("&nbsp;&nbsp;|&nbsp;&nbsp;<a href='"+subthref[k]+"' style='color:"+textcolor+"; text-decoration:none;'>"+subtext[k]+"</a>");
}
else {
document.write("</td></tr>");
document.write("</div>");

document.write("<div id=sub"+subfromMenu[k]+k+" style='visibility:hidden;'>");
document.write("<tr>");
document.write("<td bgcolor="+bgcolor+"><a href='"+subthref[k]+"' style='color:"+textcolor+"; text-decoration:none;'>"+subtext[k]+"</a>");
z=k;
}
}

document.write("</table");
document.write("</div>");


it will produce:

Javascript Kit | Dynamic Drive
MSN Zone | joe | hi

which grouped the scripts and games category

Carl
11-13-2002, 05:00 AM
Ahh I see, I did k=z. Wrong way. :rolleyes:

Yes that did it. Now just for a few formatting and it should be done.

Thanks :) :thumbsup: