my case similar insert array values foreach using codeigniter (user input dynamically adding , removing fields).
but it's ci, trying in open cart, build function insert , passing value(in array) , performs each in model.
what want insert product code controller :
// check request , store array if(isset($this->request->get['product'])) { $product = $this->request->get['product']; $product2 = array(); foreach($product $p) { $product2[] = $p; } } else { $product = null; } // function insert public function insert() { $this->language->load('item/item'); $this->document->settitle($this->language->get('heading_title')); $this->load->model('item/item'); if (($this->request->server['request_method'] == 'post') && $this->validateform()) { $this->model_item_item->insert_detail($this->request->post); $this->session->data['success'] = $this->language->get('text_success'); $this->redirect($this->url->link('item/item', 'token=' . $this->session->data['token'], 'ssl')); } }
this model :
public function insert_detail($product2) { foreach($product2 $detail) { $this->db->query("insert " . db_prefix . "show_product_detail set product_id = '". $this->db->escape($detail['product']) . "'"); } }
this view :
<?php echo $header; ?> <div id="content"> <div class="breadcrumb"> <?php foreach ($breadcrumbs $breadcrumb) { ?> <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a> <?php } ?> </div> <?php if ($error_warning) { ?> <div class="warning"><?php echo $error_warning; ?></div> <?php } ?> <?php if ($success) { ?> <div class="success"><?php echo $success; ?></div> <?php } ?> <div class="box"> <div class="heading"> <h1><img src="view/image/product.png" alt="" /> <?php echo $heading_title; ?></h1> <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a href="<?php echo $cancel; ?>" class="button"><?php echo $button_cancel; ?></a></div> </div> <div class="content"> <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form"> <table class="form"> <tr> <td><span class="required">*</span> <?php echo $entry_head; ?></td> <td><input type="text" name="head_text_field" value="" placeholder="input head text" size="40"/> <?php if ($error_head) { ?> <span class="error"><?php echo $error_head; ?></span> <?php } ?> </td> </tr> <tr> <td><span class="required">*</span> <?php echo $entry_title; ?></td> <td><textarea name="title_text_field" placeholder="input title text" style="width:230px;"/></textarea> <?php if ($error_title) { ?> <span class="error"><?php echo $error_title; ?></span> <?php } ?> </td> </tr> <tr> <td><?php echo $entry_max_item; ?></td> <td> <select name="max" id="maxitem"> <?php for($i=1; $i<=6; $i++) { if($i == 1) echo "<option selected='selected' value=".$i.">".$i."</option>"; else echo "<option value=".$i.">".$i."</option>"; } ?> </select> </td> </tr> <tr> <td><?php echo $entry_product; ?></td> <td><input type="text" id="product" name="product" value="" placeholder="input product" size="40"/></td> </tr> <tr> <td></td> <td> <table> <tr> <td><input type="button" id="add" value="add item"></td> <td><input type="reset" id="reset" value="reset"></td> </tr> </table> </td> </tr> <tr> </tr> </table> <table border="1" id="tblname" cellpadding="5" cellspacing="5"> <thead> <tr> <td> total item </td> <td> name item </td> <td> delete </td> <tr> </thead> <tbody align="center"> </tbody> </table> </form> </div> </div> </div>
this javascript:
<script type="text/javascript"><!-- $('input[name=\'product\']').autocomplete({ delay: 100, source: function(request, response) { $.ajax({ url: 'index.php?route=catalog/product/autocomplete&token=<?php echo $token; ?>&filter_name=' + encodeuricomponent(request.term), datatype: 'json', success: function(json) { response($.map(json, function(item) { return { label: item.name, value: item.product_id } })); } }); }, select: function(event, ui) { $('input[name=\'product\']').val(ui.item.label); return false; }, focus: function(event, ui) { return false; } }); //--></script> <script type="text/javascript"> $(document).ready(function(){ var item = 1; var isallowed = 3; var isset = 0; $('#add').click(function(){ var maxitem = parseint($("#maxitem").val(), 10); //from max item in html var icount = 0; if($('#product').val()){ // check input product if( item <= maxitem ) { icount = $('#tblname tbody tr').length + 1; sztr = "<tr><td>"; sztr = sztr + icount + "</td>"; sztr = sztr + "<td>" +$('#product').val() +"</td>"; sztr = sztr + "<td><input type='button' class='del' value='delete'></td>"; sztr = sztr + "</tr>"; $('#tblname tbody').append(sztr); item +=1; isset = 1; restformopts(); } else { alert ("max limit !!!"); } }else{alert('enter product !!! ');} }); // delete row $('body').on('click','#reset', function() { item = 1; isallowed = 3; isset = 0; $("#maxitem").attr("disabled",false); $('.del').parents('tr').remove(); }); $('body').on('click', 'input.del', function() { $(this).parents('tr').remove(); item -= 1; }); function restformopts() { if(isset === isallowed) { $("#add").attr("disabled",true); $("#maxitem").attr("disabled",false); } else { $("#add").attr("disabled",false); $("#maxitem").attr("disabled",true); } } }); </script>
this code works enter database , no contents? did mistake?
here's correct insert statement:
$this->db->query(" insert " . db_prefix . "show_product_detail (show_product_detail) values ('". $this->db->escape($detail['product']) . "'");
Comments
Post a Comment