...

View Full Version : php inside Javascripts



boeing747fp
05-15-2004, 02:57 AM
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

------------------------

<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>

Taylor_1978
05-15-2004, 04:08 AM
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.

Spookster
05-15-2004, 05:16 AM
$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:


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:



mm_menu_0513170219_1.addMenuItem("<?php echo $row['conference_name']; ?>","location=conference.php?id=<?php echo $row['conference_id']; ?>");

boeing747fp
05-15-2004, 02:06 PM
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

Spookster
05-15-2004, 06:02 PM
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.

brothercake
05-15-2004, 06:05 PM
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/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum