Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.