Milonic provide full featured pull down web menus for some of the worlds largest companies
click here to see what it can do for you

Download Milonic DHTML Menu
Buy Milonic DHTML Menu

Back To Start Of Archive
Taken From The Forum: Help & Support for DHTML Menu Version 5+
Forum Topic: Click to view post
Last Updated: Saturday July 14 2012 - 06:07:32

Array handoff from PHP to Milonic Menu?


Poster: stein
Dated: Wednesday April 21 2004 - 1:03:59 BST

http://www.akdemocrats.org -- my site

Notice the TOOLS menu item on the right - chock full o' forms and is working well AFAICT.

Notice the Sponsor Statement pull down list menu on the LEFT of the page near the top. This list is dynamically created from a mySQL database table using PHP. See the call to the function "selectMenu" below:
Code:
<?php
selectMenu($connection,"statement","ss_num","ss_num","","");
?>

then the code for the function itself:
Code:
function selectMenu($connection,$tableName,$columnName,$pulldownName,$addOption,$defaultValue)
   {
   //Query to find distict values for $columnName in $tableName
   $distinctQuery = "SELECT $columnName, ss_id from $tableName ORDER BY $columnName";         
//run the distinctQuery on the databaseName
   $resultId = mysql_query ($distinctQuery,$connection);
//retrieving all distinct values
   $i = 0;
   while ($row = __at__ mysql_fetch_array($resultId))
   $resultBuffer[$i++] = $row[$columnName];
//start the <select> widget
   echo "\n<select name=\"$pulldownName\">";
//check for default
   if (isset($defaultValue))
   {
   foreach ($resultBuffer as $result)
      if($result == $defaultValue)
      //yes show as selected
      echo "\n\t<option selected>$result";
   else
      //no, just show
      echo "\n\t<option>$result";
   } //end if defaultValue
   else
   {
   //no defaultValue
   //show database values as options
   foreach ($resultBuffer as $result)
   echo "\n\t<option>$result";
   }
   echo "\n</select>";
} //function done


So my dilemma is that I want to embed this dynamic select list into the DHTML menu system. I don't honestly know any javascript except for a few calls. I had visualized building an array with PHP that is then stored in cache and appended to the javascript call as an additional menu item.

'course I have no clue if data strings/arrays can be handed between PHP, HTML and Javascript. Does anyone else?

Code:
aI("text=</script>
<?php selectMenu($connection,'statement','ss_num','ss_num','','');?>
<input type=submit name=Submit value=read></form>
<script>;type=form;");
}


Poster: kevin3442
Dated: Wednesday April 21 2004 - 4:20:41 BST

Hi,

If I understand you correctly, you want to take the same dynamic select box that you have now and embed it inside of amenu item, corect? If that's the case, it seems to me that you already most of what you need. All you have to do is use the html output of your php selectMenu() function as input into the string passed to the js aI() function. I'll tell you how I'd do it, but keep in mind that I have barely scratched the surface of php... I'm a php novice at best... so if any php gurus out there have a better idea, please feel free to share...

Anyway... three steps:

(1) You're talking about geneating menus dynamically, so you'll be putting some php code into your menu_data file. You'll have to rename it to menu_data.php to get the php preprocessor to parse the php code.

(2) In the html file, where you currently load the menu_data.js file, you'll need to edit the <script> tag so that it loads menu_data.php instead of menu_data.js.

(3) Define the menu item with aI() something like this:
Code:
aI("text=<form action=fulltext.php target=_blank method=get>Sponsor Statements<br><?php selectMenu() ?> <input type=submit value=read></form>;type=form;");
It should all be on one line! In fact, you might try pasting the above code directly into your menu_data.php file... it might work just as it is. If you like, you can use a <table> inside the aI() to better format the placement of the form elements. The <?php selectMenu() ?> part of the aI() string calls the php selectMenu() function, which then fills out the <option>s for the <select>, just as it already does.

Hope that helps,

Kevin


Poster: stein
Dated: Wednesday April 21 2004 - 21:57:33 BST

Thanks so much Kevin - I slapped my forehead when you mentioned the filename needing .php extension. I had thought about it before but dismissed it rather quickly because I thought my 'mother' page (header.php) which calls out the javascript would be processed already but I think you were correct.

page architecture:
index.php - loads header.php which loads Milonic js/php menu files

Regardless, I could not get a menu to render at all. If I get time this afternoon I will trim the function down so it doesn't require any variables to be passed (like your example shows). Should the function code selectMenu() be located in the header.php file or the menu_data.php file? I would figure the menu_data.php but like I said, I didn't get it working last night.

Any additional suggestions? I just uploaded 5.13 codebase...


Poster: kevin3442
Dated: Wednesday April 21 2004 - 23:31:52 BST

Hi Stein,

I think I know what the problem is... it would explain the menu not appearing at all. I hadn't considered that your php selectMenu() function adds newlines and tabs to the html output when it creates the select box. That makes for pretty html, but if it's included in a call to aI() it'll break the line of js code, so that it ends prematurely... bad aI() line = no menu.

Quote:
If I get time this afternoon I will trim the function down so it doesn't require any variables to be passed (like your example shows).

Don't bother. Leave the variables in... because I don't think that's the problem (my lack of parameters in the funciton call was just because of a simple test I did and I forgot that you passed parameters to yours).

Quote:
Should the function code selectMenu() be located in the header.php file or the menu_data.php file? I would figure the menu_data.php but like I said, I didn't get it working last night.

It should be in the menu_data.php file, in the call to aI() that will generate the menu item... like in my example, but with your parameters added... except I'd try single quotes instead of double, since double quotes are used to surround the aI() text. Like so (all on one line):
Code:
aI("text=<form action=fulltext.php target=_blank method=get>Sponsor Statements<br><?php selectMenu($connection,'statement','ss_num','ss_num','','') ?> <input type=submit value=read></form>;type=form;");


You'll have to edit your selectMenu() function, removing all of the \n and \t from the echos to get rid of the newlines and tabs... your new function would look like this:
Code:
function selectMenu($connection,$tableName,$columnName,$pulldownName,$addOption,$defaultValue)
   {
   //Query to find distict values for $columnName in $tableName
   $distinctQuery = "SELECT $columnName, ss_id from $tableName ORDER BY $columnName";
//run the distinctQuery on the databaseName
   $resultId = mysql_query ($distinctQuery,$connection);
//retrieving all distinct values
   $i = 0;
   while ($row = __at__ mysql_fetch_array($resultId))
   $resultBuffer[$i++] = $row[$columnName];
//start the <select> widget
   echo "<select name=\"$pulldownName\">";
//check for default
   if (isset($defaultValue))
   {
   foreach ($resultBuffer as $result)
      if($result == $defaultValue)
      //yes show as selected
      echo "<option selected>$result";
   else
      //no, just show
      echo "<option>$result";
   } //end if defaultValue
   else
   {
   //no defaultValue
   //show database values as options
   foreach ($resultBuffer as $result)
   echo "<option>$result";
   }
   echo "</select>";
} //function done

When you load the page, if your menu doesn't appear, go dig the menu_data.php file out of your cache (e.g., copy it to another folder, where it'll probably be named something like menu_data[1].htm)... open it in an editor and look at the aI() line in question; it should be one long line of code, including your <select></select> with all of the the <option>s in between. If the line is broken, there's still a \n in the selectMenu() function.

Hope that made sense.

Kevin