0

Basically I'm trying to group my arrays like this:

Shopping
   Amazon
Social
   Amoeblo
   American express

By using below PHP code:

<?php

echo '<ul id="list"><h2 class="searchresults"></h2>';
foreach($records as $catval) {
  $sitechar = $catval->site_category; 
  echo '<h3 id="disappear">'. strtoupper($sitechar) .'</h3>';       
  echo '<li class="siteli"><a href="#" class="add">'; 
  echo '<p id="text-site">'.$catval->site_name. '</p></a>';
  echo '</li>'; 
}
echo '</ul>';
?>

But I'm getting values only like below.

Shopping
       Amazon
Social
       Amoeblo
Social
       American express

I'm not getting the exact PHP sorting to use for this.

9
  • Try to put a variable $last_category which always store your last parent category. Then before showing parent category compare it with the last displayed one .. if they are the same .. don't display it again. Post the code here if you have issues. Commented Jan 9, 2013 at 10:45
  • 2
    Can you show the database query you're using to retrieve the data? Edit it into your question please. Commented Jan 9, 2013 at 10:47
  • Why did you accept it if does not worked? stackoverflow.com/questions/14233428/… Commented Jan 9, 2013 at 10:48
  • @PedroGabriel - I think you meant this link: stackoverflow.com/questions/14232483/… Commented Jan 9, 2013 at 10:50
  • you probably need GROUP BY clause in you Query Commented Jan 9, 2013 at 10:50

3 Answers 3

2

I would create a new array with categories as keys for arrays with the sites.

<?php
$arr = array();

// First create multidimensional array with categories as keys for site arrays
foreach($records as $catval) {
    $sitechar = $catval->site_category;
    if (!array_key_exists($sitechar, $arr)) {
        // Set new array for a category if it does not exist
        $arr[$sitechar] = array();
    }

    // Add site to category
    $arr[$sitechar][] = array(
        "name"=>$catval->site_name,
        "image"=>$catval->site_img
    );
}

// Then iterate the new array of categories
echo ("<ul>");
foreach($arr as $category => $sites) {
    echo("<h3>" . $site_category "</h3>");

    // Iterate array of sites
    foreach($sites as $site) {
        echo("<li>" . $site["name"] . "-" ,  $site["image"] . "</li>");
    }
}
echo("</ul>");
?>
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it using a foreach and ksort

Let $your_array be the array you mentioned above

$res_array    = array();
foreach($your_array as $val){
   $res_array[$val->site_category][] = $val->site_name;
}
ksort($res_array);

print_r($res_array);

OR search for multisort in php which will solve your problem :)

Comments

0
$tmp = null;

   echo '<ul id="list"><h2 class="searchresults"></h2>';
     foreach($records as $catval) {
       $myHtml = makeHtml($catval,$tmp);
       echo  $myHtml;
      $tmp = $catval->site_category;
     }
   echo '</ul>';


 function makeHtml($catval,$tmp){
       if($tmp != $catval->site_category){ $html .= '<h3 id="disappear">'. strtoupper($catval->site_category) .'</h3>';}
       $html .='<li class="siteli"><a href="#" class="add"><p id="text-site">'.$catval->site_name. '</p></a></li>'; 
       return $html;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.