You are here: Array manipulation (for Mysql - PHP menu) « PHP Programming Language « IT news, forums, messages
Array manipulation (for Mysql - PHP menu)

Posted by Angelos on 07/13/05 16:26

OK I have the folowing $tree = array(); that you can see bellow.Array
(
[1] => Array
(
[level] => 0
[parent] => 0
[name] => Category_I
[body] => Please design your content
[img] => 3410110705nopic.gif
)

[17] => Array
(
[level] => 1
[parent] => 1
[name] => sub_cat_I
[body] => Please design your content
[img] => 5504120705nopic.gif
)

[18] => Array
(
[level] => 2
[parent] => 17
[name] => sub_sub_cat_I
[body] => Please design your content
[img] => 4704120705nopic.gif
)

[3] => Array
(
[level] => 0
[parent] => 0
[name] => Category_III
[body] => Please design your content
[img] => 5203120705nopic.gif
)

[4] => Array
(
[level] => 1
[parent] => 3
[name] => Sub cat_III
[body] => Please design your content
[img] => 3502120705nopic.gif
)

[2] => Array
(
[level] => 0
[parent] => 0
[name] => Category_II
[body] => Please design your content
[img] => 5103120705nopic.gif
)

)What I am trying to do is to display this array as a menu ... The second
level Arrays are my keys from the database.
I can't find a loop that will go through each array element and display each
record in hierarchical order.
What I want to achieve is
<ul>
(loop goes here)
<li>
echo "<a href=\"?$key\"> $tree[$key]['name']</a>";
<ul>
(another loop goes here)
<li>
echo "<a href=\"?$key\">
$tree[$key]['name']</a>";
</li>
</ul>
</li>
</ul>

Anyone has any idea how to achieve this ?
I am going to paste the code for what I have done so far (Sorry for the long
post);
class menu
{
var $parent_id=0;
var $level=0;
var $tree = array();
var $nodesCount=0;
var $firstItem=1;
var $lastItem=0;
var $maxLevel=0;
var $firstLevelCnt=0;
var $i=0;
var $x = array();

function menu()
{
$this->nodesCount = 0;
$this->tree = array();
$this->treecnt = array();
$this->maxLevel = 0;
$this->firstLevelCnt = array();
$this->firstItem = array();
$this->lastItem = array();
}

function scanTableForMenu()
{
$dbresult = mysql_query('SELECT * FROM product_categories
WHERE product_categories_id != 0
');
$this->tmpArray = array();
while ($row = mysql_fetch_array($dbresult))
{
$this->tmpArray[$row['product_categories_id']]['parent_id'] =
$row['product_categories_parent_id'];
$this->tmpArray[$row['product_categories_id']]['name'] =
$row['product_categories_name'];
$this->tmpArray[$row['product_categories_id']]['body'] =
$row['product_categories_body'];
$this->tmpArray[$row['product_categories_id']]['img'] =
$row['product_categories_img'];
}
$this->depth($this->tmpArray,0,0);
$this->displayMenu($this->tree);
}
function depth($tmpArray,$parent_id,$level)
{
reset ($tmpArray);
foreach($tmpArray as $key => $value)
{
if ($value['parent_id'] == $parent_id)
{
unset($tmpArray[$key]);
unset($this->tmpArray[$key]);
$cnt = count($this->tree) + 1;
$this->tree[$key]['level'] = $level;
$this->tree[$key]['parent'] = $value['parent_id'];
$this->tree[$key]['name'] = $value['name'];
$this->tree[$key]['body'] = $value['body'];
$this->tree[$key]['img'] = $value['img'];
unset($value);
if($key !=$parent_id)
$this->depth($this->tmpArray,$key,$level+1);
}
}
}
function displayMenu($tree)
{
//PRINT The $tree array
echo "<pre>";
print_r($this->tree);
echo "</pre>";

$currentLevel = 0;
$i =1;
foreach($tree as $key => $value)
{
$this->maxLevel = max($this->maxLevel, $this->tree[$key]['level']);
if($this->tree[$key]['level'] == 0)
{
$this->cntFirstlevel = $i;
$i++;
}
}

//Here goes the <ul> <li> loop
}
$menu1 = new menu();
$menu1->scanTableForMenu();


THANKS

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация