0

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" class="form-control select2 inventory-select" 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 ?

14
  • Are there more than 1 elements? Commented May 22, 2022 at 9:22
  • 1
    The [0] part is telling PHP you want the first element. If there are multiple, make that part dynamic, for example [$next]. Or use a loop if you want them all. Commented May 22, 2022 at 9:24
  • 1
    No, because you haven't actually shared your code. That is to say, I would need more information about the context that single line of code is executed in. Is it already in a loop? How do you know which item you want at that particular place? Commented May 22, 2022 at 9:25
  • 1
    Okay, but that doesn't appear to be where that line of code is. Can you share the code before and after that line where $id2 is defined? Commented May 22, 2022 at 9:30
  • 1
    No, you haven't. I still have no idea where that $id2=$locationProducts[0]->inventory_update_stock_details_id; line is in relation to the code fragments you've posted. Can you literally copy/paste that specific line plus, for example, the 5 lines of code before and after it? Commented May 22, 2022 at 9:36

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@ rickdenhaan. You are correct. But got the same result. The same id outs repeatedly, even select different items.
@ rickdenhaan. I am selecting only one item at a time using my view. Not to all InventoryLocationProducts at a time. So, needs to select one inventory_update_stock_details_id at a time internally to be inserted into the location_update_table. If I say in other words, that I needs to select item id (inventory_update_stock_details_id) from the item table as per form selecting of item to be inserted into location_update_table. (Left Join will be arranged)
I'm finding it difficult to follow, but it sounds like $id2 is actually the same as $this->input->post('inventory' . $x) then, isn't it? Because the value chosen there already came from inventory_update_stock_details_id.
@ rickdenhaan. Yes. That is.

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.