Reputation: 2422
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.
Upvotes: 1
Views: 134
Reputation: 1265
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 :
Upvotes: 1
Reputation: 31739
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>";
Upvotes: 1
Reputation: 468
<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>
Upvotes: 1
Reputation: 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>";
Upvotes: -1