shadd Posted September 20 Share Posted September 20 (edited) I have this resultset : pId Duration emdate Sub SubId Lvl LvId 1.1 null null basic lug 1 ule 1 1.2 null null basic lug 1 ule 1 1.2 null null ger 2 ace 1 I would like to have it display like so in a <ul>: ULE BASIC LUG BASIC LUG ACE ger how can I achieve that if each is a row from database resultset. I have tried ; foreach($ScheduledExamlist as $Scheduledexam) { echo '<li class="current"><a href="#">'.$Scheduledexam['Lvl'].'</a></li>' ; echo '<li>'.$Scheduledexam.'</li>'; } but heading appears twice Edited September 20 by shadd clarity Quote Link to comment Share on other sites More sharing options...
Barand Posted September 20 Share Posted September 20 And what are you wanting to do with that table? BTW, which values belong to which columns (do you have a problem with your keyboard's spacebar? If so you could have used the "table" button in the toolbar) What code have you tried so far? Quote Link to comment Share on other sites More sharing options...
jodunno Posted September 20 Share Posted September 20 where is your database code? it would be nice to see your query in addition to expected results. Quote Link to comment Share on other sites More sharing options...
shadd Posted September 20 Author Share Posted September 20 10 minutes ago, jodunno said: where is your database code? it would be nice to see your query in addition to expected results. here is the array that i need to print out in a <ul> : array ( 0 => array ( 'pId' => '1.1', 'Duration' => NULL, 'emdate' => NULL, 'Sub' => 'BASIC lug', 'SubId' => 1, 'Lvl' => ' ULE', 'LvId' => 1, ), 1 => array ( 'pId' => '1.2', 'Duration' => NULL, 'emdate' => NULL, 'Sub' => 'BASIC lug', 'SubId' => 1, 'Lvl' => ' ULE', 'LvId' => 1, ), 2 => array ( 'pId' => '1.3', 'Duration' => NULL, 'emdate' => NULL, 'Sub' => 'ger', 'SubId' => 1, 'Lvl' => ' ACE', 'LvId' => 2, ) ) iam expecting to get this result; ULE BASIC LUG BASIC LUG ACE ger Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 20 Share Posted September 20 you need to index/pivot the data when you fetch it, using the Lvl value as the main array index. you can then simply loop over the data using two nested foreach loops to produce the output, like was shown in your previous similar thread. Quote Link to comment Share on other sites More sharing options...
shadd Posted September 20 Author Share Posted September 20 (edited) 29 minutes ago, mac_gyver said: you need to index/pivot the data when you fetch it, using the Lvl value as the main array index. you can then simply loop over the data using two nested foreach loops to produce the output, like was shown in your previous similar thread. can you please, show your concept on that array using php Edited September 20 by shadd Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted September 20 Solution Share Posted September 20 assuming you are using the PDO extension, it has a fetch mode that will index/pivot the data for you - // build and execute the query here... // index/pivot the fetched data using the first column in the SELECT list $data = $stmt->fetchAll(PDO::FETCH_GROUP); // at the point of producing the output if(!$data) { echo "<p>There is no data to display</p>"; } else { foreach($data as $index=>$items) { // output the section heading echo "<li class='current'><a href='#'>$index</a></li>\n"; // loop over the data in the section foreach($items as $row) { // output each row of data echo "<li>{$row['Sub']}</li>\n"; } // if needed, close the section here... } } Quote Link to comment Share on other sites More sharing options...
Barand Posted September 20 Share Posted September 20 ... or ... $res = $pdo->query("SELECT Lvl , Sub FROM shadd_1 ORDER BY Lvl DESC, Sub "); $results = $res->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_COLUMN); foreach ($results as $lvl => $arr) { echo "$lvl <UL>"; foreach ($arr as $sub) { echo "<li>$sub</li>"; } echo "</ul>"; } FYI - the $results array (slightly simpler than the $data array in above post) looks like this.. Array ( [ULE] => Array ( [0] => BASIC lug [1] => BASIC lug ) [ACE] => Array ( [0] => ger ) ) 1 Quote Link to comment Share on other sites More sharing options...
shadd Posted September 20 Author Share Posted September 20 (edited) 1 hour ago, Barand said: ... or ... $res = $pdo->query("SELECT Lvl , Sub FROM shadd_1 ORDER BY Lvl DESC, Sub "); $results = $res->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_COLUMN); foreach ($results as $lvl => $arr) { echo "$lvl <UL>"; foreach ($arr as $sub) { echo "<li>$sub</li>"; } echo "</ul>"; } FYI - the $results array (slightly simpler than the $data array in above post) looks like this.. Array ( [ULE] => Array ( [0] => BASIC lug [1] => BASIC lug ) [ACE] => Array ( [0] => ger ) ) correct and simple but,is it possible to add a key to the results array like so: foreach ($results as $lvl => $arr) { echo "$lvl <UL>"; foreach ($arr as $sub) { $results['go_to_exam'] ="dear";echo "<li>$sub</li>"; } echo "</ul>"; } in a for loop this could work: for ($i = 0; $i < count($results); $i++) { $results[$i]['go_to_exam'] = "dear".$i; } if i had used fetch:assoc Edited September 20 by shadd Quote Link to comment Share on other sites More sharing options...
Barand Posted September 20 Share Posted September 20 If you want to add another key to the results array add it to the query, and use @mac_gyver method. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.