0

Path Navigation using PHP and ColdFusion


This morning I wrote a script for both PHP and ColdFusion that dynamically generate a navigation bar like the one in the Nautilus GNOME File Browser:

It uses URL values to generate content for the navigation bar. Basically, it convents the URL string to an array based on the “/” or “&,=” pattern. It then forms navigation bar like style by using converted array inside a loop. This technique is actually inspired by the professor in my PHP class. I came up with this idea when she talks about how to use one PHP page to translate to several languages based on the URL. The following code is based on this URL style: http:www.example.com/?var=value&number=94

Here is ColdFusion code:

<cfoutput>

<!—- Change it to CGI.SCRIPT_NAME for use with URL path —>
<cfset variables.urlQuery=CGI.QUERY_STRING>

<!— Use listToArray() to convert CGI variable to an array
Change & to / for use with URL path —>
<cfset variables.arrayQuery=listToArray(“#variables.urlQuery#”,’&’)>

<!— Use arraylen() to determine the size of the array —>
<cfif arraylen(variables.arrayQuery) NEQ 0>

<!— &gt; is the greater than symbol —>
<a href=”/Students/Students/0111″ title=”Home”>Home</a> &gt;

<cfloop from=”1″ to=”#arraylen(variables.arrayQuery)#” index=”i”>

<!— Doesn’t need this statement if used with URL path —>
<cfset variables.splitQuery=listtoArray(“#variables.arrayQuery[i]#”,”=”)>

<!— Doesn’t need this loop if used with URL path —>
<cfloop from=”2″ to=”#arraylen(variables.splitQuery)#” index=”o”>

<cfif variables.arrayQuery[i] CONTAINS “projectid”>
Projects &gt;
<a href=”?projectid=#variables.splitQuery[o]#”>Project #variables.splitQuery[o]#</a>
</cfif>
</cfloop>
</cfloop>
</cfif>
</cfoutput>

The following is PHP code. The URL path method mentioned in the above code also works in the code below:

<?php
if (!empty($query)) {
$query = $_SERVER['QUERY_STRING'];
$queryArray = explode(“&”,$query);$_SERVER['QUERY_STRING'] ?>
<a href=”
<?php if ($_SERVER['SERVER_NAME']==’localhost’) { ?>
/ca282
<?php } else { ?>
/Chen
<?php } ?>
“>Home</a> &gt;

<?php
for ($i=0;$i<sizeof($queryArray);$i++) {
$splitArray = explode(“=”,$queryArray[$i]);
for ($o=0;$o<sizeof($splitArray);$o++) {
$firstLevel = $splitArray[$o];
$secondLevel = $splitArray[++$o];
switch ($firstLevel) {
case ‘project’: echo “<a href=”?page=projects”>Projects</a>”;
break;
case ‘page’:    echo ”;
}
if ($firstLevel == ‘project’) {
echo ‘ &gt; <a href=”?project=’. $secondLevel . ‘”>Project ‘. $secondLevel . ‘</a>’;
}
if ($firstLevel == ‘page’) {
switch ($secondLevel) {
case ‘about’: echo ‘<a href=”?page=about”>About Me</a>’;
break;
case ‘projects’: echo ‘<a href=”?page=projects”>Projects</a>’;
}
}
}
}
}
?>


There are no posts related to Path Navigation using PHP and ColdFusion.