3

I have a table view of some products having Edit and Delete options.When i click on Edit it redirects to another page and shows particular data.There is one field named Product Category which shows product category of product in combobox e.g. Pizza.

Now,the problem begins.Suppose i select Sandwich instead of Pizza from combobox and try to update that value in database.It's not update anything.

I have used Codeigniter for this project,so i want solution for the same.I have searched in Google as well from other help,but hard luck. I am not getting proper solution.

Any reply would be appreciate.

My Code Snippet is given below.

1)Controller

function products_edit($product_id)
{
    $this->load->library('form_validation');
    $this->load->helper('form');  
    $this->load->helper('html');    
    $this->load->model('products_model');
    $data=$this->products_model->general();
    $product = $this->products_model->get_product($product_id);
    $category['categories']=$this->products_model->get_category();
    $this->data1['title'] = 'Edit Product';

    //validate form input
    $this->form_validation->set_rules('name', 'Product name', 'required|xss_clean');
    $this->form_validation->set_rules('description', 'Description', 'required|xss_clean');
    //$this->form_validation->set_rules('category', 'Category', 'required|xss_clean');
    //$this->form_validation->set_rules('extras', 'Extras', 'required|xss_clean');
    $this->form_validation->set_rules('price', 'Price', 'required|xss_clean');
    $this->form_validation->set_rules('is_featured', 'Is Featured', 'required|xss_clean');
    $this->form_validation->set_rules('prorder', 'Order', 'required|xss_clean');

    if (isset($_POST) && !empty($_POST))
    {       
        $data1 = array(
            'product_name'=> $this->input->post('name'),
            'product_desc'=> $this->input->post('description'),
            'product_category'=> $this->input->post('category'),
            'extras'=> $this->input->post('extras'),
            'price'=> $this->input->post('price'),
            'is_featured'=> $this->input->post('is_featured'),
            'prorder' => $this->input->post('order'),
        );

        if ($this->form_validation->run() === true)
        {
            $this->products_model->updateproducts($product_id, $data1);

            $this->session->set_flashdata('message', "<p>Product updated successfully.</p>");

            redirect('products_controller/products_edit/'.$product_id);
        }           
    }

    $this->data1['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));

    $this->data1['product'] = $product;
    //$this->data1['category']=$category;

    //display the edit product form     
    $this->data1['name'] = array(
        'name'      => 'name',
        'id'        => 'name',
        'type'      => 'text',
        'style'     => 'width:300px;',
        'value'     => $this->form_validation->set_value('name', $product['product_name']),
    );

    $this->data1['description'] = array(
        'name'      => 'description',
        'id'        => 'description',
        'type'      => 'text',
        'cols'      =>  40,
        'rows'      =>  5,
        'value'     => $this->form_validation->set_value('description', $product['product_desc']),
    );

        $category1=$this->products_model->get_category();
        $category1['value']=set_value('category',$product['product_category']);
        $temp=array(
        '1'=>$category1['value'],
        );
        $category1=array_diff($category1,$temp);
        $category1['value']=set_value('category',$product['product_category']);

        $this->data1['category']=$category1;



    //$this->data1['category'] = array(
        //'name'    => 'category',
    //  'id'        => 'category',
    //  'type'      => 'text',
    //  'style'     => 'width:300px;',
    //'value'   => $this->form_validation->set_value('category',$category['categories']),set_value('category',$product['product_category']),


        //$this->data1['category']= $this->form_validation->set_value('category',$category['categories']);
        //);


    $this->data1['extras'] = array(
        //'name'    => 'extras',
        //'id'      => 'extras',
        //'type'    => 'text',
        //'style'       => 'width:300px;',
        'value'     => $this->form_validation->set_value('extras', $product['extras']),
    );

    $this->data1['price'] = array(
        'name'      => 'price',
        'id'        => 'price',
        'type'      => 'text',
        'style'     => 'width:40px;text-align: right',
        'value'     => $this->form_validation->set_value('price', $product['price']),
    );

    $this->data1['is_featured'] = array(
        'name'      => 'is_featured',
        'id'        => 'is_featured',
        'type'      => 'text',
        'style'     => 'width:40px;text-align: right',
        'value'     => $this->form_validation->set_value('isfeatured', $product['is_featured']),
    );

    $this->data1['prorder'] = array(
        'name'  => 'prorder',
        'id'    => 'prorder',
        'type'  => 'text',
        'style' => 'width:40px;',
        'value' => $this->form_validation->set_value('prorder', $product['prorder']),
    );
    $this->load->view('products_edit', $this->data1);
}

2)Model

function get_product($product_id) {
    $this->db->select('product_id,product_name,product_desc,product_category,extras,price,is_featured,prorder');
    $this->db->where('product_id', $product_id);
    $this->db->distinct('product_category');
    $query = $this->db->get('product');
    return $query->row_array();

}

function updateproducts($product_id, $data)
{
    $this->db->where('product_id', $product_id);
    $this->db->update('product', $data);
}

3)View

<?php $product_id = $product['product_id']; ?>
<?php echo form_open("products_controller/products_edit/$product_id");?>
<table width="500" border="1" cellpadding="0" cellspacing="2" align="center">
<tr>
<td width="130" align="right"> Product Name: </td>
<td><?php echo form_input($name); ?></td>
</tr>
<tr>
<td width="130" align="right"> Product Description: </td>
<td><?php echo form_textarea($description); ?></td>
</tr>
<tr>
<td align="right">Product Category:</td>
<td>
<?php echo form_dropdown("product_id",$category,'value')?>      
</td>
</tr>
<tr>
<td align="right">Extras:</td>
<td><?php echo  form_dropdown("product_id",$extras,"#","id='product_id'");?></td>
</tr>
<tr>
<td align="right">Price:</td>
<td><?php echo form_input($price); ?></td>
</tr>
<tr>
<td align="right">Is Featured:</td>
<td><?php echo form_input($is_featured); ?></td>
</tr>
<tr>
<td align="right">Order:</td>
<td><?php echo form_input($prorder); ?></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><?php echo form_submit('submit', 'Submit');?>
</td>
</tr>
</table>
<table align="center">
<tr>
<td>
<?php echo form_close(); ?>
<?php echo form_open("products_controller/products_search");?>
<?php echo form_submit('submit', 'Back');?>
<?php echo form_close(); ?>

Thanks in advance.

4
  • Have a look at update multiple select values in mysql database using codeigniter Commented Nov 30, 2013 at 6:06
  • @dianuj.Thank you for your reply.But, its not proper solution for me. Commented Nov 30, 2013 at 6:21
  • Have you read the comments of answer you need that type of logic Commented Nov 30, 2013 at 6:22
  • @dianuj,yes friend i have read all the comments.But, my problem is different.I want solution for combobox. Other field values are updating,but combobox value is not updating. Commented Nov 30, 2013 at 6:33

2 Answers 2

2

use below code,

$test = array();
$count=count($category['categories']);
for($i=0;$i<$count;$i++)
{
 $test[$i] = array($category['categories'][$i] => $category['categories'][$i]);
} 
$this->data['category']=set_value('category',$test);
Sign up to request clarification or add additional context in comments.

Comments

0

to get the value of <?php echo form_dropdown("product_id",$category,'value')?> you have to write $this->input->post('product_id') this will get you the product id and then you can use it to update db

3 Comments

but friend other values are updating, only combobox value is not.
i have gone through your code several times. i can see that you are accessing your product_category value by [link]'product_category'=> $this->input->post('category'), but i didnt find any dropdown named 'category' can you pls check this..thanks
$this->data1['category']=$category1; this statement is combobox named 'category'

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.