skip to Main Content

I added some radio buttons to a PHP form in osCommerce. I plan on adding a bunch more but in the meantime I have four right now. I’m wondering if I need to set all the radio buttons as true/false or just the single instance I want as true.

Here’s the code I have:

if (!isset($pInfo->tab_id)) $pInfo->tab_id = '1';

switch ($pInfo->tab_id) {
  case '0': $none_tab_id = true; $shirt_tab_id = false; 
            $coverall_tab_id = false; $glove_tab_id = false; 
            break;
  case '1': $coverall_tab_id = true; $shirt_tab_id = false; 
            $none_tab_id = false; $glove_tab_id = false; 
            break;
  case '2': $coverall_tab_id = false; $shirt_tab_id = true; 
            $none_tab_id = false; $glove_tab_id = false; 
            break;
  case '3': $glove_tab_id = true; $coverall_tab_id = false; 
            $shirt_tab_id = false; $none_tab_id = false; 
            break;
   default: $coverall_tab_id = false; $shirt_tab_id = true; 
            $none_tab_id = false;
}     

.

<?php 

echo '&nbsp;None' . tep_draw_radio_field('tab_id', '0', $none_tab_id) . 
     '&nbsp;coveralls' . tep_draw_radio_field('tab_id', '1', $coverall_tab_id) .
     '&nbsp;Shirts' .tep_draw_radio_field('tab_id', '2', $shirt_tab_id) . 
     '&nbsp;Gloves' . tep_draw_radio_field('tab_id', '3', $glove_tab_id); 

?>

osCommerce defines the tep_draw_radio_field function as this:

tep_draw_radio_field($name, $value = '', $checked = false, $compare = '')

Is it necessary to set all the variables to false in each case or if can I just set the one I want to true?

4

Answers


  1. Why not just do something like this?

    if (!isset($pInfo->tab_id)) $pInfo->tab_id = '1';
    
    $none_tab_id = ($pInfo->tab_id == '0');
    $coverall_tab_id = ($pInfo->tab_id == '1');
    $shirt_tab_id = ($pInfo->tab_id == '2');
    $glove_tab_id = ($pInfo->tab_id == '3');
    
    if(!($none_tab_id || $coverall_tab_id || $glove_tab_id || $shirt_tab_id)) {
        $shirt_tab_id = true;
    }
    
    Login or Signup to reply.
  2. In the tep_draw_radio_field() function the only required parameter is the name variable, of which the radio button will use.

    The rest of the arguments are optional and have defaults already set for them.

    You don’t need to code all of the radio buttons to either true or false. The default argument is false and any that aren’t explicitly set when you call the function will be set as such.

    Just set the one you need to be true as true. When you do, make doubly sure that it’s also the third argument.

    Login or Signup to reply.
  3. You could tighten up the switch statement:

    $none_tab_id = false; 
    $shirt_tab_id = false;                 
    $coverall_tab_id = false; 
    $glove_tab_id = false; 
    switch ($pInfo->tab_id) {
      case '0': $none_tab_id = true; 
                break;
      case '1': $coverall_tab_id = true; 
                break;
      case '2': $shirt_tab_id = true; 
                break;
      case '3': $glove_tab_id = true;  
                break;
       default: $shirt_tab_id = true; 
    
    }   
    
    Login or Signup to reply.
  4. You don’t need to code all of the radio buttons to either true or false. The default argument is false and any that aren’t explicitly set when you call the function will be set as such.

    Just set the one you need to be true as true. When you do, make doubly sure that it’s also the third argument.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search