Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post

    php inside Javascripts

    i have the following php code inside of a javascript to output dynamic DHTML menus but all it does is output a DHTML menu with $row['conference_name'] on all the selections

    ------------------------
    Code:
    <script language="JavaScript">
    <!--
    function mmLoadMenus() {
      if (window.mm_menu_0513170042_0) return;
      window.mm_menu_0513170042_0 = new Menu("root",64,16,"Verdana, Arial, Helvetica, sans-serif",10,"#ffffff","#000066","#0d469d","#82cdff","left","middle",3,0,1000,-5,7,true,true,true,0,true,true);
      mm_menu_0513170042_0.addMenuItem("Division&nbsp;1","location='nfl_division.php?id=1'");
      mm_menu_0513170042_0.addMenuItem("Division&nbsp;2","location='nfl_division.php?id=2'");
       mm_menu_0513170042_0.bgImageUp="mmmenu3_64x16_up.gif";
       mm_menu_0513170042_0.bgImageOver="mmmenu3_64x16_over.gif";
       mm_menu_0513170042_0.hideOnMouseOut=true;
       mm_menu_0513170042_0.bgColor='#0099ff';
      window.mm_menu_0513170219_1 = new Menu("root",100,16,"Verdana, Arial, Helvetica, sans-serif",10,"#ffffff","#000066","#0d469d","#82cdff","left","middle",3,0,1000,-5,7,true,true,true,0,true,true);
    <?php
      $con = mysql_connect("localhost","user","pwd");
      mysql_select_db("user_colleges",$con);
      $sql = "SELECT * FROM `conferences` ORDER BY `conference_name` ASC";
      $result = mysql_query($sql) or die("ERROR: ".mysql_error());
      	while($row = mysql_fetch_array($result)){
      $con_name = $row['conference_name'];
      $con_id = $row['conference_id'];
      echo "  mm_menu_0513170219_1.addMenuItem(".'"$row[conference_name]","location='."'conference.php?id=$row[conference_id]'".'");
      ';
      	}
    ?>   mm_menu_0513170219_1.bgImageUp="mmmenu2_84x16_up.gif";
       mm_menu_0513170219_1.bgImageOver="mmmenu2_84x16_over.gif";
       mm_menu_0513170219_1.hideOnMouseOut=true;
       mm_menu_0513170219_1.bgColor='#0099ff';
      window.mm_menu_0513170250_2 = new Menu("root",102,16,"Verdana, Arial, Helvetica, sans-serif",10,"#ffffff","#000099","#0d469d","#82cdff","left","middle",3,0,1000,-5,7,true,true,true,0,true,true);
      mm_menu_0513170250_2.addMenuItem("Premium&nbsp;Boards","location='boards/index.php?showforum=2'");
      mm_menu_0513170250_2.addMenuItem("Genereal&nbsp;Boards","location='boards/index.php?showforum=3'");
       mm_menu_0513170250_2.bgImageUp="mmmenu1_102x16_up.gif";
       mm_menu_0513170250_2.bgImageOver="mmmenu1_102x16_over.gif";
       mm_menu_0513170250_2.hideOnMouseOut=true;
       mm_menu_0513170250_2.bgColor='#0099ff';
    
      mm_menu_0513170250_2.writeMenus();
    } // mmLoadMenus()
    //-->
    </script>
    <script language="JavaScript1.2" src="mm_menu.js"></script>

  • #2
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    I dont have the time at the moment to fully read your script, however can I suggest doing all your connection and searching outside of the JavaScript...

    EG.
    <?
    mysql_connect...
    mysql_query...
    while($row = mysql_fetch_array($result)){
    $con_name = $row['conference_name'];
    $con_id = $row['conference_id'];
    ?>
    Now return back to java...
    mm_menu_0513170219_1.addMenuItem('<?
    $row[conference_name] ?>' ","location='."<? 'conference.php?id=$row[conference_id] ?>' ");

    It is always ideal to place PHP inside JavaScript as opposed to the other way round.

    I hope you understand what I'm saying as I'm rushed - if you dont hopefully someone can expand on what I am trying to say.

    Taylor.

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    $row[conference_name] should be $row['conference_name']
    $row[conference_id] should be $row['conference_id']

    also you should avoid just putting the variables inside the strings. You should concantenate. That's just a bad coding style.
    Here in the string you are echoing out you need to watch your quotes.

    echo " mm_menu_0513170219_1.addMenuItem(".

    Up to that point you echoed out a string and then have the . which is the symbol used for concatenation. However your next character is a single quote. So it's going to go until it finds another single quote

    '"$row[conference_name]","location='.

    so that is now being concatenated with

    "'conference.php?id=$row[conference_id]'".

    which is being concatenated with

    '"); ';


    You keep switching betwen using double and single quotes. You shouldn't do that. I'm not even sure why you started intermixing the quotes. You don't need them as far as I can tell.

    Besides that is just way too confusing.

    One way to write this is:
    PHP Code:
    echo "mm_menu_0513170219_1.addMenuItem(\"" $row['conference_name'] . ",\"location=conference.php?id=" $row['conference_id']" . "");"
    an even easier way is only echo the values and nothing more:

    PHP Code:
    mm_menu_0513170219_1.addMenuItem("<?php echo $row['conference_name']; ?>","location=conference.php?id=<?php echo $row['conference_id']; ?>");
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    the first one gives me the following error
    ---------------------
    Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/condraft/public_html/4/sss.php on line 26
    ---------------------
    and the second one wont give me an array of them... i need a new line of javascript for each menu item

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I left out an escape character on the first one "\");";

    And what do you mean by it doesn't give you an array of them? That might mean nothing is being returned from your query.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you use my one it'll save you all these hassles - you can generate dynamic menus just by creating HTML - http://www.udm4.com/
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

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