Home > database >  PHP Array index value
PHP Array index value

Time:05-23

I have a simple question that needs to get an element when selecting that reference ID by an input form. I used the following code to do that.

$id2=$locationProducts[0]->inventory_update_stock_details_id;

But this code outs only the first element always. AS an exmple inventory_update_stock_details_id=36, inventory_update_stock_details_id=36, inventory_update_stock_details_id=36 and so on.

But I needs to select as inventory_update_stock_details_id=35, inventory_update_stock_details_id=345, inventory_update_stock_details_id=2 like that.

inventory_update_stock_details_id included ids as 1,2,3,4,5,......1000

The model as follows :

function getInventoryLocationProducts()
    {
        $this->db->select("*");
        $this->db->from('store_inventory_update_stock_details');
        $this->db->join('store_inventory_item', 'store_inventory_item.inventory_item_id=store_inventory_update_stock_details.inventory','left');
        $this->db->join('store_inventory_location_update', 'store_inventory_location_update.inventory_update_stock_id=store_inventory_update_stock_details.inventory_update_stock_id','left');
        $this->db->join('store_inventory_update_stock', 'store_inventory_update_stock.inventory_update_stock_id=store_inventory_location_update.inventory_update_stock_id','left');
        $this->db->where('store_inventory_item.status=1');
        $this->db->where('store_inventory_update_stock_details.serial_no NOT IN (select serial_no from store_inventory_location_update)');
        
        $q = $this->db->get_where();
        
        if ($q->num_rows() > 0) {
            return $q->result();
        }

        return FALSE;
    }

Controller

public function addInventoryLocation()
{
    $this->checkPermissions('add', 'inventoryLocation');
    $bc = array(array('link' => '#', 'page' => 'Inventory Locations'));
    $meta = array('page_title' => 'Inventory Locations', 'bc' => $bc);
    $this->data['branch'] = $this->Item_model->getBranch();
    $this->data['office'] = $this->Item_model->getOffice();
    $locationProducts = $this->Item_model->getInventoryLocationProducts();
    $this->data['locationProducts'] = $locationProducts;

    $this->data['auto_loc_no'] = $this->Item_model->generate_inv_loc_ref();

    $count = $this->input->post('i');
    
    $str = $this->input->post('auto_loc_no');
    $auto_loc = preg_replace("/[^0-9]{1,4}/", '', $str);
    $this->form_validation->set_rules('branch', 'Branch', 'required');
            
    if ($this->form_validation->run() == true) {
        
        $stock = array(
            
            'office_id' => $this->input->post('office'),
            'branch' => $this->input->post('branch'),
            'l_date' => $this->input->post('l_date'),
            'is_order_no' => $this->input->post('is_order_no'),               
            'loc_no' => $this->input->post('auto_loc_no'),
            'auto_loc_no' => $auto_loc,           
            'user' => ucfirst($this->session->userdata('name')),
            'order_status' => 'locate',
            'status' => 1
        );
        $id = $this->Item_model->addInventoryLocation($stock);
        
                   
    }
    $id2=$locationProducts[0]->inventory_update_stock_details_id;       
    dd($id2);

        if ($this->form_validation->run() == true && $id2 != 0) {
            $count = $this->input->post('i');
            for ($x = 0; $x <= $count; $x  ) {
                $details[$x]['inventory_update_stock_id'] = $id2;                    
                $details[$x]['inventory'] = $this->input->post('inventory' . $x);                   
                $details[$x]['serial_no'] = $this->input->post('serial_no' . $x);
                $details[$x]['item_code'] = $this->input->post('item_code' . $x);
                $details[$x]['officer'] = $this->input->post('officer' . $x);
                $details[$x]['qty'] = 1;                    
                $details[$x]['status'] = 1;
                $details[$x]['branch'] = $this->input->post('branch');
            }

        if ($this->Item_model->addInventoryLocationDetails($details)) {
            echo("<meta http-equiv='refresh' content='1'>"); 
            $this->session->set_flashdata('message', 'Successfully Added ..!!');                              
            redirect('item/addInventoryLocation');
        }
    } else {
        
        $this->session->set_flashdata('error', validation_errors());
        $this->render('item/addInventoryLocation', $meta, $this->data);
    }

}

View

<td style="width: 30%">
                                <input type="hidden" name="i" id="i" value="0">
                                <select name="inventory0" id="inventory0"  required>
                                    <option value=""></option>
                                    <?php
                                    if (!empty($locationProducts)) {
                                        foreach ($locationProducts as $row) {
                                                echo "<option value='".$row->inventory_update_stock_details_id."'> $row->inventory_update_stock_details_id - $row->inventory_item_name</option>";
                                            ?>
                                            
                                           
                                            <?php
                                        }
                                    }

                                    ?>
                                </select>
                            </td>

What can modify my code line to do that. Can anyone help me ?

CodePudding user response:

I'm not entirely sure what the intended logic is, but I think what you want is to add the InventoryLocationDetails to all InventoryLocationProducts.

If that is correct, you could perhaps do something like this:

    if ($this->form_validation->run() == true) {
        $details = [];

        foreach ($locationProducts as $locationProduct) {
            $id2 = $locationProduct->inventory_update_stock_details_id;

            $count = $this->input->post('i');
            for ($x = 0; $x <= $count; $x  ) {
                $details[] = [
                    'inventory_update_stock_id' => $id2,
                    'inventory' => $this->input->post('inventory' . $x),
                    'serial_no' => $this->input->post('serial_no' . $x),
                    'item_code' => $this->input->post('item_code' . $x),
                    'officer' => $this->input->post('officer' . $x);
                    'qty' => 1,
                    'status' => 1,
                    'branch' => $this->input->post('branch'),
                ];
            }
        }

        if ($this->Item_model->addInventoryLocationDetails($details)) {
            echo("<meta http-equiv='refresh' content='1'>"); 
            $this->session->set_flashdata('message', 'Successfully Added ..!!');                              
            redirect('item/addInventoryLocation');
        }
    }

This assumes that $locationProducts contains only those products that actually need to be updated. If you need to only update some of them, you can add the logic to determine which product to update inside the foreach loop.

  • Related