Php 检索表行的id?

Php 检索表行的id?,php,jquery,Php,Jquery,当点击按钮“更多细节”时,我有下面的脚本打开表2 <script> $(document).ready(function() { $('#Table1 tr').click(function(event){ $('#Table2').show(); alert($(this).attr('id')); }); }); </script> 这是我的密码 <table id= "T

当点击按钮“更多细节”时,我有下面的脚本打开表2

 <script>
  $(document).ready(function() {

       $('#Table1 tr').click(function(event){

         $('#Table2').show();
         alert($(this).attr('id'));
       });
     });

</script>
这是我的密码

 <table id= "Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
    $sql2=..
    $sql3 = blabla ;
    while($row3 =mysql_fetch_array($sql3)){
    $sql4 = mysql_query (" SELECT $ww as place FROM data WHERE $ww =".$row3[$ww]." and  id_user = ".$userid." ");
    $row4 = mysql_fetch_array($sql4) ;
    $string = "<blink > here  </blink>" ;

    $wnumber = $row3[$ww] ;

     echo "<tr id= '".$wnumber."'><td  style= 'text-align : center ;'>Week ".$row3[$ww]."
              </td>" ;
     echo "<td >".(int) $row3["percent"] ."% </td>";
     echo "<td > "?><?php if($row4['place'] == 
     $row3[$ww] and $row2['id'] == $userid ){ echo $string ; } else { echo "";} ;?><?php
     "</td>";
     echo "<td ><button class='showr'>More Details</button> </td></tr>";
    //More Details when clicking on this buttom it open the table2
     }
   </tr>
 </table>
这是第二张桌子

 <?php
    echo "<div id= 'Table2' style= 'display:none;'>";
    echo "<table width='100%' border='1' cellspacing='0' cellpadding='0'>";
    echo "<th> Week ".$wnumber."</th>";
    echo "<th>try2</th>";
    echo "<tr ><td>day</td>";
    echo "<td>fff</td></tr>";
    echo "</table></div>";
 ?>
*我现在有5排5个钮扣。 *现在的情况是,当点击每个底部时,它都会回显相同的“$wnumber”,比如说6。 然而,这一行与另一行有所不同, -脚本可以很好地警告单击的行的id。 -只有使用行的最后一个id的最后一个按钮。 *我想要的是每个底部都使用其行id,该行id回显右侧的“$wnumber” *我尝试的是在div中设置变量

   echo "<div id= '".$wnumber."' style= 'display:none;'>"; 
而不是

    echo "<div id= 'Table2' style= 'display:none;'>"; 
但是没有起作用

希望它清晰,并且有解决方案

编辑:这是源代码

   <script src="http://code.jquery.com/jquery-latest.js"></script>


    <script>
    $(document).ready(function() {

   $('#Table1 tr').click(function(event){

     $('#Table2').show();
     alert($(this).attr('id'));
   });
 });

  </script>

  <br />
  <table id= "Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
  <th >Weeks</th>
 <th ><p></p></th>

 <th > Your place</th>
 <th > More Details</th>
<tr>
 <tr id= '1'><td  style= 'text-align : center ;'>Week 1</td><td style= 'text-align : 
  center ;'>33% </td><td  style= 'text-align : center ;'> <td style= 'text-align :  
  center ;'><button class='showr'>More Details</button></td></tr><tr id= '6'><td   
  style= 'text-align : center ;'>Week 6</td><td style= 'text-align : center ;'>33%   
  </td><td  style= 'text-align : center ;'> <td style= 'text-align : center   
  ;'><button  
  class='showr'>More Details</button></td></tr><tr id= '13'><td  style= 'text-align:   
  center ;'>Week 13</td><td style= 'text-align : center ;'>33% </td><td  style=   
  'text-align : center ;'> <blink style= 'color:#990000 ;font-weight: bolder;' >  69   
  here </blink><td style= 'text-align : center ;'><button class='showr'>More   
  Details</button></td></tr></tr>

 </table>
 <br />
 <div id= 'Table2' style= 'display:none;'><table width='100%' border='1'  
  cellspacing='0' cellpadding='0'><th> Week 13</th><th>try2</th><tr ><td>day</td>  
  <td>fff</td></tr></table></div>
  <br /><br /> <br />

我将您的代码复制/粘贴到一个JSFIDLE中,它似乎按照预期工作。单击该行将发送具有正确ID的警报

你的意图中有我遗漏的东西吗

注意:最佳做法是跳过内联CSS并为表添加外部样式。此外,必须使用有效的HTML

更新:

我继续对您发布的php代码进行了格式化,使其更易于阅读。有一些变量没有定义,还有一些其他问题,但我确信php文件中的变量是正确的

我认为您需要再次运行循环来创建第二个表。在第二个循环中,每个循环回显第二个表一次-您将得到一组表,每行一个表加上第一个表。然后,只需在用户单击每个id时交换表的可见性

这就是你要找的吗?如果没有,也许试着重新表述你的问题

以下是格式化代码:

<table id="Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
<?php
$sql2 = [..];
$sql3 = [..];
$ww = ?;

while($row3 = mysql_fetch_array($sql3)){

    $sql4 = mysql_query ("SELECT $ww as place FROM data WHERE $ww =".$row3[$ww]." and  id_user = ".$userid." ");
    $row4 = mysql_fetch_array($sql4) ;
    $string = "<blink> here </blink>" ;

    $wnumber = $row3[$ww];

    echo "<tr id='".$wnumber."'>";
        echo "<td style='text-align:center;'>Week ".$row3[$ww]."</td>";
        echo "<td>".(int) $row3["percent"] ."% </td>";
        echo "<td>":
        if($row4['place'] == $row3[$ww] and $row2['id'] == $userid ){
            echo $string;
        } else {
            echo "&nbsp;";
        }
        echo "</td>";
        //More Details when clicking on this buttom it open the table2
        echo "<td ><button class='showr'>More Details</button></td>";
    echo "</tr>";
} ?>
</table>

<div id= 'Table2' style= 'display:none;'>
    <table width='100%' border='1' cellspacing='0' cellpadding='0'>
        <tr>
            <th> Week <?php echo $wnumber; ?></th>
            <th>try2</th>
        </tr>
        <tr>
            <td>day</td>
            <td>fff</td>
        </tr>
    </table>
</div>

在更正了丢失的td,tr之后,我尝试了您的html代码。 然后单击每行/按钮显示div。
确保在php echo中形成正确的html代码

尝试以下方法:

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
    $(document).ready(function() {
        $('#Table1 tr').click(function(event){
            $('#details').find('#week' + $(this).attr('id')).show();
            // alert($(this).attr('id'));
        });
    });

</script>
<?php

$result = mysql_query($sql);

// save the results to an array so you can loop them twice
while($row = mysql_fetch_array($result)) :
    $rows[] = $row;
endwhile;

?>
<table id="table1">
    <tr>
        <th>heading</th>
        <th>heading</th>
        <th>heading</th>
        <th>heading</th>
    </tr>
    <?php foreach ($rows as $row) : // loop #1 to output the main table ?>
    <tr id="<?php echo $row['ww'] ?>">
        <td>value</td>
        <td>value</td>
        <td>value</td>
        <td><button type="button">More details</button></td>
    </tr>
    <?php endforeach; ?>
</table> <!-- end table 1 -->

<div id="details">
    <?php foreach ($rows as $row) : // loop #2 to output a table for each set of details ?>
    <table id="week<?php echo $row['ww'] ?>" style="display: none">
        <tr>
            <th>Week <?php echo $row['ww'] ?></th>
            <th>try2</th>
        </tr>
        <tr>
            <td>value</td>
            <td>value</td>
        </tr>
    </table>
    <?php endforeach; ?>
</div> <!-- end details -->

您可以从查看源发布转换后的html。转到您的网页,右键单击,查看源,然后复制所需的html并粘贴到此处。如果有人想尝试你的代码,在html之间使用php是没有帮助的。据我所知,你的代码工作得很好,但我不明白你想做什么。你的html无效;您的元素需要包装在一个。我认为您的PHP循环逻辑是错误的。表2中只有一行。如果我理解正确,那么表2中的行数必须与表1中的行数相同。代码非常混乱-如果你清理了你的PHP和HTML,帮助会容易得多。是的,我说它会提醒正确的id,但我想在表2中将此id作为“$wnumber”在链接中回显,因为我看到它没有显示表2,它只提醒idecho是一个PHP命令,并在创建页面时呈现-如果不刷新页面,echo无法更改每个用户单击的内容。您要做的是使用jQuery更改表2的.text。看起来您需要添加更多id标记,以便轻松交换信息。这里有一个开始:你是说第3行而不是第3行?我不知道为什么表1中会识别“$row3[$ww]”,它有值,但表2中没有识别,它没有值。你需要两个循环,这是我代码的重点。我没有使用$row3,但是如果你想使用它,这是你的要求,尽管变量名不是很可读或语义。我已经完成了你所有的代码,我必须更改“endforeach;”的位置之后的第二个表,并将行更改为第3行,因此一切看起来都很好,没有错误,但脚本不起作用,没有显示表2,endforeach;一定要在那之前。