在循环中使用redbean来存储数据

时间:2012-12-20 16:00:41

标签: php mysql loops redbean recipe

我正在使用redbean来创建我的网站,我目前正在为用户实现配方上传功能。

在'上传配方'表单中,我使用javascript动态添加4个输入字段(金额,测量,成分,注释)。但是,一旦用户点击提交,我不知道如何使用redbean来存储这些内容。谁能提出建议?我是否必须使用循环来遍历添加的每个字段并将其存储到数据库中?

以下是我正在使用的代码:

var count = 0;

$(function(addfield){
    $('p#add_field').click(function(){  
        count += 1;  
        $('#container').append(   
            '<strong>Ingredient ' + count + '</strong><br />'    
                + '`<`input id="field_' + count + 'name="amount" type="text" placeholder="Amount" `/>`'     +  
                 '<input id="field_' + count + 'name="measurement" type="text" placeholder="Measurement" `/>`'+   
                 '<input id="field_' + count + 'name="ingredient" type="text" placeholder="Ingredient" `/>`' +  
                 '<input id="field_' + count + 'name="notes" type="text" placeholder="Notes" `/>`' +   
        );  
    });  
});

这是调用函数的链接:

<p id="add_field"><a href="#addfield"><span>&raquo; Click to add ingredient</span></a></p>

1 个答案:

答案 0 :(得分:0)

您只需将代码置于循环中:

for($a=0;$a<4;$a++){
    $recipe=R::dispense('recipe');
    $recipe->import($_POST['recipe_'.$a]);//You need to configure this portion
    //OR
    $recipe->title=$_POST['recipe'][$a]['title'];
    R::store($recipe);
}