Reputation: 35
i dont know wheather this question already asked and answered it but i have search many things but did not get desired output of my problem
i have table like this
Name | desc | dep <br />
--------------------------------
abc | content | editing <br />
xyz | content | document <br />
pqr | content | marketing <br />
lcv | content | scripting <br />
khg | content | writer <br />
asd | content | finalize <br />
frt | content | checker <br />
in my php code i have created multidimensional array with single key with multiple values
$arr1 = array
(
'first' => array('abc','lcv','asd'),
'second' => array('xyz','pqr','khg','frt')
);
$RowNumber=1;
for ($row = 0; $row < count($arr1); $row++) {
$cols = count($arr1[$row]);
for ($col = 0; $col < $cols; $col++) {
$result = mysqli_query($db,"SELECT * FROM tbl_data;");
while ($rows = mysqli_fetch_array($result)) {
echo '<tr>'; echo ' <td>'.$RowNumber.'</td> <td>'.$keyname.'</td> <td>'.$rows['Name'].'</td> <td>'.$rows['desc'].'</td> <td>'.$rows['dep'].'</td>'; echo '</tr>'; $RowNumber++; } } }
i want to create seperate table for each key 'first' with all three values similarly for next key but in new table and so on so my desired output will be look like this 'first' key table
No | keyname | Name | desc | dep <br />
--------------------------------------------------------
1 | first | abc | content | editing <br />
2 | first | lcv | content | scripting <br />
3 | first | asd | content | finalize <br />
'Second' key table
No | keyname | Name | desc | dep <br />
--------------------------------------------------------
1 | second | abc | content | editing <br />
2 | second | lcv | content | scripting <br />
3 | second | asd | content | finalize <br />
whenever i create new key suppose 'third' in multidimensional array the table will get generate automatically for that 'third' key also
so how i am going to achieve my desired output any help would be appreciated please help me thanks
Upvotes: 0
Views: 52
Reputation: 884
It is an associative array and you can traverse it using foreach
loop.
foreach ( $arr1 as $key=>$val) {
echo '<table>';
foreach ( $val as $name){
$result = mysqli_query($db,"SELECT * FROM tbl_data where Name = $val;");
while ($rows = mysqli_fetch_array($result)) {
echo '<tr>';
echo ' <td>'.$RowNumber.'</td>';
echo ' <td>'.$keyname.'</td>';
echo ' <td>'.$rows['Name'].'</td>';
echo ' <td>'.$rows['desc'].'</td>';
echo ' <td>'.$rows['dep'].'</td>';
echo '</tr>';
}
}
echo '</table>';
}
Upvotes: 1