【问题标题】:Multiple loop conditions for a set of checkbox choices一组复选框选择的多个循环条件
【发布时间】:2015-01-02 05:25:12
【问题描述】:

使用 CodeIgniter,我正在创建一个管理区域,我可以在其中为任何给定的产品线创建脚注。

HTML

<p>Choose Product Line</p>
<label><input type="checkbox" name="productline[]" value="1" />Product One</label>
<label><input type="checkbox" name="productline[]" value="2" />Product Two</label>
<label><input type="checkbox" name="productline[]" value="3" />Product Three</label>
<label><input type="checkbox" name="productline[]" value="4" />Product Four</label>

PHP

for ( $i = 0; $i < count( $this->input->post( 'product[]' ) ); $i++ )
{
    $line__id = $this->input->post( 'product' )[$i];

    $footnote_data = array(
        'line__id' => $line__id,
        'code' => $this->input->post( 'footnotecode' ),
        'text' => $this->input->post( 'footnotetext' ),
        'status' => $this->input->post( 'status' ),
        'createdon' => date( 'Y-m-d H:i:s' )
    );

    error_log( print_r( $footnote_data, true ) );
}

这将输出如下内容:

日志:

Array
(
    [line__id] => 1
    [code] => ABC123
    [text] => Hello World
    [status] => 1
    [createdon] => 2014-12-31 22:28:59
)

Array
(
    [line__id] => 2
    [code] => ABC123
    [text] => Hello World
    [status] => 1
    [createdon] => 2014-12-31 22:28:59
)
....

一切都很好,我正在遍历每个选定的产品线。

正如您在$footnote_data 数组中看到的,我还有其他几个字段。它们是简单的文本输入字段,与循环的当前迭代为 1:1 $i

我还有 2 个其他复选框选项。您必须选择产品线(上图),然后输入代码和实际脚注文本。然后,您必须选择脚注的类型

HTML

<p>Footnote Type</p>
<label><input type="checkbox" name="footnotetype[]" value="awesome" />Awesome</label>
<label><input type="checkbox" name="footnotetype[]" value="cool" />Cool</label>
<label><input type="checkbox" name="footnotetype[]" value="average" />Average</label>

我可以为我正在创建的当前脚注选择任意数量的脚注类型。 我只是不确定进入第二系列复选框(类型)的最佳方法。

我在主产品线循环中尝试了一个嵌套循环,但没有得到我想要的结果。如果我记录我正在寻找的结果,它会是这样的:

输出

Array
(
    [line__id] => 2
    [type] => Awesome
    [code] => ABC123
    [text] => Hello World
    [status] => 1
    [createdon] => 2014-12-31 22:28:59
)

Array
(
    [line__id] => 2
    [type] => Cool
    [code] => ABC123
    [text] => Hello World
    [status] => 1
    [createdon] => 2014-12-31 22:28:59
)

Array
(
    [line__id] => 3
    [type] => Average
    [code] => ABC123
    [text] => Hello World
    [status] => 1
    [createdon] => 2014-12-31 22:28:59
)

....

了解同一产品线如何具有多种脚注类型?对于所选的每个产品线,实际记录将/可能完全相同。只有类型可以不同。

我的另一个想法 - 一旦完成初始循环 - 我有一个新创建的脚注的脚注 ID,我是否右转并更新每个脚注类型的数据库? 我在圈子里自说自话……

我的数据库结构如下所示:

数据库

footnote_id
type_id  // this is the question
text
code
status
createdon

感谢您的帮助!

【问题讨论】:

    标签: php codeigniter loops checkbox


    【解决方案1】:

    我没有测试代码,所以可能某处有错误:

    $products_data = array();
    foreach ($this->input->post('product') ) as $product_id)
    {
        $footnote_data = array(
            'line__id' => $product_id,
            'code' => $this->input->post( 'footnotecode' ),
            'text' => $this->input->post( 'footnotetext' ),
            'status' => $this->input->post( 'status' ),
            'createdon' => date( 'Y-m-d H:i:s' )
        );
    
        foreach($this->input->post('footnotetype') as $type){
           $footnote_data['type'] = $type;
           array_push($products_data, $footnote_data);
        }
    
        // error_log( print_r( $footnote_data, true ) );
    }
    var_dump($products_data);
    

    【讨论】:

    • 非常感谢!这正是我需要推动的推动力。
    • 很高兴我能帮上忙 ^^
    猜你喜欢
    • 2018-06-23
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多