作为函数的参数

时间:2017-01-08 12:28:32

标签: php function parameters

我有两个功能:

复选框创建者:

function AddCheckBoxes($title,$box_values,$box_titles){
    echo '<div class="form-group"><label>' . $title . '</label>';
    $boxes_values_titles = new MultipleIterator();
    $boxes_values_titles->attachIterator(new ArrayIterator($box_values));
    $boxes_values_titles->attachIterator(new ArrayIterator($box_titles));
    foreach ( $boxes_values_titles as $value ) {
        list($box_values, $box_titles) = $value;
        echo '<div class="checkbox"><label><input type="checkbox" value="' . $box_values . '">' . $box_titles , '</label></div>' , PHP_EOL;
    }
}

带标签创建者的面板:

function AddBasicTabsPanel($tabs_ids,$tabs_names,$tabs_data){
    //START TABS HEADERS
    echo '<ul class="nav nav-tabs">';
    //PRINT TABS HEADERS
    $tabs_ids_names_data = new MultipleIterator();
    $tabs_ids_names_data->attachIterator(new ArrayIterator($tabs_ids));
    $tabs_ids_names_data->attachIterator(new ArrayIterator($tabs_names));
    $tabs_ids_names_data->attachIterator(new ArrayIterator($tabs_data));
    foreach ( $tabs_ids_names_data as $ids_names_data ) {
        list($tabs_ids, $tabs_names, $tabs_data) = $ids_names_data;
        echo '<li><a href="#' . $tabs_ids . '" data-toggle="tab">' . $tabs_names . '</a></li>' , PHP_EOL;
    }
    //END TABS HEADERS
    echo '</ul>';
    //START TAB PANES
    echo '<div class="tab-content">';
    //PRINT TAB PANES
    foreach ( $tabs_ids_names_data as $ids_names_data ) {
        list($tabs_ids, $tabs_names, $tabs_data) = $ids_names_data;
        echo '<div class="tab-pane fade" id="' . $tabs_ids . '"><h4>' . $tabs_names . '</h4>' . $tabs_data . '</div>' , PHP_EOL;
    }
    //END TAB PANES
    echo '</div>';
}

目前,当我尝试以下代码时:

AddBasicTabsPanel(["One","Two","Three"],["One","Two","Three"],["data","Some data","More data"]);

我得到了一个显示所有数据的正确标签元素。图片:

enter image description here

但是当我想用tabsesdate替换tabs_data中的文本时,复选框会显示在带有标签的面板之前。

AddBasicTabsPanel(["One","Two","Three"],["One","Two","Three"],[AddCheckBoxes("Checkboxes",["20","39","31"],["One","Two","Three"]),"Some data","More data"]);

照片:

enter image description here

我尝试了几个小时的故障排除,但不知道如何使用复选框功能作为创建标签的面板的功能参数。我哪里出错了?

1 个答案:

答案 0 :(得分:1)

之前您正在看复选框,因为这是执行函数的顺序。您遇到的问题是在AddBasicTabsPanel之前调用AddCheckBoxes并且它打印出来并返回null。意思是你的代码是

  1. 打印出AddCheckBoxes
  2. 致电AddBasicTabsPanel(["One","Two","Three"],["One","Two","Three"],[null,"Some data","More data"]);
  3. 你可以从你的方法返回字符串做这样的事情。

    function AddCheckBoxes($title,$box_values,$box_titles) {
        $result = '';
        $result+= '<div class="form-group"><label>' . $title . '</label>';
        $boxes_values_titles = new MultipleIterator();
        $boxes_values_titles->attachIterator(new ArrayIterator($box_values));
        $boxes_values_titles->attachIterator(new ArrayIterator($box_titles));
        foreach ( $boxes_values_titles as $value ) {
            list($box_values, $box_titles) = $value;
            $result+= '<div class="checkbox"><label><input type="checkbox" value="' . $box_values . '">' . $box_titles . '</label></div>' . PHP_EOL;
        }
    
        return $result;
    }
    

    此外,您的代码段在少数地方有,而不是.

    如果你有每个参数代表单个标签及其数据,那会不会更有意义?您可以拥有类似这样的内容

    ,而不是让3个参数包含3个标签的数据
    $tab1 = [
        'id' => 'tab1-id',
        'name' => 'tab1-name',
        'data' => 'tab1-data',
    ];
    
    $tab2 =  [
        'id' => 'tab2-id',
        'name' => 'tab2-name',
        'data' => YourMethodThatReturnsWhateverHTMLYouWant(),
    ];
    
    AddBasicTabsPanel($tab1, $tab2);
    

    此外,您不需要MultipleIterator()

    function AddCheckBoxes($title, $checkboxes) {
        $result = '<div class="form-group"><label>' . $title . '</label>';
    
        foreach($checkboxes as $checkbox) {
            $result+= '<div class="checkbox"><label><input type="checkbox" value="' . $checkbox['value'] . '">' . $checkbox['title] . '</label></div>' . PHP_EOL;  
        }
    
        return $result;
    }
    
    $checkboxes = [
        [
            'title' => 'checkbox1-title',
            'value' => 20,
        ],
        [
            'title' => 'checkbox2-title',
            'value' => 39,
        ],
    ];
    
    $checkboxHtml = AddCheckBoxes('Checkbox-title', $checkboxes);