0

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

5
  • You need to show us what you tried so far. This is not a tutorial site to get the basics explained. Commented Nov 25, 2019 at 7:54
  • $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++; } } } Commented Nov 25, 2019 at 8:03
  • @04FS above code i have tried so far please check Commented Nov 25, 2019 at 8:03
  • Extended pieces of code are hardly readable in comments. Please edit this into the question instead (and format it properly.) And also give a proper problem description - explain what you want the code to do, and how exactly it is not doing that. Commented Nov 25, 2019 at 8:05
  • i have added code what i had tried @ 04FS Commented Nov 25, 2019 at 9:16

1 Answer 1

1

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>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much i really appreciate for your help the solution you provided is working well @CaffeinatedCod3r
I am glad that i could help. Happy Coding !!

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.