user876200
user876200

Reputation:

for loop problem

function cartinsert() {
    $num = $this->input->post('numOflim');
    $numb = count($num);
    echo $num;
    for ($x =1; $x <= $numb; $x++) {
       $quanoutput = $this->input->post('quanoutput');
       $barcodeoutput = $this->input->post('barcodeoutput');
       $productsoutput = $this->input->post('productsoutput');
       $buyprice = $this->input->post('buyprice');
       $outward_date=$this->input->post('outward_date');
       $stock=$this->input->post('stock');
       $warehouse_id =$this->input->post('warehouse_id');
       $request_id =$this->input->post('request_id');
       $warehouse=$this->input->post('warehouse');
       $flag2 = $productsoutput;
       $undefined = 'undefined';
       if ($flag2 == $undefined) {
           $flag3 = $this->cartmodel->cartInsert($quanoutput,$barcodeoutput,$productsoutput);
       } else {
           $flag3 = $this->cartmodel->cartInsert( $barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
       }
    }
}

I am just able to get only the first row and rest other rows are not being displayed

Upvotes: 0

Views: 105

Answers (2)

afuzzyllama
afuzzyllama

Reputation: 6548

The element inside your for loop doesn't have an index.

The line $quanoutput = $this->input->post('quanoutput'); in code igniter is only going to fetch the element $_POST['quanoutput']

You need to index your post by doing something like this in your input:

<input name='quanoutput[1]' ... >
<input name='quanoutput[2]' ... >
<input name='quanoutput[{NUM}]' ... >

where {NUM} is an index you can fetch like this:

for ($x = 1; $x <= $numb; $x++) {
    $quanoutput[] = $_POST['quanoutput'][$x];
}

PRO TIP: even though I am indexing from 1 because that is what you have done, it is usually good practice to index from 0.


Also, as mattumotu's answer points out, your count does only return 1 so it would also make sense that your loop would only be running one time as well.

Upvotes: 1

mattumotu
mattumotu

Reputation: 1504

look at the code

$num = $this->input->post('numOflim');
$numb = count($num);
echo $num;
for ($x =1; $x <= $numb; $x++) {

you say $num is 10? Well count(10) = 1, so the loop is for x = 1 to x<= 1, ie it only happens once!

Upvotes: 3

Related Questions