Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert PHP script to C# or Javascript

Posted on 2008-10-23
4
Medium Priority
?
792 Views
Last Modified: 2012-05-05
The attached code snippet happens on load of the web page. Can anyone tell me how to convert it to Javascript or C#? Bascially, what I have done is loaded some list items in an array. Then, based on the current page, the HTML is created when the page loads.
// maybe you need to add the directory name(s) too
      $items = array(
          array("link"=>"php.html", "label"=>"PHP scripts"),
          array("link"=>"tut.html", "label"=>"Tutorial"),
          array("link"=>"css.html", "label"=>"CSS template"),
          array("link"=>"code.html", "label"=>"Code examples"));
           
      $menu = '';
      foreach ($items as $val) {
          if  ($_SERVER['SCRIPT_NAME'] == $val['link']) {
          $menu .= '<li><span>' .$val['label'].'</span></li>';
 
	  } else {
	  $menu .= '<li><a href="'.$val['link'].'" >'.$val	   ['label'].'</a></li>';
	  }
      }
      echo $menu;

Open in new window

0
Comment
Question by:computerstreber
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 1200 total points
ID: 22794014
I saw your pages are in .html, so I decided to use javascript to generate the menu.
<script language="javascript">
	//Ver 1.0
	function getFileName(fpath, removeQueryingString, delimiter) {
		if (fpath==null) fpath = window.location.href;
		if (delimiter==null) delimiter = "/";
		var tmp = fpath;
		if (tmp.lastIndexOf("?") > -1) {
			tmp = tmp.substring(0,tmp.lastIndexOf("?"));		
		}
		tmp = tmp.substring(tmp.lastIndexOf(delimiter)+1);
		if (removeQueryingString != null) {
			if (removeQueryingString) {
				var tmpArr = tmp.split("?");
				tmp = tmpArr[0];
			}
		}
		return tmp;
	}
 
	function generateMenu() {
		var arrlink = new Array(3);
		var arrlabel = new Array(3);
		arrlink[0] = "php.html";
		arrlabel[0] = "PHP scripts";
		arrlink[1] = "tut.html";
		arrlabel[1] = "Tutorial";
		arrlink[2] = "css.html";
		arrlabel[2] = "CSS template";
		arrlink[3] = "code.html";
		arrlabel[3] = "Code examples";
		
		var menu = "";
		for (var i=0;i<arrlink.length;i++) {
			if  (getFileName() == arrlink[i]) {
				menu += "<li><span>" + arrlabel[i] + "</span></li>";
			} else {
				menu += "<li><a href='" + arrlink[i] + "'><span>" + arrlabel[i] + "</span></a></li>";
			}
		}
		return menu;
	}
	
	document.write(generateMenu());
</script>

Open in new window

0
 
LVL 21

Assisted Solution

by:naspinski
naspinski earned 800 total points
ID: 22795230
asp.net/c#
<!--asp.net-->
<asp:DropDownList ID="ddl" runat="server" />
 
// C# -- put this in your page_load
if (!IsPostBack)
{
    Dictionary<string, string> items = new Dictionary<string, string>();
    items.Add("php.html", "PHP scripts");
    items.Add("tut.html", "Tutorial");
    items.Add("css.html", "CSS template");
    items.Add("code.html", "Code examples");
 
    foreach (var item in items)
        ddl.Items.Add(new ListItem(item.Value, item.Key));
}

Open in new window

0
 
LVL 21

Assisted Solution

by:naspinski
naspinski earned 800 total points
ID: 22795283
actually, I made that more complicated than I needed to, keep the same asp.net of:

<asp:DropDownList ID="ddl" runat="server" />

and put this in your codebehind:
if(!IsPostBack)
{
    ddl.Items.Add(new ListItem("php.html", "PHP scripts"));
    ddl.Items.Add(new ListItem("tut.html", "Tutorial"));
    ddl.Items.Add(new ListItem("css.html", "CSS template"));
    ddl.Items.Add(new ListItem("code.html", "Code examples"));
}

Open in new window

0
 
LVL 3

Author Closing Comment

by:computerstreber
ID: 31509541
both of these were awesome examples. However, I found the javascript to render much better.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question