SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry regexp to search arrays

    i am new to regular expressions and need help.

    all the code i have seen in tutorials only shows regexp application to one set of data from a form field or coded directly into the example.

    however, i am trying to perform a search of array values. i have created an array of my company's product part numbers with associated description and price for each. i have a simple, one text box form where users can enter a complete or partial part number. i want to check each part number position in the array and return (in a table) all possible matches along with the other information from the array.

    part numbers are made up of numbers and letters and i would like to return possible matches regardless of where the input value is located in the length of the array value.

    so far i have this:
    Code:
    ===========================================================
    <SCRIPT language="javascript">
    <!--
    var partnumber= new Array();
    partnumber[0] = new Array("PN","Description","price");
    
    partnumber[1] = new Array("part001","description001","price001");
    partnumber[2] = new Array("part002","description002","price002");
    
    
    function checkDatabase(){
    
    var param=document.form.pn.value;
    var insertedHTML="";
    var count="0";
    
    if (param == "")
    {
    	insertedHTML = "<p><font face=Verdana size=2 color=red><b>Please Enter a Partial or Complete Part 
    
    Number</b></font>";
    
    document.getElementById('results').innerHTML = insertedHTML;
    return false;
    }
    else
    {
    var insertedHTML="<FONT face=Arial size=-1>•&nbsp;<A HREF=\"#\" onClick=\"javascript:window.print()\">Print This 
    
    Page</A></FONT><BR><TABLE border><TR><TD>part number</TD><TD>description</TD><TD>price</TD></TR>";
    	for (i=1;i<partnumber.length;i++)
    	{
    	if (param==partnumber[i][0])
    	{
    count="1";
    	insertedHTML += 
    
    "<TR><TD>"+partnumber[i][0]+"</TD><TD>"+partnumber[i][1]+"</TD><TD>"+partnumber[i][2]+"</TD></TR>";
    }
    }
    	insertedHTML += "</TABLE>";
    
    if (count>0) {
    
    document.getElementById('results').innerHTML = insertedHTML;
    return false;
    }
    else {
    
    document.getElementById('results').innerHTML = "No Entries Contained Your Search<BR>";
    return false;
    }
    }
    
    }
    -->
    </SCRIPT>
    
    <FORM name=form onsubmit="return checkDatabase();">
    		<INPUT type=text name="pn" onChange="javascript:this.value=this.value.toUpperCase();"><INPUT type=submit 
    
    value=Search>
    	      </FORM>
    <P>
    <DIV id="results"></DIV>
    ===========================================================
    unfortuantely i haven't been able to digest and apply regexp theory to apply to this situation. i would appreciate any help.

    thanks for your time.

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the code here is a compiliation of two scripts I've got in my personal code library I'm pretty sure it does what you need and then some...

    http://www.lanwizards.com/test3.htm

    Just view the source to see what's going on. It's pretty functional, but there's alot more that could be done with it too.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks for the help

    awesome script. i don't understand it all yet, but i'm working on it.

    actually i have already been trying to add an options column to the results table for links to spec sheets, handtools to use with our products, web pages specific to the part number, etc.

    i outline below what i tried, but haven't had luck.

    can you take a stab at it??

    **i expanded this.dataNames to include "Options".
    **i added option1 and option2 to function dataItem(pn, desc, price) and added this.option1 and this.option2 as variables below that
    **i added "option1","option2" to each this.list[i++]

    not every part will have all options so i tried to create a loop to check if each option field is occupied.

    something like:

    if this.list[i++][3]
    document.write ^^a href=this.list[i++][3]^^Specs ^^/a^^ ^^BR^^

    if this.list[i++][4]
    document.write ^^a href=this.list[i++][4]^^Find Suggested Tools ^^/a^^ ^^BR^^


    etc.

    my problem right now is i can't decipher the script you gave me enough to make this happen. my initial attempts resulted in each option being printed in its own data cell (so there was an extra cell protruding past the header row).

    if it is helpful to know, i usually use the url (like specs.pdf) for the option field value so i can just stick it in the appropriate spot in the script as outlined above.

    thanks for your time.

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I've updated the script. Just re-check the link. I'm not sure what you are doing with these options, so I just output them as HTML (using a <br> in between). Not sure if that's what you want....

    Sorry if it's over your head, but OO is definitely the best way to handle this stuff
    Last edited by beetle; Nov 27, 2002 at 09:15.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    dead on!!

    you nailed it!!

    there are slight variations i want with the options, but i can handle it from here.

    thanks for the info.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •