Jump to content

[SOLVED] bundling sql results


P3t3r

Recommended Posts

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

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']
                                                );
}
?>

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?

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>";
    }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.