...

View Full Version : Unique div ids in loop



Ryoushi
03-11-2009, 10:12 PM
I was kindly redirected from the javascript area to here with my issue, as I realized that the problems with my code were not javascript in nature, but most likely from the php on my page.

In a nutshell, my page here (http://ryoushi.comyr.com/viewguardians.php) utilizes a javascript function to toggle the visibility of a div. However, this page is meant to display user-generated content pulled from a database, so these divs are created in a loop so that one is created for each existing row.

The issue is that the javascript function relies on the ids of the divs, which are not unique. It seems to me that I would use a variable that would increase by one with each cycle of the loop as the id, but after searching and a few failed attempts, I still haven't found a way to go about this. It feels like I'm missing something rather obvious, so I was hoping one of you might be able to catch it.

Here's the section of code that deals with this.

<?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++;
}
?>

Thank you.

sea4me
03-11-2009, 11:11 PM
Please wrap the code using the PHP tag not the CODE tag :thumbsup:

Makes it easier to see

djm0219
03-12-2009, 12:18 AM
<a href="javascript:;" onmousedown="toggle_visibility('div<?php print $i; ?>');">Details</a>
<br>
<div id="div<?php print $i; ?>" style="display: none; overflow: hidden; text-align: left;">

Ryoushi
03-12-2009, 02:41 AM
@Dave: Oh, thank you so much! I'm not sure what I can do to thank you, short of professing undying love. I have been trying to figure this out for a week. Thank you so much.

@sea4me: Thank you for the tip. I'll remember that in the future.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum