0

我真的找不到一个好的方法来命名它,所以如果它具有误导性,我深表歉意,但我想做的是我有玩家获得的徽章,我希望他们能够点击徽章并根据他们单击的徽章会使用 jquery 获取不同的描述以加载。我已经让它部分工作了,但我遇到的问题是它只获取最后一个徽章的信息。我知道它为什么这样做(我认为,php 在页面中首先加载,它需要是一个数组。或者我听起来真的很愚蠢,哈哈)这是我的代码到目前为止:

    <table id="table-profile" width="78%">
  <tr>
    <td><h3><?php echo "$usernameprofile";?>'s Badges (work in progress!!!!!!!) |  <a href="#">View All</a></h3></td>
  </tr>
  <tr>
    <td><?  
 $sqlbadges = mysql_query("SELECT * FROM membersbadges WHERE username='$usernameprofile' LIMIT 8 
 ");
 $checkbadges = mysql_num_rows($sqlbadges);
 if($checkbadges == "0"){
     echo "User is currently badgeless";
 }else{
?>
<table width="100%">
<?php
$tableCount = 0;
    $tableRowOpen = false;
    while($rowbadge = mysql_fetch_assoc($sqlbadges)) { 
    $bid = $rowbadge['badgeid'];

    $getbadge = mysql_query("SELECT * FROM badges WHERE id='$bid'");
while($rowbadge2 = mysql_fetch_assoc($getbadge)){
$badgeid = $rowbadge2['id'];    
$badgename = $rowbadge2['name'];
$badgedesc = $rowbadge2['description'];
$badgereward = $rowbadge2['reward'];
$badgerequirements = $rowbadge2['requirements'];
$badgepic = $rowbadge2['image'];
$date = strftime("%b %d, %Y", strtotime($rowbadge2['date']));

}
        if($tableCount == 0){
            echo "<tr>";
            $tableRowOpen = true;
        }
        echo"<td width='25%'>";
        echo  "<badge".$badgeid.">
<img src=\"http://my.iheff.net/images/badges/".$badgepic.".png\" border=2 alt=\"
        " . $row["name"] . "\" height=40px width=40px></a></badge".$badgeid.">" ;

        echo"<br/><br/></td>";
        if($tableCount + 1 == 4){
            $tableCount = 0;
            echo "</tr>";
            $tableRowOpen = false;
        }
        else{
            $tableCount++;
        }

    }
    if($tableRowOpen){
        echo "
</tr>
";
    }
    echo"
    <table id='badgeinfo".$badgeid."' style='display:none' width='80%' border='0' cellspacing='5' cellpadding='10'>
  <tr>
    <th colspan='2' scope='col'>".$badgename."</th>
    </tr>
  <tr>
    <td>Description:</td>
    <td>".$badgedesc."</td>
  </tr>
  <tr>
    <td>Reward:</td>
    <td>+".$badgereward." Experience</td>
  </tr>
</table>
    ";
?>
</table>
<?
 }
 ?></td>
  </tr>

</table>

<script>
$("badge<?php echo "$badgeid"?>").click(function () {
$("#badgeinfo<?php echo "$badgeid"?>").toggle("slow");
});    
</script>

抱歉代码乱七八糟,我离php高手还差得很远。但是,我将不胜感激。谢谢!

这是一个示例页面,如果您想查看页面的实时状态。http://my.iheff.net/profile.php?id=1637

4

1 回答 1

1

看起来你关闭while循环太快了。(它循环遍历所有记录,并分配它们,但不渲染它们。)所以将所有徽章渲染逻辑放在这个循环中:

while($rowbadge2 = mysql_fetch_assoc($getbadge)){
    $badgeid = $rowbadge2['id'];    
    $badgename = $rowbadge2['name'];
    $badgedesc = $rowbadge2['description'];
    $badgereward = $rowbadge2['reward'];
    $badgerequirements = $rowbadge2['requirements'];
    $badgepic = $rowbadge2['image'];
    $date = strftime("%b %d, %Y", strtotime($rowbadge2['date']));

    // render the row here
}

要修复 Javascript 问题,您可以在循环中添加 Javascript,但最好将属性添加到徽章标签:

echo  "<badge".$badgeid." data-type='badge' data-id='".$badgeid."'">"

这样,您可以使用单个(静态)Javscript 块来构造点击事件:

// select all the badge tags
$("[data-type=badge]").each(function() {
    // get the id for the current badge
    var thisId = $(this).attr('data-id');
    $(this).click(function() {
        $("#badgeinfo" + thisId).toggle("slow");
    });
});
于 2012-09-14T03:16:19.580 回答