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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Unique div ids in loop

    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 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.
    Code:
    <?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.

  • #2
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    Please wrap the code using the PHP tag not the CODE tag

    Makes it easier to see

  • Users who have thanked sea4me for this post:

    Ryoushi (03-12-2009)

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,293
    Thanks
    4
    Thanked 202 Times in 199 Posts
    Code:
    <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;">
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    Ryoushi (03-12-2009)

  • #4
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts
    @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.


  •  

    Posting Permissions

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