SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generate .js menu from MySQL

    Below is a snap code of Dynamic Menu using DHTML. The menu is generated using a .js file.

    Menu1=new Array("Home","http://www.dynamicdrive.com","",0,20,138);

    Menu2=new Array("News","blank.htm","",2);
    Menu2_1=new Array("General","blank.htm","",5,20,150);
    Menu2_1_1=new Array("CNN","http://www.cnn.com","",0,20,150);
    Menu2_1_2=new Array("ABCNews","http://www.abcnews.com","",0);
    Menu2_1_3=new Array("MSNBC","http://www.msnbc.com","",0);
    Menu2_1_4=new Array("CBSNews","http://www.cbsnews.com","",0);
    Menu2_1_5=new Array("Canadian News","http://news.bbc.co.uk","",2);
    Menu2_1_5_1=new Array("Vancouver Sun","http://www.vancouversun.com","",0,20,150);
    Menu2_1_5_2=new Array("CTV News","http://www.ctvnews.com","",0);
    Menu2_2=new Array("Technology","blank.htm","",3);
    Menu2_2_1=new Array("TechWeb","http://www.techweb.com","",0,20,200);
    Menu2_2_2=new Array("News.com","http://www.news.com","",0);
    Menu2_2_3=new Array("Wired News","http://www.wired.com","",0);

    Menu3=new Array("Search Engines","blank.htm","",3);
    Menu3_1=new Array("Altavista","http://www.altavista.com","",0,20,150);
    Menu3_2=new Array("Google","http://www.google.com","",0);
    Menu3_3=new Array("Yahoo","http://www.yahoo.com","",0);

    Menu4=new Array("Webmaster","blank.htm","",4);
    Menu4_1=new Array("Dynamic Drive","http://www.dynamicdrive.com","",0,20,180);
    Menu4_2=new Array("JavaScript Kit","http://www.javascriptkit.com","",0);
    Menu4_3=new Array("Freewarejava","http://www.freewarejava.com","",0);
    Menu4_4=new Array("Web Review","http://www.webreview.com","",0);
    the problem is I'm using PHP/MySQL to generate current menu of my website.
    I don't want to type the links manually because the menu lists are pulled from the MySQL database.

    Is it possible to pulled MySQL database from a .js file?

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo 'Menu1=new Array("' $foo '","' $url '","etc...");'

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    PHP Code:
    echo 'Menu1=new Array("' $foo '","' $url '","etc...");'
    The code I showed above is a .js file, not PHP file....

    I don't think you can just put echo'javascript code' just like in PHP since it has to be in the .js file...

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    PHP Code:
    echo 'Menu1=new Array("' $foo '","' $url '","etc...");'
    The code I showed above is a .js file, not PHP file....

    I don't think you can just put echo'javascript code' just like in PHP since it has to be in the .js file...

  5. #5
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't completely understand your question....

    If you are asking whether you can pull MySQL data straight from the javascript file, then the answer is no. Javascript cannot acces the MySQL database.

    You can dynamically create the javascript file with php, and pull the data from the mysql database with php and just echo it out. And then on your pages the source of the javacript include would be <filename>.php instead of <filename>.js

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was asking if you can pull MySQL database out of Javascript (.js) file.

    But you are saying to include the JavaScript code in the PHP file... (which is the other way around from what I asked).

    But it means using PHP variables ,$string, (which pulled from MySQL database) into the javascript codes...
    which I'm not sure if that's possible....

    I hope you understand what I'm asking. because this Javascript/PHP code blending making me dizzy...

    Quote Originally Posted by maskd
    I don't completely understand your question....

    If you are asking whether you can pull MySQL data straight from the javascript file, then the answer is no. Javascript cannot acces the MySQL database.

    You can dynamically create the javascript file with php, and pull the data from the mysql database with php and just echo it out. And then on your pages the source of the javacript include would be <filename>.php instead of <filename>.js

  7. #7
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    So, what you really want to do is read info from a MySQL database table, then create a JavaScript array of menu items? Yes?

    Then do what clamcrusher suggested. PHP runs BEFORE anything is loaded in the client browser. You can use PHP to echo JS code - as far as PHP is concerned it is just string data, just the same as HTML code is string data. How that string data is then used depends on what it contains and how the browser interprets it.

    You can either have the PHP script include the JS array data as and when required when the page is requested by the client, or you can create a separate PHP file to get the data and write it to a text file and save it as myfilename.js (using PHP's filesystem functions).
    Ian Anderson
    www.siteguru.co.uk

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the sample code i gave you will work if you just want to creat some inline javascript code embedded in html.

    if you want to make a dynamic external file, then you need to send the correct mime header
    PHP Code:
    header('Content-Type: text/javascript');
    echo .... 
    Code:
    <script type="text/javascript src="foo.php"></script>
    but it sounds more like you just want to embed it in html.

  9. #9
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    the sample code i gave you will work if you just want to creat some inline javascript code embedded in html.

    if you want to make a dynamic external file, then you need to send the correct mime header
    PHP Code:
    header('Content-Type: text/javascript');
    echo .... 
    Code:
    <script type="text/javascript src="foo.php"></script>
    but it sounds more like you just want to embed it in html.
    clamcrusher, you'll notice that you don't actually need to send the mime header, I'm pretty sure because you added the type attribute to the script tag.

  10. #10
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, didnt know that

  11. #11
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Menu1=new Array("Home","http://www.dynamicdrive.com","",0,20,138);

    Making this menu is not as trivial as it might first look, you have to generate the number of items in the menu too.

    I used something similar and found the function:
    http://fr.php.net/manual/en/function...-data-seek.php
    a very good way of avoiding nested selects.

    I just echoed the js arrays onto the page (I only used it on a handfull of pages) - though now I would tend to cache it, and include it.
    Upgrading to Mysql 5? Auto-increment fields now strict
    use NULL
    Or zero or leave the field name out completely.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •