JayminsFakeAccount JayminsFakeAccount - 2 months ago 10
PHP Question

Same Function for editing and inserting In Codeigniter

OKay So I am developing Ecommerce website, where I am using Common View file for editing and inserting data. Issue is I have made a common function for adding and inserting too. So If we edit the form, It's Id is being passed in URL. The Function gets the URL and Manipulates the data and view edited form. Problem is for inserting the data. My Common Function accepts one parameter.[Id which is being passed when Editing]. It gives me error when I try to load 'Add File'.

Missing argument 1 for Register::product()


Here is my Code:

Controller:
function product($get_product_id)
{
//DO Something
//Check POST data
// add OR UPDATE POST Data
}

View File:
<? if(isset($data))
{
foreach ($data as $data1)
{
$get_product_id=$data1->id;
$get_product_name=$data1->product_name;
$get_product_code=$data1->code;
$get_product_price=$data1->price;
$get_product_quantity=$data1->quantity;
$get_product_status=$data1->status;
$get_product_details=$data1->details;
$get_product_image=$data1->productimage;

}

}
else
{
$get_product_id="";
$get_product_name="";
$get_product_code="";
$get_product_price="";
$get_product_quantity="";
$get_product_status="";
$get_product_details="";
$get_product_image="";
}
?>
<? echo form_open_multipart('register/product/'.$get_product_id.'');?>
<table width="90%" border="0" cellspacing="0" cellpadding="6">

<tr>
<td width="20%" align="right"><? echo form_label('Product Name');?></td>
<td width="80%"><?echo form_input(array('id' =>'product_name','name' =>'product_name','size' => '64','value' => $get_product_name));echo form_hidden('id', $get_product_id);?>
<br/>
</td>
</tr>
//And So on...

Answer

Your controller code should be

Controller:
   function product($get_product_id = NULL) {
    if ($get_product_id) {
        //edit code
    } else {
        //insert code
    }

    //DO Something
    //Check POST data
    // add OR UPDATE POST Data
}
Comments