SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    49
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question how to loop while array_unique in pdo

    anyone can solve my problem
    i have table like this
    +tbl_menu
    -menu_id
    -menu_name (news | tips | reviews)
    ...................
    +tbl_sub_menu
    -sub_id
    -menu_id
    -sub_name (phone, tablet, laptop.......)
    ...............
    i mean if i code in html it look like this
    <ul>
    <li>News</li>
    <li>
    <ul>
    <li>phone</li>
    <li>teble</li>
    <li>labtop</li>
    </ul>
    </li>
    <li>Tips</li>
    <li>
    <ul>
    <li>phone</li>
    <li>teble</li>
    <li>labtop</li>
    </ul>
    </li>
    </ul>
    PHP Code:
    $field "`tbl_menu`.`menu_name` , `tbl_sub_menu`.`sub_name`";
    $table "`tbl_menu` ,`tbl_sub_menu` WHERE `tbl_sub_menu`.`menu_id`= `tbl_menu`.`menu_id`";
    $statement=selecttest($field,$table);
     while (
    $row $statement->fetch(PDO::FETCH_ASSOC)) {
         echo 
    $row['menu_name']."|".$row['sub_name']."<br />";
     }
    echo 
    "<hr>";
    $row $statement->fetchAll(PDO::FETCH_ASSOC);
    $a array_unique($row);
    echo 
    "<pre>";
    print_r($a);
    echo 
    "<hr><br />";
    var_dump($a); 
    i want to use array_unique() because the database it duplicated result so any way to solve this problem.
    Regards,
    Heng

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    I guess the "duplicated result" is the menu name?
    You could do it like this
    PHP Code:
    // put the query results in a 2-dimensional array
    $menuArray = array();
    while (
    $row $statement->fetch(PDO::FETCH_ASSOC)) {
      
    $menuArray[$row['menu_name']][] =  $row['sub_name'];


    // let's dump the array to see what's in it
    var_dump($menuArray);

    // let's loop through the array and display the results
    foreach ($menuArray as $mainmenu => $submenuArray) {
      echo 
    $mainmenu '<br />';
      foreach (
    $submenuArray as $submenu) {
        echo 
    $submenu '<br />';
      }


  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    49
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Dear Guido,
    You're awesome honestly i'm not good at array now it work perfectly that what i wanted.
    Thank so much Guido i would give you gold medal you save my life
    Regards,
    Heng

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    49
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Dear Guido
    PHP Code:
    $field "`tbl_menu`.`menu_name` , `tbl_sub_menu`.`sub_name`,`tbl_sub_menu`.`sub_id`";
    $table "`tbl_menu` ,`tbl_sub_menu` WHERE `tbl_sub_menu`.`menu_id`= `tbl_menu`.`menu_id`";    
    $statement=selecttest($field,$table);
        
    $menuArray = array();
        while (
    $row $statement->fetch(PDO::FETCH_ASSOC)) {
            
    $menuArray[$row['menu_name']=>$row['menu_id']][] =  $row['sub_name'];
        }
        echo 
    '<div id="menu">';
        echo 
    '<ul id="nav">';
        foreach (
    $menuArray as $mainmenu => $submenuArray) {
            foreach (
    $mainmenu as $url){
        echo 
    '<li><a href="'.$url.'">'.$mainmenu.'</a><ul>';
        foreach (
    $submenuArray as $submenu) {
            echo 
    '<li><a href="'.$url.'">'.$submenu '</a></li>';
             }
        }
        echo 
    '</ul></li>';
    }
    echo 
    '</ul></div>'
    the variable $url is not working because i want to verify each sub menu have an unique menu id easy to calling the right article i know it gonna have 1 more problem when pass 2 parameter into <a href="article.php?menu_id=$row['menu_id']&sub_id=$row[sub_id]">$row['sub_name']</a>
    any good way better then this??
    Regards,
    Heng

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Sorry, I've no idea what you are trying to do. I do see that you made some changes to my code that won't work (as far as I can tell).

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    49
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    sorry guido now i found the solution thanks for your take time to reply
    Regards,
    Heng

  7. #7
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,033
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    You might be able to group up your results and purge redundant values at query time.

    Try this...

    Code php:
    $statement->fetchAll( PDO::FETCH_GROUP| PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC );

    And yes, that's legal - using bitwise or to set multiple PDO fetch style flags on the same fetchAll call.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •