R
E
S
O
U
R
C
E
S
       Home      Products & Services      Contact Us      Links


WebHatchers will design & develop your site for you.
_______________________

Website Menu Heaven: menus, buttons, etc.
_______________________

Send us your questions.
_______________________

site search by freefind
_______________________

HOME
SEO, Google, Privacy
   and Anonymity
Browser Insanity
JavaScript
Popups and Tooltips
Free Website Search
HTML Form Creator
Animation
Buttons and Menus
Counters
Captchas
Image Uploading
CSS and HTML
PHP
AJAX
XPATH
Website Poll
IM and Texting
Databases—MySQL
   or Not MySQL
Personal Status Boards
Content Management
   Systems
Article Content
   Management Systems
Website Directory
   CMS Systems
Photo Gallery CMS
Forum CMS
Blog CMS
Customer Records
   Management CMS
Address Book CMS
Private Messaging CMS
Chat Room CMS
JavaScript Charts
   and Graphs




Free Personal Status Boards (PSB™)

Free Standard Free PSB

Free PSB Pro Version

Free Social PSB

Free Social PSB Plus (with Email)

Free Business PSB

Free Business PSB Plus (with Email)

PSB demo

Social PSB demo

Business PSB demo

So what's all this PSB stuff about?

Chart comparing business status boards

PSB hosting diagram

PSB Licence Agreement



Copyright © 2002 -
MCS Investments, Inc. sitemap

PSBs, social networking, social evolution, microcommunities, personal status boards
PSBs, social networking, business personal status boards
website design, ecommerce solutions
website menus, buttons, image rotators
Ez-Architect, home design software
the magic carpet and the cement wall, children's adventure book
the squirrel valley railroad, model railroad videos, model train dvds
the deep rock railroad, model railroad videos, model train dvds

Get an Array Whose Name is in a Different Array

In this tutorial, you can check out how to use eval() to reference an array after getting its name as an element value of a different array. There are 3 buttons on the page to which the link, below, goes. Click them in order to use the JavaScript function eval() to get array values the hard way. After clicking a button, hit your browser's Back button—and then try the others.

In creating a simulation of accessing a MySQL database via PHP and displaying the contents onscreen, we needed to create arrays that had the data from the MySQL database table. We stored these in a .js file called links.js. This file was loaded in before the page content was shown, as you can see below.

The first button lets us get one value from the array whose name is in the A'th element of the other array "cat[]" referenced by the value c. Okay, there's no such word as A'th! It means that the value of the A variable is a number which tells which element in the other array (not cat[] array) to reference.

We'll give the specifics, in case the printout of the links.js file at the bottom of the page isn't clear enough: The cat[] array contains 10 names of arrays, 'ajax', 'asp', 'css', 'dhtml', 'html', 'javascript', 'mysql', 'php', 'xhtml', and 'xml'. To get the first name, we can use cat[0] or eval('cat['+c+']') if c is 0. To use this value, which is 'ajax', we can just put it directly in the code like this: ajax[5], but this only works if we know we need this array. What if we only know we need the sixth element value for the first array name in the cat[] array? Then we have to get to it with eval(). So, eval(eval('cat['+c+']')+'['+A+']') will get us the c'th array name in cat[] and the A'th element value of that array. And for a loop that loops through the array getting all values, we need the size of the array, which requires this: eval(eval('cat['+c+']')+'.length'). To get ALL array values of ALL arrays, we need to use a loop in a loop, with the c for the cat[] array first and the arrays whose names are in cat[] being the second FOR loop.

Eval(), a JavaScript function, is good for other things beside getting a name from an array and turning it into an array value, or just building array elements from parts, such as pluses and brackets. Elsewhere we show that eval can turn a string into a variable name and we'd be hard pressed to do without it. And we used it as an essential function in fixed positioning as well. And yet the downside of eval is that it creates security issues when used with GET, POST, inputs, databases, and JSON. It's also called a slow function. We find its speed quite adequate. We could have used a 2-dimensional array to deal with the issue we deal with on this page, eliminating the need for eval(). But our way is much more efficient. It simulates the use of a database table with a category field and a url field for each record. Such JavaScript simulations of PHP and MySQL applications can get quite involved. The actual PHP-MySQL application may be a lot simpler!

Try it out: get-array-whose-name-is-in-different-array.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>

<script type="text/javascript" src="links.js"></script>

</head>

<body>
LET'S CHECK OUT HOW TO USE EVAL() TO REFERENCE AN ARRAY AFTER GETTING ITS NAME AS AN ELEMENT OF A DIFFERENT ARRAY: (after clicking a button, hit browser's Back button)<br><br><br>

<SCRIPT LANGUAGE="JavaScript">

var z="";
c=0; //which element of the cat[] array
A=0; /*which element of array whose name is found in the c element of the cat[] array*/

/*get one value from the array whose name is in the Ath element of the other array "cat[]" referenced by the value c*/
function getvalue(c,A){
z=eval(eval('cat['+c+']')+'['+A+']');
document.write("<a HREF='"+z+"' target='_blank'>"+z+"</a><br><br>");} /*make the value a link since it's a url*/

/*get all values from the array whose name is in the element of the other array "cat[]" referenced by the value c*/
function getarray(c){
for (i=0;i<eval(eval('cat['+c+']')+'.length');i++) {
z=eval(eval('cat['+c+']')+'['+i+']');
document.write("<a HREF='"+z+"' target='_blank'>"+z+"</a><br><br>");}}/*make the values links since they're urls*/

/*get all values from all arrays whose names are in the elements of the other array "cat[]" referenced by the value c */
function getallarrays(){
for (c=0;c<cat.length;c++) {document.write("array: "+c+"<br>");
for (i=0;i<eval(eval('cat['+c+']')+'.length');i++) {
z=eval(eval('cat['+c+']')+'['+i+']');
document.write("<a HREF='"+z+"' target='_blank'>"+z+"</a><br>");}}}/*make the values links since they're urls*/

</script>

<button type="button" onclick="c=3,A=4,getvalue(c,A)">Click Me to get value</button><br>
<button type="button" onclick="c=3,getarray(c)">Click Me to get array</button><br>
<button type="button" onclick="getallarrays()">Click Me to get all arrays</button>

</body>
</html>

The links.js file: