This topic is locked

Dynamically create menu items?

10/25/2011 6:56:38 AM
PHPRunner General questions
M
mickna author

Hi there,
I try to figure out, how I can add links into the menu structure dynamically.
e.g:

Table "DODO" has this structure:
UserName | Category | Item

------------------------------

User01 | Cat_A | white Socks

User01 | Cat_A | white Shirt

User01 | Cat_A | white Pants

User01 | Cat_B | black Socks

User01 | Cat_B | black Shirt

....

User02 | Cat_foo | red trousers

User02 | Cat_foo | green trousers
And another Table "UserCat" just stores the categories a user has applied:
UserName | CatName

------------------

User01 | Cat_A

User01 | Cat_B

User02 | Cat_foo
Now I would like to dynamically add entries to the menu regarding to the User and categories
User01 should see

  • Cat_A (linking to all entries in Cat_A from User01 -> The result of this link will display in a table "white Socks", "white Shirts", "white Pants)
  • CAT_B (Linking to Cat_B from User01)
    MENU

    |

    +- Cat_A

    +- Cat_B
    User02 should only see in the menu
  • Cat_foo (red trousers, green trousers)
    MENU

    |

    +- Cat_foo
    Pseudo code for menueItemModify:

//Counting how much category a User has

$sql="SELECT COUNT(*) AS c FROM UserCat WHERE `userName` ='".$_SESSION["UserID"]."'"; // Count the entries from this User

$rs=CustomQuery($sql);

$data = db_fetch_array($rs);
//Store the names of the categories from the user

$strID = "SELECT DISTINCT CatName FROM userCat WHERE `userName` = '".$_SESSION["UserID"]."'";

$rsID = db_query($strID,$conn);

$dataID = db_fetch_array($rsID);
//Cycle through the categories (for User01 two times, for User02 only one time)

while ($i <= $data) {

*Print a link with the category name $dataID[$i] // For User01: Cat_A, Cat_B and for User02 only Cat_foo

}


I' guess I have to modify menueitem.php or displaymenu.php with a place holder/variable and assign the entries to this place holder to show the links inside the menu struckture...
I really hope you understand, what I mean...

thx,

mickna

M
mickna author 10/25/2011

Ok, found a solution. It's not really nice - but it works:

  • Created a dummy entry in MenuBuilder called "dummy" and override the title and link with my custom ones
  • In Menu item: Modify:

if ($menuItem->getTitle() == 'dummy') {

//Get the names of the categories from the user

$strColName = "SELECT userColName,userColLayout,userColID FROM col_usercol WHERE `userName` = '".$_SESSION["UserID"]."'";

$resColName = db_query($strColName,$conn);
$menu = "</a>";

while ($title = db_fetch_array($resColName, MYSQL_ASSOC)) {

$menu = $menu."<a href='".$title['userColLayout']."_list.php?mastertable=".$strTableName."&masterkey1=".$title['userColID']."'>".$title['userColName']."</a>
";

}

$menuItem->setTitle($menu);

}