在PHP数组中加载不同的描述

时间:2012-09-14 03:08:01

标签: php

我真的找不到一个很好的方法来标题,所以我道歉,如果它有误导性,但我想要做的是我有玩家获得的徽章,我希望他们能够点击徽章和基于徽章,他们点击获取使用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

1 个答案:

答案 0 :(得分: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问题,可以在循环中添加Javscript,但最好将属性添加到徽章标记:

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

通过这种方式,您可以使用单个(静态)Javscript块来构建click事件:

// 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");
    });
});