Jump to content

PHP and Javascript problem


iJoseph

Recommended Posts

I didn;t know if I should put this in javascript or in PHP as it covers both in a way, but as there is nobody in the Javascript forum I put it here. Here is my problem:

 

I have a script that get's info from a mysql database and makes it into a navigation bar (The bar is in Javascript.)

 

And the 'url' bit seems to be the problem. - If I type the url in Manually, "index.php?catt=mainpage-home" Then it works, no problem. But if I type: "' . $rowa['url'] . '" then the menu doesn't drop.

 

The rowa[''] method works fine because it works for the 'name' bit. Just the url seems to not work. I will post the code below.

 

<?php
include "../../includes/mysql_connect.php";
include "../../includes/info_files.php";
?>
<link rel="stylesheet" href="http://www.controlhabbo.net/layout/global.css" type="text/css"  />
<link rel="stylesheet" type="text/css" href="http://www.controlhabbo.net/layout/menu/anylinkmenu.css" />

<script type="text/javascript">
<?php
$result = mysql_query("SELECT * FROM nav_title");

while($row = mysql_fetch_array($result))
  {
echo 'var ' . $row['id'] . '={divclass:\'anylinkmenu\', inlinestyle:\'width:150px; border: solid black 1px; background:#ebebeb\', linktarget:\'\'}
' . $row['id'] . '.items=[
';

$resulta = mysql_query("SELECT * FROM nav_sub WHERE titlereg='$row[reg]'");

$num = mysql_numrows($resulta);

while($rowa = mysql_fetch_array($resulta))
  {

$url = $rowa['url'];

echo '	["' . $rowa['name'] . '", "index.php?catt=main&page=home"]'; ////// ---------  "' . $rowa['url'] . '"  ----------------- Here is the error!!! -------------
if($num > 1) echo ',';
echo '
';

$num = $num - 1;

if($num == 0) { $num = mysql_numrows($resulta); }
}
echo ']

';
}
?>
</script>

<script type="text/javascript" src="http://www.controlhabbo.net/layout/menu/anylinkmenu.js"></script>
<script type="text/javascript">

//anylinkmenu.init("menu_anchors_class") //Pass in the CSS class of anchor links (that contain a sub menu)
anylinkmenu.init("menuanchorclass")

</script> 
<div id="navbar">
            
            	<div id="navbardropdown">

<?php

$resultb = mysql_query("SELECT * FROM nav_title");

while($rowb = mysql_fetch_array($resultb))
  {
echo '<a href="" class="menuanchorclass" rel="' . $rowb['id'] . '">';
echo '<img src="http://www.controlhabbo.net/layout/images/buttons/' . $rowb['id'] . '.png" style="border-width:0" />';
echo '</a>';
  }

?>

	</div>
                
</div>

 

It's nothing to do with the include files, as it works if I type in the url manually, just not if I use the MY SQL database.

 

Any help would be great!

Link to comment
https://forums.phpfreaks.com/topic/209932-php-and-javascript-problem/
Share on other sites

First of all your code is really inefficient. You are querying pretty much the same data 3 different times when you could simply just use a join. Without being able to see the page its kinda hard to tell what the issue is but I have cleaned up your code a bit and hopefully it will help or resolve the issue. You may have to fix some of the variables but it should get you on the right path.

 

<?php
include "../../includes/mysql_connect.php";
include "../../includes/info_files.php";

$bottom_links = '';
$javascript = '';

$result = mysql_query("SELECT nt.id, ns.name AS sub_name, ns.url AS sub_url FROM nav_title nt LEFT JOIN nav_sub ns ON ns.titlereg=nt.reg");
$num_rows = mysql_num_rows($result);

$count = 0;
while($row = mysql_fetch_array($result)){
//increment the counter
$count++;

//build your bottom links
$bottom_links .= '<a href="" class="menuanchorclass" rel="' . $row['id'] . '"><img src="http://www.controlhabbo.net/layout/images/buttons/' . $row['id'] . '.png" style="border-width:0" /></a>';

//build the javascript
$javascript .= 'var ' . $row['id'] . '={divclass:\'anylinkmenu\', inlinestyle:\'width:150px; border: solid black 1px; background:#ebebeb\', linktarget:\'\'}' . $row['id'] . '.items=[';

//add onto the javascript
$javascript .= '["'.$row['sub_name'].'", "'.$row['sub_url'].'"]';

//add ending bracket ?
if($num_rows == $count){
	$javascript .= ']';
}

//add a comma if there is more than one row and its not the last one add a comma ?
if($num_rows > 1 && $num_rows != $count){
	$javascript .= ',';
}
}
?>

<link rel="stylesheet" href="http://www.controlhabbo.net/layout/global.css" type="text/css"  />
<link rel="stylesheet" type="text/css" href="http://www.controlhabbo.net/layout/menu/anylinkmenu.css" />
<script type="text/javascript" src="http://www.controlhabbo.net/layout/menu/anylinkmenu.js"></script>
<script type="text/javascript">

<?php echo $javascript; ?>

//anylinkmenu.init("menu_anchors_class") //Pass in the CSS class of anchor links (that contain a sub menu)
anylinkmenu.init("menuanchorclass");

</script> 
<div id="navbar">
<div id="navbardropdown"><?php echo $bottom_links; ?></div>            
</div>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.