-1

My case similar with this Insert array values with foreach using codeigniter (user input by dynamically adding and removing fields).

But it's CI, I trying in open cart, so I build function insert and passing value(in array) and performs for each in model.

What I want insert is product this for code Controller :

// check request and store to array
if(isset($this->request->get['product']))
{ 
    $product = $this->request->get['product'];
    $product2 = array();
    foreach($product as $p)
    {
        $product2[] = $p;
    }
}
else 
{
    $product = null;
}


// This for 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 is for Model :

public function insert_detail($product2)
{
    foreach($product2 as $detail)
    {
        $this->db->query("INSERT INTO " . DB_PREFIX . "show_product_detail SET product_id = '". $this->db->escape($detail['product']) . "'");
    }
}

This for View :

    <?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $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 for 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 !!! ');}
    });

    // for 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 to enter into the database , but no contents? Where i did mistake?

8
  • Open Cart and Codeigniter code are different. The may look they same though. Commented Jan 29, 2016 at 3:44
  • @wolfgang1983 ow sorry as I recall only enter foreach and opencart only. Commented Jan 29, 2016 at 3:47
  • INSERT INTO " . DB_PREFIX . "show_product_detail SET product_id = '". $this->db->escape($detail['product']) . "' does not generate a valid mysql insert statement. Commented Jan 29, 2016 at 3:58
  • @Fabricator sorry can you help me with write the correct code? Commented Jan 29, 2016 at 4:29
  • sure. what fields are in show_product_detail table, and what's in $detail? Commented Jan 29, 2016 at 4:34

2 Answers 2

0

Here's the correct insert statement:

$this->db->query("
    INSERT INTO " . DB_PREFIX . "show_product_detail 
    (show_product_detail) 
    values ('". $this->db->escape($detail['product']) . "'");
Sign up to request clarification or add additional context in comments.

2 Comments

it's not work the query not run when i try it, its stuck in insert, if correct should be back to admin/item/item.php
i'm sorry. i'm out of ideas
0

You are putting an array in product_id field.

First you need to change product_id field type int to text and

Check with following code

public function insert_detail($product2)
{
    foreach($product2 as $detail)
    {
        $this->db->query("INSERT INTO " . DB_PREFIX . "show_product_detail SET product_id = '". $this->db->escape(json_encode($detail)) . "'");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.