1

Basically this is my array

Array
(
    [0] => Array
        (            
            [mainProdName] => CellPhone           
            [mainproductname] => Array
                (
                    [0] => Array
                        (   
                            [subProdName] => TG8                           
                        )

                    [1] => Array
                        (  
                            [subProdName] => TG5                          
                        )

                )

        )

    [1] => Array
        (
            [mainProdName] => LapTop
            [mainproductname] => Array
                (
                    [0] => Array
                        (   
                            [subProdName] => TD9                          
                        )

                    [1] => Array
                        (                           
                            [subProdName] => TD7                            
                        )

                )

        )

    [2] => Array
        ( 
            [mainProdName] => Corn            
            [mainproductname] => Array
                (
                    [0] => Array
                        (                           
                            [subProdName] => SWEET CORN                            
                        )

                )

        )
)

I am trying to display this result in an HTML table

<table><tr>
    ?>
        for($j=0;$j<count($res);$j++)
        {
            ?><td>$res[$i].mainProdName}</td><?php
            for($k=0;$k<count($res[$i].mainproductname);$k++)
            {
                ?><td>$res[$i].mainproductname[$k].subProdName}</td><?php
            }
        }
    <?php
</tr></table>

For which i'm getting this.

----------------------------------------------------------------
CellPhone | TG8 | TG5 | LapTop | TD9 | TD7 | Corn | SWEET CORN  
----------------------------------------------------------------

Basically i need to display the result in this format

-------------------------------------------------------------------
       CellPhone       |        LapTop          |       Corn
-----------------------|------------------------|------------------
   TG8     |    TG5    |    TD9    |     TD7    |    SWEET CORN  
-------------------------------------------------------------------

For every main product, respective subproduct list should be displayed below in a separate row.

2
  • this is just a simple html mark-up question imho. Commented Feb 17, 2016 at 7:42
  • MATAR please check the answer below and check which one is correct for you. Commented Feb 17, 2016 at 7:49

4 Answers 4

1

You can try this -

$first_tr = $second_tr = "<tr>";

foreach($array as $result) {

    // Print the first columns of first row with colspan
    $first_tr .= "<td colspan='" . count($result['mainproductname']) . "'>" . $result['mainProdName'] . "</td>";

    // Loop through the sub-products and print them
    foreach($result['mainproductname'] as $name) {
        $second_tr .= "<td>" . $name['subProdName'] . "</td>";
    }

}

echo $first_tr . "</tr>" . $second_tr . "</tr>"; 

Demo

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

Comments

1
 <table>
 <thead>
    <tr>
    <?php for($i=0;$i<count($res);$i++){ ?>
        <th><?php echo $res[$i].mainProdName;?></th>
    <?php } ?>    
    </tr>
 </thead>
 <tbody>
 <?php for($i=0;$i<count($res);$i++){ ?>
 <tr>
    <?php for($j=0;$j<count($res);$j++){ ?>
    <td><?php echo $res[$i]["mainproductname"]['subProdName'][$j]; ?></td>
    <?php } ?>
 </tr>
 <?php } ?>
 </tbody>
 </table>  

Comments

1

Please try this

$html ="<table border='1'>";
$html.="<tr>";
$innerhtml="<tr>";
foreach($arr as $key=>$valArr){

    $html.="<td>".$valArr['mainProdName']."</td>";
    if(count($valArr['mainproductname'])){
        $subValArr =array();
        foreach($valArr['mainproductname'] as $subKey => $subVals){
            $subValArr[] = $subVals['subProdName'];

        }
        $subArrString = implode("|", $subValArr);
        $innerhtml.="<td>".$subArrString."</td>" ;
    }


}
$innerhtml.="</tr>";
$html.=$innerhtml;
$html.="</tr>";
$html.="</table>";
echo $html;

Output :

enter image description here

Comments

-1

i m using this format to show php result in html table format

$con=mysql_connect("localhost","root");
mysql_select_db("aggregate",$con);
$q=mysql_query("SELECT ASCII('A');");
echo "<h2>ASCII Function</h2>";
echo "<br>";
echo "<table border=1>";
    echo "<tr><th> ASCII Function</th></tr>";


while($qq=mysql_fetch_row($q)){

    echo "<tr><td> ".$qq[0]. "</td></tr>";
}
echo "</table>";

1 Comment

This doesn't answer the question in anyways.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.