Search code examples
phposcommerce

Select multiple default, or previously chosen, values in a select box in osCMax


I am using the following method for displaying the multiple select box and it works fine in the new form.

echo tep_draw_pull_down_menu("cat[$i][]", $cat_array, '', 'id=cat3, size=5 multiple');

But in the edit form I want the values to be selected by default which are inserted in the add form.

The values which are to be selected by default are stored in an array. So I passed the array to the default value like the following.

echo tep_draw_pull_down_menu("cat[$i][]", $cat_array, "$arr", 'id=cat3, size=5 multiple');

But it did not selected the required values. It only selected one value at a time because we cannot pass an array to the default value.

How will I do this?


Solution

  • You're not going be able to without changing some of the codebase.

    By default, the tep_draw_pull_down_menu method is only going to allow and check for strings. It's not at all expecting to be passed more than one value to check against.

    Edit the tep_draw_pull_down_menu method found in the includes/functions/html_output.php by looking at this line (around 312 of a clean install):

    if ($default == $values[$i]['id']) {
    

    Change it to the following:

    if ($default == $values[$i]['id'] || (is_array($default) && in_array($values[$i]['id'], $default))) {
    

    The extra bit adds a check to see if the $default variable passed was an array, and if so, if the current value of the select option is in there. If yes on both counts, then mark the option as selected.

    Here's an example of it in use:

    $cat_array[] = array("id" => 'marvelman', "text" => 'Kimota!');
    $cat_array[] = array("id" => 'rorschach', "text" => 'hurm');
    $cat_array[] = array("id" => 'cerebus', "text" => 'Something Fell');
    $cat_array[] = array("id" => 'wolvie', "text" => 'Snikt!');
    $cat_array[] = array("id" => 'spider-man', "text" => 'Thwip');
    
    $arr = array('rorschach', 'wolvie', 'cerebus');
    
    echo tep_draw_pull_down_menu("cat[$i][]", $cat_array, $arr, 'id="cat3" size="5" multiple');
    

    You may also not want to pass the $arr variable in quotes unless you explicitly want to pass the string $arr as opposed to its array of values.