P3t3r Posted January 1, 2008 Share Posted January 1, 2008 I have a script doing $sql = 'SELECT * FROM table1 WHERE page_id = '.$page_id.' ORDER BY section ASC, displaynumber ASC'; if (!$result = $db->sql_query($sql)) { message_die(GENERAL_ERROR, 'Could not query resource page information', '', __LINE__, __FILE__, $sql); } $page_count = $db->sql_numrows($result); $page_row = array(); while ($row = $db->sql_fetchrow($result)) { $page_row[] = $row; } Which logically returns me a $page_row like Array ( [0] => Array ( [post_id] => 6 [displaynumber] => E198 [section] => EWE ) [1] => Array ( [post_id] => 2 [displaynumber] => G12 [section] => EWE ) [2] => Array ( [post_id] => 3 [displaynumber] => G13 [section] => RSE ) [3] => Array ( [post_id] => 5 [displaynumber] => R89 [section] => RSE ) ). I would like to bundle the results by section, i.e. return a $page_row like Array ( [0] => Array ( [0] => 'EWE', [1] => Array ( [post_id] => 6 [displaynumber] => E198) , [2] => Array ( [post_id] => 2 [displaynumber] => G12 ) ) [1] => Array ( [0] => 'RSE', [1] => Array ( [post_id] => 3 [displaynumber] => G13 ) , [2] => Array ( [post_id] => 5 [displaynumber] => R89) ) ) How would I do this best? Also, within the subarrays I'd also like to have them sorted on displaynumber. Thank in advance and a happy new year! Link to comment https://forums.phpfreaks.com/topic/83929-solved-bundling-sql-results/ Share on other sites More sharing options...
Barand Posted January 1, 2008 Share Posted January 1, 2008 try <?php $sql = 'SELECT * FROM table1 WHERE page_id = '.$page_id.' ORDER BY section ASC, displaynumber ASC'; if (!$result = $db->sql_query($sql)) { message_die(GENERAL_ERROR, 'Could not query resource page information', '', __LINE__, __FILE__, $sql); } $page_count = $db->sql_numrows($result); $page_row = array(); while ($row = $db->sql_fetchrow($result)) { $page_row[$row['section']][] = array ( 'post_id' => $row['post_id'], 'displaynumber' => $row['displaynumber'] ); } ?> Link to comment https://forums.phpfreaks.com/topic/83929-solved-bundling-sql-results/#findComment-427197 Share on other sites More sharing options...
P3t3r Posted January 3, 2008 Author Share Posted January 3, 2008 Thanks! Now this might be a stupid question, but... how to find out which sections there are in the array? I mean, I can't call $page_row['EWE'] in my code since I don't know its name. How to extract this most efficiently? Make second array $sections and fill them with $row['section'] in the while-loop? Or are there better ways? Link to comment https://forums.phpfreaks.com/topic/83929-solved-bundling-sql-results/#findComment-429036 Share on other sites More sharing options...
Zane Posted January 3, 2008 Share Posted January 3, 2008 I'm not too sure about your second question but foreach($page_row as $key=>$value) echo $key . " "; should show you all the sections you have in this particular array assuming you used what Barrand gave you Link to comment https://forums.phpfreaks.com/topic/83929-solved-bundling-sql-results/#findComment-429046 Share on other sites More sharing options...
Barand Posted January 3, 2008 Share Posted January 3, 2008 try <?php foreach ($page_row as $section => $sectdata) { echo $section . '<br>'; foreach ($sectdata as $data) { echo "Post ID : {$data['post_id']}<br>Display : {$data['displaynumber']}<br>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/83929-solved-bundling-sql-results/#findComment-429111 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.