0

Sorry but I dont know how to explain my problem but rather I will demonstrate my problem here.

This is what I get when I used print_r()

Array ( [0] => 2     [1] => 200 ) 
Array ( [0] => 5000  [1] => 1000 )  
Array ( [0] => 2     [1] => 200 ) 
Array ( [0] => 5000  [1] => 1000 ) 
Array ( [0] => 2     [1] => 200 ) 
Array ( [0] => 5000  [1] => 1000 )  
Array ( [0] => 2     [1] => 200 ) 
Array ( [0] => 5000  [1] => 1000 )  
Array ( [0] => 4     [1] => 300 ) 
Array ( [0] => 10000 [1] => 1500 )  
Array ( [0] => 4     [1] => 300 ) 
Array ( [0] => 10000 [1] => 1500 )  
Array ( [0] => 4     [1] => 300 ) 
Array ( [0] => 10000 [1] => 1500 )  
Array ( [0] => 4     [1] => 300 ) 
Array ( [0] => 10000 [1] => 1500 ) 
Array ( [0] => 3     [1] => 100 ) 
Array ( [0] => 7500  [1] => 500 ) 
Array ( [0] => 3     [1] => 100 ) 
Array ( [0] => 7500  [1] => 500 ) 
Array ( [0] => 3     [1] => 100 ) 
Array ( [0] => 7500  [1] => 500 )  
Array ( [0] => 3     [1] => 100 ) 
Array ( [0] => 7500  [1] => 500 )

And this is my code

foreach ($supp_dtl_1 as $key => $value) {
    $arr = explode(',',$value->unit_price);
    $arr1 = explode(',',$value->total_amount);
    foreach($arr as $cell){
        foreach($arr1 as $cell1){
            //echo print_r($arr);
            //echo print_r($arr1);
            <td><input type="text" value="<?php echo $cell; ?>"></td>
            <td><input type="text" value="<?php echo $cell1; ?>"></td>
        }
    }
}

This is the result of the above code

2 | 5000 | 2 | 1000 | 200 | 5000 | 200 | 1000 | 4 | 10000 | 4 | 1500 | 300 | 10000 | 300 | 3 | 7500 | 3 | 500 | 100 | 7500 | 100 | 500
2 | 5000 | 2 | 1000 | 200 | 5000 | 200 | 1000 | 4 | 10000 | 4 | 1500 | 300 | 10000 | 300 | 3 | 7500 | 3 | 500 | 100 | 7500 | 100 | 500

The Expected output should be something like this

2   | 5000 |  3    | 7500   | 4   | 10000
200 | 1000 |  100  | 500    | 300 | 1500  

This is my data in db and the result of my query.

Name    |   unit_price  |   total_amount
j1      |   2           |   5000
j1      |   200         |   1000
j2      |   3           |   7500
j2      |   100         |   500
j3      |   4           |   10000
j3      |   300         |   1500

Please see the image Click me supp_dtl_1 print_r() Click me

Expected Output Click Me Database Click Me

4
  • by the way, I am using group_concat in my query. That's why Im using explode Commented Jan 24, 2019 at 8:57
  • It may be worth looking at your SQL as it would be better to just fetch the right data than try and fix the results. Commented Jan 24, 2019 at 8:58
  • This is my data in db and the result of my query. Name | unit_price | total_amount j1 | 2 | 5000 j1 | 200 | 1000 j2 | 3 | 7500 j2 | 100 | 500 j3 | 4 | 10000 j3 | 300 | 1500 Commented Jan 24, 2019 at 9:04
  • please see above my updated post. I put there the result of my query Commented Jan 24, 2019 at 9:06

2 Answers 2

1

I don't really know what exactly you want, but I hope the following code will help. You don't need to nest your second array with another for loop, instead, you can iterate it under the same loop as the first array with $key.

foreach($arr as $key => $cell){
        //echo print_r($arr);
        //echo print_r($arr1[$key]);
        <td><input type="text" value="<?php echo $cell; ?>"></td>
        <td><input type="text" value="<?php echo $arr1[$key]; ?>"></td>
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Almost there. But there it duplicates the output.
I added image in my post. That's the result using your code.
0

Try this

<?php
foreach ($supp_dtl_1 as $key => $value) {
    $arr = explode(',',$value->unit_price);
    $arr1 = explode(',',$value->total_amount);
    foreach($arr as $key2 => $cell){
        echo '<td><input type="text" value="'.$cell.'"></td>';
        echo '<td><input type="text" value="'.$arr1[$key2].'"></td>';
    }
}

21 Comments

I added image in my post. That's the result using your code.
If you make a print_r on $supp_dtl_1 before the foreach, whats the result?
Please see image above. I added there.
If i hard code the $supp_dtl_1 to be $supp_dtl_1 = array( (object) array( 'unit_price' => 'u1', 'total_amount' => 't1' ), (object) array( 'unit_price' => 'u2', 'total_amount' => 't2' ), (object) array( 'unit_price' => 'u3', 'total_amount' => 't3' ) ); it works fine @solomon
Have you tried using my $supp_dtl_1? as you can see in the image I post.
|

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.