SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: match question

  1. #1
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    match question

    hi!
    i have this script and i want figure out how to get the properties of the matched variable which is into an array!
    hth
    Code:
    function mathOp(numt) {
    var mathop = new Array()
    <?php // start php to get array from db
    $ret = mysql_query("SELECT * FROM table ORDER BY id ASC");
    $count = mysql_num_rows($ret);
    for($i = 0; $i < $count; ++$i) {
    $row = mysql_fetch_row($ret);
    echo "mathop[".$i."] = ".$row[1];
    }
    mysql_free_result($ret);
    ?> // end php
    if(document.getElementById(numt).innerHTML == mathop[i]) {
    // show the properties of the matched variable from the array above...
    }
    i hope im clear....
    how to show the property from the above variable if it is matched?
    any suggestion?
    thanx in advance!!!

  2. #2
    SitePoint Member
    Join Date
    Oct 2004
    Location
    Bangalore
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest you to name your DIVs as div0, div1, div2....
    Pass n as a variable to the mathop function,
    "div"+n will give you the div and you can match this div's innerHTM with mathop[n].

    HTML Code:
    var mathop = new Array() 
    /*
    ... php to populate mathop array
    */
    
    function mathOp(n) { 
    	if( document.getElementById("div"+n).innerHTML == mathop[n]) { 
    		alert( n+": Matched");
    	}
    }
    Also
    1) keep the mathod array declaration and initialization outside the function, so that the array will not be created and destroyed every time the function is invoded.

    2) it might be better to use innerText instead of innerHTML, so that you can avoid any html tags inside your div. (innerText is supported by IE)

    - Kiran Makam

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much!
    u helped me a lot!!!
    cheers

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    another question....
    can i use this...
    Code:
    var x = "/+%/";
    couse it seems that wont work!!!
    thanx in advance!

  5. #5
    SitePoint Member
    Join Date
    Oct 2004
    Location
    Bangalore
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your requirement is not clear

    Can you post sample html

    - Kiran Makam

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi!
    ok ill be more clear!
    nevermind for the last post...
    hth
    Code:
    <script type="text/javascript" language="javascript">
    var mathop = new Array()
    function modmath(numt) {
    <?php
    $ret = mysql_query("SELECT * FROM mathop ORDER By id ASC");
    $i = 1;
    while($row = mysql_fetch_array($ret)) {
    echo "mathop[".$i."] = \"".$row[1]."/".$row[2]."/".$row[3]."\";\n";
    $i++;
    }
    mysql_free_result($ret);
    ?>
    if(document.getElementById("a"+numt).innerHTML == mathop[numt]) { // here i need the replacing
    alert(mathop[numt]);
    }
    }
    </script>
    my output of mathop array is longer then i want to match..
    so i need to match the value till the first slash and output into allert the all mathop value that is matched...
    so here i need some explaination about how to replace the value after first slash with noting into if statement!
    any suggestion will be appreciated!
    thanx in advance!!!

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    solved..
    thanx anyway...

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script type="text/javascript" language="javascript">
    var mathop = new Array()
    <?php
    $ret = mysql_query("SELECT * FROM mathop ORDER By id ASC");
    $i = 1;
    while($row = mysql_fetch_array($ret)) {
    echo "mathop[".$i."] = \"".$row[1]."/".$row[2]."/".$row[3]."\";\n";
    $i++;
    }
    mysql_free_result($ret);
    ?>
    function modmath(numt) {
    var reM, valM, operM, proceM, minusM;
    if(document.getElementById(document.getElementById("hiddenVal").value).value == 0) {
    alert("The selected field is empty!");
    return false;
    }
    if(document.getElementById(document.getElementById("hiddenVal").value).value.search(/[A-Za-z]/) != -1) {
    alert("Can't do math operations becouse of letters\npresent into the selected field!");
    return false;
    }
    if(mathop[numt].search(/.+\/.+\/\d+\.\d+/) != -1) {
    reM = /\/.+\/\d+\W\d+/
    } else {
    reM = /\/.+\/\d+/
    }
    if(document.getElementById("a"+numt).innerHTML == mathop[numt].replace(reM, "")) {
    valM = /.+\//
    operM = /\/.+\//
    if(mathop[numt].replace(mathop[numt], operM).length == 3) {
    if(mathop[numt].replace(mathop[numt], operM).substr(1, 1) == "+" ||  mathop[numt].replace(mathop[numt], operM).substr(1, 1) == "-" ||  mathop[numt].replace(mathop[numt], operM).substr(1, 1) == "/") {
    document.getElementById("valz").value = Math.round(eval((document.getElementById(document.getElementById("hiddenVal").value).value * 1) + mathop[numt].replace(mathop[numt], operM).substr(1, 1) + mathop[numt].replace(valM, "")) * 100) / 100;
    }
    if(mathop[numt].replace(mathop[numt], operM).substr(1, 1) == "x") {
    document.getElementById("valz").value = Math.round(eval((document.getElementById(document.getElementById("hiddenVal").value).value * 1) * mathop[numt].replace(valM, "")) * 100) / 100;
    }
    } else if(mathop[numt].replace(mathop[numt], operM).length == 4) {
    proceM = eval(parseInt(document.getElementById(document.getElementById("hiddenVal").value).value) / 100 * mathop[numt].replace(valM, ""));
    document.getElementById("valz").value = Math.round(eval(parseInt(document.getElementById(document.getElementById("hiddenVal").value).value) + mathop[numt].replace(mathop[numt], operM).substr(1, 1) + proceM) * 100) / 100;
    }
    }
    if(document.getElementById(document.getElementById("hiddenVal").value).value > document.getElementById("valz").value) {
    document.getElementById("dif").value = Math.round(eval((document.getElementById(document.getElementById("hiddenVal").value).value * 1) - (document.getElementById("valz").value * 1)) * 100) / 100;
    } else {
    minusM = Math.round(eval((document.getElementById(document.getElementById("hiddenVal").value).value * 1) - (document.getElementById("valz").value * 1)) * 100) / 100;
    document.getElementById("dif").value = - (minusM);
    }
    }
    </script>
    for js funs
    Last edited by reminder; Jan 4, 2005 at 02:30.


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
  •