homepage Welcome to WebmasterWorld Guest from 54.211.80.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Using array to build navigation
Dynamic navigation based on a custom ACL
jspeed




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

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




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved