Welcome to WebmasterWorld Guest from 54.242.94.72

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Using array to build navigation

Dynamic navigation based on a custom ACL

     

jspeed

7:18 pm on Aug 1, 2012 (gmt 0)

5+ Year Member



Scenario: Dynamic navigation based on a custom access control list I'm doing for a project.

I have a table that has all the sections of the website, along with an enum column (yes,no) on whether that user has access to the section. Then I build navigation based off the results. I've been storing the results in an array like so:

$columnValues = array();
while ($row = mysql_fetch_array($result)) {
$columnValues[col1] = $row['col1'];
$columnValues[col2] = $row['col2'];
$columnValues[col3] = $row['col3'];
$columnValues[col4] = $row['col4'];
$columnValues[col5] = $row['col5'];
$columnValues[col6] = $row['col6'];
}
foreach ($columnValues as $key => $val) {
if ($val == "yes") {
$acceptable_pages[] = $key;
}
}

and that works. What I want to do is make that array populate dynamically without hard coding the column names and values.

So this is what I've come up with so far:

while ($row = mysql_fetch_array($result)) {
foreach ($row as $key => $val) {
if ($val == "yes") {
$acceptable_pages[$key]=$val;
}
}
}

The output:
Array ( [0] => yes [col1] => yes [1] => yes [col2] => yes [2] => yes [col3] => yes [3] => yes [col4] => yes [4] => yes [col5] => yes [5] => yes [col6] => yes )

The navigation builds links for the numbers, and the column names.

Admittedly I am not a pro with arrays, so any help would be much appreciated.

EDITED: for clarity

jspeed

7:30 pm on Aug 2, 2012 (gmt 0)

5+ Year Member



Figured it out. For anyone interested, I just excluded the integer keys before I built the array.

while($row = mysql_fetch_array($result)) {
foreach ($row as $key => $val) {
if(!is_int($key) && $val=='yes'){
$acceptable_pages[] = $key;
}
}
}

Then when I build navigation from the array, I just check if the website section exists in the array:

if (in_array($page,$acceptable_pages))
{
foreach ($acceptable_pages as $key => $val) {
// build links
}
}

Where $page would be set from where the user navigated from.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month