Skip to content Skip to sidebar Skip to footer

Modify This Js Function So That It Uses The Value Fetched From The Db Instead Of The Values Specified In The Array To Generate The Select Options

How can I modify this function so that it can use the value fetched from the db instead of the values specified in the array used to generate the Select options? The function allow

Solution 1:

I think you should consider changing the PHP code, and leave the JavaScript as it is.

How about this:

Before your call to echo printSelectOptions(...), find the entry in $options where the key equals $row_default['jeans'] (which never ends with "_default") and change key of that entry to $row['jeans']. You might be just setting it to what it already was, but you might be changing it so it now ends with "_default".

You would also change $selected = $row_default['jeans']; to $selected = $row['jeans'];.

Now if the value from the database ends with "_default", the selected option (and only the selected option) will end with "_default". Since the JavaScript remains the same, it will remove the "_default" if the user focuses on the select element.

The code would look like this:

// This is a helper function that replaces a key while maintaining the entry's// position in the array. It does not modify the given array, but returns a// new array.functionreplaceKey($array, $oldKey, $newKey) {
    $newArray = array();
    foreach ($arrayas$key => $value) {
        $newArray[($key === $oldKey) ? $newKey : $key] = $value;
    }
    return$newArray;
}

$options = array("Null"=>"", "$0"=>"$0", "$50"=>"$50", "$100"=>"$100", "$150"=>"$150");

// We will change the key for the entry in $options so it includes "_default" if// the value from the database includes "_default". We need to do this only if// $row['jeans'] ends with "_default". That will be the case if these two are// different.if ($row_default['jeans'] !== $row['jeans']) {
    $options = replaceKey($options, $row_default['jeans'], $row['jeans']);
}

// Now we can use the value from the db for setting the selected value, rather// than the value that had "_default" removed. $selected = $row['jeans'];

// Pre-selects the option matching the db information.echo printSelectOptions($options, $selected);

UPDATE:

The JavaScript that executes when the select gains focus looks correct. I tested it in a jsfiddle and it appears to work fine. If you look at the jsfiddle, I altered it some so it reflects the fact that the options end with "_default". I also changed the use of .attr() to .prop(), but the original worked as well. (If you are using jQuery >=1.6, you should use .prop() to change element properties, not .attr().)

If the PHP is generating the select the way it looks in the jsfiddle, then there must be another reason the JavaScript is not working. Perhaps there is a JavaScript error that is causing the event handler to not get hooked up.

Solution 2:

$row in your code is an array and you access field using $row['jeans'] and to return all rows you need to put $stmt->fetch() in loop.

while ($row = $stmt->fetch()) {
    $row_default = str_replace($search_default, $replace_default, $row['jeans']);
    echo'<option value="' . $row_default . '">' . $row_default . '</option>';
}

Here is fetch documentation

Post a Comment for "Modify This Js Function So That It Uses The Value Fetched From The Db Instead Of The Values Specified In The Array To Generate The Select Options"