PHP Array Generation Problem

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8415
  • Loc: USA

Post 3+ Months Ago

I have this menu that is dynamically created from an SQL database which I'm having problems with. Below is the database:
SQL Code: [ Select ]
CREATE TABLE IF NOT EXISTS `cms_navigation` (
  `navID` int(11) NOT NULL AUTO_INCREMENT,
  `parentID` int(11) DEFAULT NULL,
  `nav_name` tinytext NOT NULL,
  `nav_href` varchar(100) NOT NULL,
  PRIMARY KEY (`navID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11;
 
INSERT INTO `cms_navigation` (`navID`, `parentID`, `nav_name`, `nav_href`) VALUES
(3, NULL, 'INDEX', '#'),
(4, NULL, 'SOMETHING', '#'),
(5, 4, 'Link 1', '#'),
(6, 4, 'Link 2', '#'),
(7, 4, 'Link 3', '#'),
(8, 4, 'Link 4', '#'),
(9, NULL, 'ANYTHING', '#'),
(10, 9, 'Link 1', '#');
  1. CREATE TABLE IF NOT EXISTS `cms_navigation` (
  2.   `navID` int(11) NOT NULL AUTO_INCREMENT,
  3.   `parentID` int(11) DEFAULT NULL,
  4.   `nav_name` tinytext NOT NULL,
  5.   `nav_href` varchar(100) NOT NULL,
  6.   PRIMARY KEY (`navID`)
  7. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11;
  8.  
  9. INSERT INTO `cms_navigation` (`navID`, `parentID`, `nav_name`, `nav_href`) VALUES
  10. (3, NULL, 'INDEX', '#'),
  11. (4, NULL, 'SOMETHING', '#'),
  12. (5, 4, 'Link 1', '#'),
  13. (6, 4, 'Link 2', '#'),
  14. (7, 4, 'Link 3', '#'),
  15. (8, 4, 'Link 4', '#'),
  16. (9, NULL, 'ANYTHING', '#'),
  17. (10, 9, 'Link 1', '#');

And below is the PHP that generates the array:
PHP Code: [ Select ]
$sql = "SELECT * FROM `cms_navigation`";
$results = mysql_query($sql);
 
$navigation = array();
$i = 1;
while(list($navID, $parentID, $nav_name, $nav_href) = mysql_fetch_row($results))
{
   if($parentID != false || $parentID != null)
   {
      $current_parent_id = $parentID;
   }
   else
   {
      $navigation[$i]['name'] = $nav_name;
      $navigation[$i]['href'] = $nav_href;
     
      if($current_parent_id == $navID)
      {
         $p = 1;
         $result = mysql_query("SELECT * FROM `cms_navigation` WHERE `parentID` = '{$current_parent_id}' ORDER BY `navID` ASC");
         while(list(, , $nav_name, $nav_href) = mysql_fetch_row($result))
         {
            $navigation[$i]['navigation'][$p]['name'] = $nav_name;
            $navigation[$i]['navigation'][$p]['href'] = $nav_href;
            ++$p;
         }
      }
      else
      {
         $navigation[$i]['navigation'] = false;
      }
      ++$i;
   }
}
  1. $sql = "SELECT * FROM `cms_navigation`";
  2. $results = mysql_query($sql);
  3.  
  4. $navigation = array();
  5. $i = 1;
  6. while(list($navID, $parentID, $nav_name, $nav_href) = mysql_fetch_row($results))
  7. {
  8.    if($parentID != false || $parentID != null)
  9.    {
  10.       $current_parent_id = $parentID;
  11.    }
  12.    else
  13.    {
  14.       $navigation[$i]['name'] = $nav_name;
  15.       $navigation[$i]['href'] = $nav_href;
  16.      
  17.       if($current_parent_id == $navID)
  18.       {
  19.          $p = 1;
  20.          $result = mysql_query("SELECT * FROM `cms_navigation` WHERE `parentID` = '{$current_parent_id}' ORDER BY `navID` ASC");
  21.          while(list(, , $nav_name, $nav_href) = mysql_fetch_row($result))
  22.          {
  23.             $navigation[$i]['navigation'][$p]['name'] = $nav_name;
  24.             $navigation[$i]['navigation'][$p]['href'] = $nav_href;
  25.             ++$p;
  26.          }
  27.       }
  28.       else
  29.       {
  30.          $navigation[$i]['navigation'] = false;
  31.       }
  32.       ++$i;
  33.    }
  34. }

The way I have it set up is that if an entry doesn't have anything in ParentID (if it's null), than that is a parent, so if any other entry has it's navID as their parentID, you would see that nav if you hover over this nav. (I really hope that makes sense).

The array that it should create is:
Code: [ Select ]
Array
(
    [0] => Array
        (
            [name] => INDEX
            [href] => #
            [navigation] =>
        )
 
    [1] => Array
        (
            [name] => SOMETHING
            [href] => #
            [navigation] => Array
                (
                    [0] => Array
                        (
                            [name] => Link 1
                            [href] => #
                        )
 
                    [1] => Array
                        (
                            [name] => Link 2
                            [href] => #
                        )
 
                    [2] => Array
                        (
                            [name] => Link 3
                            [href] => #
                        )
 
                    [3] => Array
                        (
                            [name] => Link 4
                            [href] => #
                        )
 
                )
 
        )
 
    [2] => Array
        (
            [name] => ANYTHING
            [href] => #
            [navigation] => Array
                (
                    [0] => Array
                        (
                            [name] => Link 1
                            [href] => #
                        )
               )
        )
 
)
  1. Array
  2. (
  3.     [0] => Array
  4.         (
  5.             [name] => INDEX
  6.             [href] => #
  7.             [navigation] =>
  8.         )
  9.  
  10.     [1] => Array
  11.         (
  12.             [name] => SOMETHING
  13.             [href] => #
  14.             [navigation] => Array
  15.                 (
  16.                     [0] => Array
  17.                         (
  18.                             [name] => Link 1
  19.                             [href] => #
  20.                         )
  21.  
  22.                     [1] => Array
  23.                         (
  24.                             [name] => Link 2
  25.                             [href] => #
  26.                         )
  27.  
  28.                     [2] => Array
  29.                         (
  30.                             [name] => Link 3
  31.                             [href] => #
  32.                         )
  33.  
  34.                     [3] => Array
  35.                         (
  36.                             [name] => Link 4
  37.                             [href] => #
  38.                         )
  39.  
  40.                 )
  41.  
  42.         )
  43.  
  44.     [2] => Array
  45.         (
  46.             [name] => ANYTHING
  47.             [href] => #
  48.             [navigation] => Array
  49.                 (
  50.                     [0] => Array
  51.                         (
  52.                             [name] => Link 1
  53.                             [href] => #
  54.                         )
  55.                )
  56.         )
  57.  
  58. )

But instead, it creates:
Code: [ Select ]
Array
(
    [0] => Array
        (
            [name] => INDEX
            [href] => #
            [navigation] =>
        )
 
    [1] => Array
        (
            [name] => SOMETHING
            [href] => #
            [navigation] =>
        )
 
    [2] => Array
        (
            [name] => ANYTHING
            [href] => #
            [navigation] =>
        )
 
)
  1. Array
  2. (
  3.     [0] => Array
  4.         (
  5.             [name] => INDEX
  6.             [href] => #
  7.             [navigation] =>
  8.         )
  9.  
  10.     [1] => Array
  11.         (
  12.             [name] => SOMETHING
  13.             [href] => #
  14.             [navigation] =>
  15.         )
  16.  
  17.     [2] => Array
  18.         (
  19.             [name] => ANYTHING
  20.             [href] => #
  21.             [navigation] =>
  22.         )
  23.  
  24. )

Those [navigation] => are set to false, although it should be set to a filled up array

I don't know how to get it done correctly... it's just not happening.

On my local host, it works almost perfectly... just that I have to have two nav items set to the same parent ID for that parent to show... this one doesn't show any no matter what.

I don't know if I explained myself correctly, but I really hope that I get an answer to this... I've being at this for nearly two hours and I can't figure it out.

Thanks
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8415
  • Loc: USA

Post 3+ Months Ago

Nevermind... the following fixed the problem :handsup:
PHP Code: [ Select ]
$sql = "SELECT * FROM `cms_navigation` WHERE parentID IS NULL ORDER BY `navID` ASC";
$results = mysql_query($sql);
 
$navigation = array();
$i = 0;
 
while(list($navID, $parentID, $nav_name, $nav_href) = mysql_fetch_row($results))
{
    $navigation[$i]['name'] = $nav_name;
    $navigation[$i]['href'] = $nav_href;
    $navigation[$i]['navigation'] = false;
   
    $sql = "SELECT * FROM `cms_navigation` WHERE parentID = '{$navID}' ORDER BY `navID` ASC";
    $result = mysql_query($sql);
    if(mysql_num_rows($result) > 0)
    {
        $p = 0;
        while(list(, , $nav_name, $nav_href) = mysql_fetch_row($result))
        {
            $navigation[$i]['navigation'][$p]['name'] = $nav_name;
            $navigation[$i]['navigation'][$p]['href'] = $nav_href;
           
            ++$p;
        }
    }
   
    ++$i;
}
  1. $sql = "SELECT * FROM `cms_navigation` WHERE parentID IS NULL ORDER BY `navID` ASC";
  2. $results = mysql_query($sql);
  3.  
  4. $navigation = array();
  5. $i = 0;
  6.  
  7. while(list($navID, $parentID, $nav_name, $nav_href) = mysql_fetch_row($results))
  8. {
  9.     $navigation[$i]['name'] = $nav_name;
  10.     $navigation[$i]['href'] = $nav_href;
  11.     $navigation[$i]['navigation'] = false;
  12.    
  13.     $sql = "SELECT * FROM `cms_navigation` WHERE parentID = '{$navID}' ORDER BY `navID` ASC";
  14.     $result = mysql_query($sql);
  15.     if(mysql_num_rows($result) > 0)
  16.     {
  17.         $p = 0;
  18.         while(list(, , $nav_name, $nav_href) = mysql_fetch_row($result))
  19.         {
  20.             $navigation[$i]['navigation'][$p]['name'] = $nav_name;
  21.             $navigation[$i]['navigation'][$p]['href'] = $nav_href;
  22.            
  23.             ++$p;
  24.         }
  25.     }
  26.    
  27.     ++$i;
  28. }

Thanks for reading :D It's always a great feeling when you fixed the problem all by yourself... especially if it took you about 2+ hours :D

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 114 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.