1

so I have this array

array (size=17)
'id' => string '21' (length=2)
'request_area' => string 'Area 1' (length=6)
'ship_product_code' => string '795919' (length=6)
'ship_product_description' => string 'BULBs' (length=5)
'qty' => string '2.0000' (length=6) 

 array (size=17)
'id' => string '22' (length=2)
'request_area' => string 'Area 1' (length=6)
'ship_product_code' => string '123' (length=3)
'ship_product_description' => string '321s' (length=4)
'qty' => string '3.0000' (length=6)

those arrays are inside

$product 

I loop the product array and echo a table using following code

if(count($product)>0){
            $i=0;
            foreach($product as $v){
                $i++;
                $CONTENT.= '
                    <tr> 
                        <td align="center" width="7%"> </td>
                        <td align="center" width="50%">'.$v['request_area'].'</td>
                    </tr>
                    <tr>
                        <td align="center" width="7%">'.$i.'</td>
                        <td align="center" width="50%">'.$v['ship_product_description'].'</td>
                        <td align="center" width="7%">'.number_format($v['qty']).'</td>
                        <td width="7%"> </td>
                    </tr>
                ';
            }
        }

The problem from above code is, it will create a tr with

 Request Area
 Item 1
 Request Area
 Item 2

What I want is, it prints the request area only once, and then followed by the Items. How can I achieve it?

 Request Area //only once
 Item 1
 Item 2
 Item 3 
 // and so on

Thank you :)

2
  • it would be helpful to have the code for the arrays (the 2 upper arrays look like they could be json) Commented Aug 18, 2016 at 10:05
  • the $product array is a query result from the database sir :) Commented Aug 18, 2016 at 10:07

1 Answer 1

2

According to your question if you try the following it will give you your desired results:

   if(count($product)>0){
        $i=0;
        foreach($product as $v){
            if($i == 0){
                $CONTENT .= '<tr> 
                    <td align="center" width="7%"> </td>
                    <td align="center" width="50%">'.$v['request_area'].'</td>
                </tr>';
            }
            $i++;
            $CONTENT.= '

                <tr>
                    <td align="center" width="7%">'.$i.'</td>
                    <td align="center" width="50%">'.$v['ship_product_description'].'</td>
                    <td align="center" width="7%">'.number_format($v['qty']).'</td>
                    <td width="7%"> </td>
                </tr>
            ';
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

this is really a great "hack" ! you are awesome Sir! wonder why I can't think of such way. thank you!

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.