...

View Full Version : Writing out a searched array



CrazyCoder
12-30-2006, 01:58 AM
Okay, here we go. I am building a JavaScript site search [must-be client side]. The code works very well except that it only writes out the last item returned from the search array not ALL the items returned in the search.

The magic happens [or not] at the win.innerHTML = searchopen + line + searchclose;

I need to get the 'line' variable to write out all the items returned not just the last one.

Help!

The array is huge so I left it off to save space.



searchopen = "<table border='0' cellspacing='2' cellpadding='5' width='100%' bgcolor='#f0f8ff' style='border:1px solid #000066;'><tr><td><h3 style='margin:0px; padding:0px;'>Search Results:</h3></td><td align='right' valign='top'>[<a href='javascript:void(0);' onclick='document.getElementById(\"searchDiv\").innerHTML=\"\"' class='copyright'><u>close search</u></a>]";

function search(frm){
//win = window.open("","search","scrollbars,width=500,height=500,left=10,top=10");
var win = document.getElementById("searchDiv");
frm = document.searchform.srchval.value;
txt = frm.split (http://php.net/split)(" ");
fnd = new Array(); total=0;
for (i = 0; i < item.length; i++){
fnd[i] = 0; order = new Array(0, 4, 2, 3);
for (j = 0; j < order.length; j++)
for (k = 0; k < txt.length; k++)

if (item[i][order[j]].toLowerCase().indexOf(txt[k]) > -1 && txt[k] != "")
fnd[i] += (j+1);
}
for (i = 0; i < fnd.length; i++){
n = 0; w = -1;
for (j = 0;j < fnd.length; j++)
if (fnd[j] > n){
n = fnd[j];
w = j;
};
if (w > -1) total += show(w, win, n);
fnd[w] = 0;
}
searchclose = "<tr><td align='right' colspan='2'>Total found: " + total + "<br></td></tr></table>";
win.innerHTML = searchopen + line + searchclose;
}

function show(which,wind,num) {
link = item[which][1] + item[which][0];
line = "<tr><td colspan='2'><b><a href='" + link + "' style='text-decoration:underline;' target='parent'>" + item[which][2] + "</a></b> &nbsp; &nbsp; Score: " + num + "<br>";
line += item[which][3] + "</td></tr>";
//alert(result);
return 1;
}

document.body.appendChild(win);

david_kw
12-30-2006, 06:44 PM
Just glancing at it I suggest formally making line a global variable and setting it to an empty string. Then on assignment to line make it '+=' instead of just '='. My take on your code is it overwrites line every time you call show().



var line = "";

function show(which,wind,num) {
link = item[which][1] + item[which][0];
line += "<tr><td colspan='2'><b><a href='" + link + "' style='text-decoration:underline;' target='parent'>" + item[which][2] + "</a></b> &nbsp; &nbsp; Score: " + num + "<br>";
line += item[which][3] + "</td></tr>";
//alert(result);
return 1;
}


dvaid_kw

CrazyCoder
01-02-2007, 11:04 PM
:thumbsup: Let it be known that david_kw has the eyes of an eagle and generally ROCKS!

Thanks a bunch. Even though it was a fairly simple fix I would have never have gotten that. I was stumbling down the wrong path entirely.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum