-1

我已在 jpg 文件中附加了代码格式问题的问题:::

问题是它没有将任何数据保存到“tbl_featured_table”中。

控制器

public function save_featured_product() {
$data = array();

if ($this->input->post()) {
$data['featured_id'] = $this->input->post('featured_id', true);
$data['product_id'] = $this->input->post('product_id', true);
$data['product_name'] = $this->input->post('product_name', true);

$data['featured_product_name'] = $this->input->post('featured_product_name', true);
$data['featured_product_price'] = $this->input->post('featured_product_price', true);
$data['featured_product_quantity'] = $this->input->post('featured_product_quantity', true);
$data['featured_product_sku'] = $this->input->post('featured_product_sku', true);
$data['featured_product_short_description'] = $this->input->post('featured_product_short_description', true);
$data['featured_product_long_description'] = $this->input->post('featured_product_long_description', true);
$data['featured_product_status'] = $this->input->post('featured_product_status', true);
$data['featured_product_image'] = $this->input->post('featured_product_image', true);

$this->sa_model->save_featured_product_info($data);
}

模型

function save_featured_product_info($data){
if (isset($data['product_id']) && is_array($data['product_id'])) {
foreach ($data['product_id'] as $key => $each) {
$temp[] = array(
'featured_id' => $data['featured_id'][$key],
'product_id' => $data['product_id'][$key],
'product_name' => $data['product_name'][$key],
'featured_product_name' => $data['featured_product_name'][$key],
'featured_product_price' => $data['featured_product_price'][$key],
'featured_product_quantity' => $data['featured_product_quantity'][$key],
'featured_product_sku' => $data['featured_product_sku'][$key],
'featured_product_short_description' => $data['featured_product_short_description'][$key],
'featured_product_long_description' => $data['featured_product_long_description'][$key],
'featured_product_status' => $data['featured_product_status'][$key],
'featured_product_image' => $data['featured_product_image'][$key],
);
}
if (isset($temp)) {
$this->db->insert_batch('tbl_featured_products', $temp);
}

看法

        Product Name:
    <select name="product_id[]">
    <?php foreach ($all_product as $values) { ?>
    <option value="<?php echo $values->product_name; ?>"><?php echo $values->product_name; ?></option>
    <?php } ?>
    </select>
    <?php foreach ($all_product as $values) { ?>
    <input type="checkbox" name="featured_product_name[]" value="<?php echo $values->product_name; ?>" /> 
    <?php echo $values->product_name; ?> <br>
    <input hidden="hidden" name="featured_id[]" value="<?php echo $values->product_id; ?>" />
    <input type="hidden" name="product_name[]" value="<?php echo $values->product_name; ?>" >
    <input name="featured_product_price[]" value="<?php echo $values->product_price;?>" /> 
    <input name="featured_product_quantity[]" value="<?php echo $values->product_quantity; ?>" /> 
    <input name="featured_product_sku[]" value="<?php echo $values->product_sku; ?>" /> 
    <input name="featured_product_short_description[]" value="<?php echo $values->product_short_description; ?>" /> 
    <input name="featured_product_long_description[]" value="<?php echo $values->product_long_description; ?>" /> 
    <input name="featured_product_status[]" value="<?php echo $values->product_status; ?>" /> 
    <input name="featured_product_image[]" value="<?php echo $values->product_image; ?>" /> 
    <?php } ?>

注意 ::: 我想按产品名称或产品 ID 保存多个数据。它也不能正常工作。我需要你的帮助来解决我的问题。

请专业帮助我。请不要给我打负分。

4

2 回答 2

2

查看

<select name="product_id[]">
<?php foreach ($all_product as $values) { ?>
<option value="<?php echo $values->product_name; ?>"><?php echo $values->product_name; ?></option>
<?php } ?>
</select>
<?php foreach ($all_product as $values) { ?>
<input type="checkbox" name="product_id[]" value="<?php echo $values->product_id; ?>" /> 
<?php echo $values->product_name; ?> <br />
<!-- <input hidden="hidden" name="featured_id[]" value="<?php //echo $values->product_id; ?>" /> -->
<input type="hidden" name="product_name[]" value="<?php echo $values->product_name; ?>" />
<input name="featured_product_price[]" value="<?php echo $values->product_price;?>" /> 
<input name="featured_product_quantity[]" value="<?php echo $values->product_quantity; ?>" /> 
<input name="featured_product_sku[]" value="<?php echo $values->product_sku; ?>" /> 
<input name="featured_product_short_description[]" value="<?php echo $values->product_short_description; ?>" /> 
<input name="featured_product_long_description[]" value="<?php echo $values->product_long_description; ?>" /> 
<input name="featured_product_status[]" value="<?php echo $values->product_status; ?>" /> 
<input name="featured_product_image[]" value="<?php echo $values->product_image; ?>" />

控制器

function save_featured_product() {
    $data = array();
    $feat = array();
    $fin  = array();
    if ($this->input->post()) {
        $feat    = $this->input->post('product_id', true);
        $data['featured_id']                        = $this->input->post('featured_id', true);
        //$data['product_id']                         = $this->input->post('product_id', true);
        $data['product_name']                       = $this->input->post('product_name', true);
        //$data['featured_product_name']              = $this->input->post('featured_product_name', true);
        $data['featured_product_price']             = $this->input->post('featured_product_price', true);
        $data['featured_product_quantity']          = $this->input->post('featured_product_quantity', true);
        $data['featured_product_sku']               = $this->input->post('featured_product_sku', true);
        $data['featured_product_short_description'] = $this->input->post('featured_product_short_description', true);
        $data['featured_product_long_description']  = $this->input->post('featured_product_long_description', true);
        $data['featured_product_status']            = $this->input->post('featured_product_status', true);
        $data['featured_product_image']             = $this->input->post('featured_product_image', true);
       if( isset( $feat ) && is_array( $feat ) && count( $feat ) > 0 ){
            foreach( $feat as $key => $each ){
                $fin[]  = array(
                'product_id'                            => $each,
                'product_name'                          => $data['product_name'][$key],
                'featured_product_price'                => $data['featured_product_price'][$key],
                'featured_product_quantity'             => $data['featured_product_quantity'][$key],
                'featured_product_sku'                  => $data['featured_product_sku'][$key],
                'featured_product_short_description'    => $data['featured_product_short_description'][$key],
                'featured_product_long_description'     => $data['featured_product_long_description'][$key],
                'featured_product_status'               => $data['featured_product_status'][$key],
                'featured_product_image'                => $data['featured_product_image'][$key],
                );  
            }
       } 
    $this->sa_model->save_featured_product_info($fin);
    }
}

模型

function save_featured_product_info($data){
    if( is_array( $data ) && count( $data ) > 0 ){
        $this->db->insert_batch('tbl_featured_products', $data);    
    }
}
于 2013-10-03T07:31:15.733 回答
1

我只是给你一些一般性的提示。以及在您的 $this->input->post 中包含 xss clean TRUE 的荣誉

总是以一两个值开始这样的项目。让它工作。然后添加其余的字段。我称之为“健全性检查”,因为试图处理这么多领域——会让你发疯:-)

查看控制器中的代码。现在查看模型中的代码。你看到你有很多代码——对于完全相同的值吗?控制器应该是瘦的和专横的。控制器老板是这样的:这个表单验证真假?如果为 false - 再次显示视图。如果为真:您是否将值添加到数据库表中?如果是 - 显示成功页面。

这些模型完成了繁重的数据工作。模型验证表单数据。该模型创建数据库所需的数据数组。对于有这么多数据的东西 - 创建一个单独的方法来创建数据数组。然后你有一个很好的简单数据库调用,比如

function _insertProduct( $product ) {
    $this->db->insert( 'tbl_featured_products', $product );
    if ( $this->db->affected_rows() == '1' ) {return TRUE;}
    else {return FALSE;}

} 

看看那个简单漂亮的代码。它让我们知道插入是否有效。

现在从您的模型数据库方法中查看

if (isset($data['product_id']) && is_array($data['product_id'])) {

不不不!和不!如果您将数据结构传递给数据库调用 - 您首先已经对其进行了清洁、抛光和打蜡。你知道它的完美和有效,所以你不必再用一堆混乱来猜测自己是这个集合吗?

终于永远不会命名任何东西:temp,因为 temp 是什么意思?回答:没有任何意义。那是个问题。虽然我在咆哮,但不要称它为 $data。仅为您传递给视图的值保留名称 $data。(好吧,我知道这可能不是一个流行的观点,但我保证它会让你的编码更容易)。方法和变量名真的很重要,它们是你代码之旅的路标:-)

于 2013-10-03T22:57:20.787 回答