Looping two arrays to make html table in php -
i have 2 array, 1 meant table heading , second meant table data.
table heading array
array ( [0] => techrepublic.com [1] => office.microsoft.com [2] => en.pstrecovery.net [3] => easeus.com [4] => download.cnet.com [5] => datanumen.com [6] => 2324 ) table data array
array ( [software services] => array ( [datanumen.com] => 2 [office.microsoft.com] => 3 [easeus.com] => 8 [download.cnet.com] => 9 [en.pstrecovery.net] => 10 ) [software services in delhi] => array ( [en.pstrecovery.net] => 1 [datanumen.com] => 3 [office.microsoft.com] => 4 [easeus.com] => 9 [download.cnet.com] => 10 ) [orignal software services] => array ( [en.pstrecovery.net] => 1 [easeus.com] => 6 [download.cnet.com] => 7 [datanumen.com] => 8 [office.microsoft.com] => 9 ) )
i want output table shown in attachment
what have done till
// code table heading foreach ( $_post['competitor']) $value) { // $_post['competitor']) // containts table heading // array echo "<th>". $value ."</th>" ; } //code table row foreach ($details $keywords => $comp) { // $details contains row // data array echo '<tr><td>' .$keywords . '</td>'; foreach ($_post['competitor'] $competitor) { if (isset($comp[$competitor])) { echo '<td>' . $comp[$competitor] . '</td>' ; } else { echo '<td>not found</td>' ; } } echo '</tr>' ; } i unable arrange td , tr. how can achieve output shown in attachment?
the thing is, in order data in same order headers, need loop through headers again each row in data. since data uses headers array keys, should pretty easy.
foreach ( $_post['competitor'] $value) { echo "<th>". htmlspecialchars($value) ."</th>" ; } foreach ($details $keywords => $comp) { echo '<tr><td>' . htmlspecialchars($keywords) . '</td>'; // iterate headers here instead foreach ($_post['competitor'] $competitor) { // echo value header current row if it's present if (isset($comp[$competitor])) { echo '<td>' . htmlspecialchars($comp[$competitor]) . '</td>' ; } else { echo '<td>not found</td>' ; } } echo '</tr>' ; } side note - don't forget escape strings html output.
wiki

Comments
Post a Comment