views:

89

answers:

4

Very basic question.. but I'm missing the point.. I have this data on my table:

ID SITEID SECTION
1  1      posts
2  2      posts
3  1      aboutme
4  1      contact
5  2      questions

The output is an array. I can't change it.

I want to make this output on php with a single for loop with that array:

<h1> sections for site 1 </h1>
   posts
   aboutme
   contact

<h1>sections for site 2 </h1>
   posts
   questions

I'm trying to do something like this, where $sectionsArray is my output. And I want to check if siteid is the same, then make a loop..

for ($j = 0; $j < sizeof($sectionsArray); $j++) {
   while (siteid == 1) {
       echo '<h1>' . $sectionsArray['siteid'] . '</h1>';
   }
   echo "<a href='section.php?id=' . $sectionsArray['id'] . ' '">' . $sectionsArray['section'] . '</a>;
}

But I don't get the logic of "grouping" the results with a while.. INSIDE a loop. Any light will be welcome.

Thanks

+1  A: 

You can't really do this with just 1 loop, assuming you can't change the query. As you for loop through $sectionsArray, you have to deal with each row as you encounter it. It doesn't matter if you only want sections for site 1, you'll still encounter sections for site 2, 3, etc & either ignore them or deal with them.

If you can change your query, have it order by SITEID, then ID. That'll group all sections by site (note: this is not the same as using the SQL GROUP BY statement).

If you can't change your query, use your for loop to iterate through $sectionsArray once. In that loop, build a second array with the different sections ordered by site id. Iterate through that second array to output all the sections which are now in site id order.

Pickle
hm.. how can I build a second array with the different sections ordered by site id... that's the point
ookla
You can sort the array. I'll show how in an answer.
webbiedave
A: 

If you want to do one pass on the array, first sort (technically, that's also a pass!) and then check for a change in siteid as you loop through it. Here's how you can sort the array by siteid before looping through it:

$sections = array(
    array('id'=>1, 'siteid'=>1, 'section'=>'posts'),
    array('id'=>2, 'siteid'=>2, 'section'=>'posts'),
    array('id'=>3, 'siteid'=>1, 'section'=>'aboutme'),
    array('id'=>4, 'siteid'=>1, 'section'=>'contact'),
    array('id'=>5, 'siteid'=>2, 'section'=>'questions'),
);

usort($sections, 'sortBySiteId');

function sortBySiteId($a, $b)
{
    if ($a['siteid'] == $b['siteid']) {
        return 0;
    }
    return ($a['siteid'] < $b['siteid']) ? -1 : 1;
}
webbiedave
+1  A: 

Quick and dirty ;)

<?php
$arr = array(
    array('id' => 1, 'site_id' => '1', 'section' => 'posts'),
    array('id' => 2, 'site_id' => '2', 'section' => 'posts'),
    array('id' => 3, 'site_id' => '1', 'section' => 'aboutme'),
    array('id' => 4, 'site_id' => '1', 'section' => 'contact'),
    array('id' => 5, 'site_id' => '2', 'section' => 'questions')
);

$htmlStrings = array();
$curr_site_id = '0';

foreach($arr as $item)
{
    if ( $item['site_id'] != $curr_site_id )
    {
        $curr_site_id = $item['site_id'];
        if ( !isset($htmlStrings[$curr_site_id]) ) $htmlStrings[$curr_site_id]['header'] = '<h' . $curr_site_id . '>Sections for site ' . $curr_site_id . '</h' . $curr_site_id . '>';
        $htmlStrings[$curr_site_id]['content'] .= '<br />' . $item['section'];
    }
    else
    {
        $htmlStrings[$curr_site_id]['content'] .= '<br />' . $item['section'];
    }
}

foreach($htmlStrings as $section)
{
    echo $section['header'];
    echo $section['content'];
}
falomir
+1 shoving into an array of strings for later output is the way to go.
webbiedave
A: 

Hi guys, thanks.. but I ended up doing this:

for ($j=0;$j<sizeof($sectionArray);$j++) {

 $section_id = $sectionArray[$j]['id'];
 $section_name = $sectionArray[$j]['secao'];

  echo '<div id="menu">';
  echo '<div class="section">'.$section_name.'</div>';

  $categoryArray = $objectCategory->listCategory($section_id); // return my array from db     

    for ($ii=0;$ii<sizeof($categoryArray);$ii++) {
        $categoryId = $categoryArray[$ii]['id'];
        $categoryName = $categoryArray[$ii]['category'];

        echo "<div class=\"item\">";
        echo "<a href=\"category.php?id=$categoryId\">$categoryName</a>";
        echo "</div>";  
    }
  echo '</div>';    
}   

Is that ok? I mean, I have some problems with arrays... :(

But thanks anyway

ookla