Ryoushi
03-11-2009, 05:18 PM
So, I'm not sure exactly where to ask this, but I've been wrestling with this for a week and I need help.
I've been using javascript to toggle divs created for displaying user-generated content pulled from a database. However, only the first div is diplayed when the toggle link is clicked, even for the 2nd, 3rd, 4th, etc. (link here (http://ryoushi.comyr.com/viewguardians.php)).
The source code viewed in the browser indicates that the information IS being called and the divs ARE being created.
I think the reason the script is only working for the first div is because the IDs are not unique, since they are created via a loop. However, I'm not sure how I can get around this. I've done tons of searching, but I can't quite find the answer.
Here is my code (edited out personal info).
<html>
<head>
<title>View Guardians Characters</title>
<link href="style.css" type="text/css" rel="stylesheet" />
<!-- <script language="JavaScript" src="motionpack.js"></script> -->
<script type="text/javascript">
<!--
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
//-->
</script>
</head>
<body>
<?php
$username="username";
$password="password";
$database="database";
mysql_connect("host here",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM Guardians ORDER BY faction";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th>Characters (ordered by Faction)</th>
</tr>
<?php
$i=0;
while ($i < $num) {
$firstname=mysql_result($result,$i,"name_first");
$lastname=mysql_result($result,$i,"name_last");
$sex=mysql_result($result,$i,"sex");
$race=mysql_result($result,$i,"race");
$primary=mysql_result($result,$i,"primary");
$primaryother=mysql_result($result,$i,"primaryother");
$secondary=mysql_result($result,$i,"secondary");
$secondaryother=mysql_result($result,$i,"secondaryother");
$weaponpref=mysql_result($result,$i,"weapon_preferred");
$channel=mysql_result($result,$i,"weapon_channel");
$power=mysql_result($result,$i,"power_scale");
$faction=mysql_result($result,$i,"faction");
$factionother=mysql_result($result,$i,"factionother");
$rank=mysql_result($result,$i,"rank");
$genetic=mysql_result($result,$i,"appearance_genetic");
$modified=mysql_result($result,$i,"appearance_modified");
$ref=mysql_result($result,$i,"ref_sheet");
$personality=mysql_result($result,$i,"personality");
$esteem=mysql_result($result,$i,"esteem");
$strength=mysql_result($result,$i,"strength");
$weakness=mysql_result($result,$i,"weakness");
$events=mysql_result($result,$i,"events");
$decisions=mysql_result($result,$i,"decisions");
$drive=mysql_result($result,$i,"drive");
?>
<tr>
<td>
<?php echo $firstname; ?> <?php echo $lastname; ?>
<br />
<a href="javascript:;" onmousedown="toggle_visibility('div1');">Details</a>
<br>
<div id="div1" style="display: none; overflow: hidden; text-align: left;">
<i><?php echo $race; ?></i>
<br />
Faction: <i><?php echo $faction; ?></i>
<br />
Rank: <i><?php echo $rank; ?></i>
<br />
<i><?php echo $sex; ?></i>
<br />
<br />
Primary Power: <i><?php echo $primary; ?></i>
<br />
Other (if applicable): <i><?php echo $primaryother; ?></i>
<br />
<br />
Secondary Power: <i><?php echo $secondary; ?></i>
<br />
Other (if applicable): <i><?php echo $secondaryother; ?></i>
<br />
<br />
Power Level: <i><?php echo $power; ?></i>
<br />
<br />
Genetic Appearance:
<br />
<?php echo $genetic; ?>
<br />
<br />
Modified Appearance:
<br />
<?php echo $modified; ?>
<br />
<br />
Personality:
<br />
<?php echo $personality; ?>
<br />
<br />
Self Opinion:
<br />
<?php echo $esteem; ?>
<br />
<br />
Strengths:
<br />
<?php echo $strength; ?>
<br />
<br />
Weaknesses:
<br />
<?php echo $weakness; ?>
<br />
<br />
Defining Events:
<br />
<?php echo $events; ?>
<br />
<br />
Important Decisions to Make/Have Made:
<br />
<?php echo $decisions; ?>
<br />
<br />
Inner Drive:
<br />
<?php echo $drive; ?>
<br />
<br />
</div>
</td>
</tr>
<?php
$i++;
}
?>
</body>
</html>
I've been using javascript to toggle divs created for displaying user-generated content pulled from a database. However, only the first div is diplayed when the toggle link is clicked, even for the 2nd, 3rd, 4th, etc. (link here (http://ryoushi.comyr.com/viewguardians.php)).
The source code viewed in the browser indicates that the information IS being called and the divs ARE being created.
I think the reason the script is only working for the first div is because the IDs are not unique, since they are created via a loop. However, I'm not sure how I can get around this. I've done tons of searching, but I can't quite find the answer.
Here is my code (edited out personal info).
<html>
<head>
<title>View Guardians Characters</title>
<link href="style.css" type="text/css" rel="stylesheet" />
<!-- <script language="JavaScript" src="motionpack.js"></script> -->
<script type="text/javascript">
<!--
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
//-->
</script>
</head>
<body>
<?php
$username="username";
$password="password";
$database="database";
mysql_connect("host here",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM Guardians ORDER BY faction";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th>Characters (ordered by Faction)</th>
</tr>
<?php
$i=0;
while ($i < $num) {
$firstname=mysql_result($result,$i,"name_first");
$lastname=mysql_result($result,$i,"name_last");
$sex=mysql_result($result,$i,"sex");
$race=mysql_result($result,$i,"race");
$primary=mysql_result($result,$i,"primary");
$primaryother=mysql_result($result,$i,"primaryother");
$secondary=mysql_result($result,$i,"secondary");
$secondaryother=mysql_result($result,$i,"secondaryother");
$weaponpref=mysql_result($result,$i,"weapon_preferred");
$channel=mysql_result($result,$i,"weapon_channel");
$power=mysql_result($result,$i,"power_scale");
$faction=mysql_result($result,$i,"faction");
$factionother=mysql_result($result,$i,"factionother");
$rank=mysql_result($result,$i,"rank");
$genetic=mysql_result($result,$i,"appearance_genetic");
$modified=mysql_result($result,$i,"appearance_modified");
$ref=mysql_result($result,$i,"ref_sheet");
$personality=mysql_result($result,$i,"personality");
$esteem=mysql_result($result,$i,"esteem");
$strength=mysql_result($result,$i,"strength");
$weakness=mysql_result($result,$i,"weakness");
$events=mysql_result($result,$i,"events");
$decisions=mysql_result($result,$i,"decisions");
$drive=mysql_result($result,$i,"drive");
?>
<tr>
<td>
<?php echo $firstname; ?> <?php echo $lastname; ?>
<br />
<a href="javascript:;" onmousedown="toggle_visibility('div1');">Details</a>
<br>
<div id="div1" style="display: none; overflow: hidden; text-align: left;">
<i><?php echo $race; ?></i>
<br />
Faction: <i><?php echo $faction; ?></i>
<br />
Rank: <i><?php echo $rank; ?></i>
<br />
<i><?php echo $sex; ?></i>
<br />
<br />
Primary Power: <i><?php echo $primary; ?></i>
<br />
Other (if applicable): <i><?php echo $primaryother; ?></i>
<br />
<br />
Secondary Power: <i><?php echo $secondary; ?></i>
<br />
Other (if applicable): <i><?php echo $secondaryother; ?></i>
<br />
<br />
Power Level: <i><?php echo $power; ?></i>
<br />
<br />
Genetic Appearance:
<br />
<?php echo $genetic; ?>
<br />
<br />
Modified Appearance:
<br />
<?php echo $modified; ?>
<br />
<br />
Personality:
<br />
<?php echo $personality; ?>
<br />
<br />
Self Opinion:
<br />
<?php echo $esteem; ?>
<br />
<br />
Strengths:
<br />
<?php echo $strength; ?>
<br />
<br />
Weaknesses:
<br />
<?php echo $weakness; ?>
<br />
<br />
Defining Events:
<br />
<?php echo $events; ?>
<br />
<br />
Important Decisions to Make/Have Made:
<br />
<?php echo $decisions; ?>
<br />
<br />
Inner Drive:
<br />
<?php echo $drive; ?>
<br />
<br />
</div>
</td>
</tr>
<?php
$i++;
}
?>
</body>
</html>