简体   繁体   English

点击一个 <td> 使用涉及多个表的SQL查询导致自发布事件的元素。 PHP

[英]Click a <td> element to cause a self posting event with an SQL query involving multiple tables. PHP

What I have is a table that is pulled from a database. 我所拥有的是从数据库中提取的表。 The goal is that I want to be able to click on any team and have a self posted page produce a table with previous yearly results per row (multiple tables, year by year) and a row of total stats. 我的目标是希望能够点击任何团队,并且自己发布的页面会生成一个表,其中每行包含以前的年度结果(多个表,逐年)和一行总统计数。 For example: Click on San Jose and get 2014-2015 stats in 1 row, 2015-2016 in the next row and then a total of all the seasons added up. 例如:点击圣何塞,然后在下一行获得2015 - 2015年的2014-2015统计数据,然后累计所有季节。 Not really sure how to implement it. 不确定如何实现它。 I would love to get some suggestions on the best way to attack this problem. 我希望得到一些关于解决这个问题的最佳方法的建议。 Link To My Project 链接到我的项目
Here is some of my code, so you can see how I am doing it so far. 这是我的一些代码,所以你可以看到我到目前为止这样做了。

$query = "SELECT * FROM standings2015_2016 WHERE confid='2' ORDER BY pts DESC, losses ASC, otl ASC, gf-ga DESC";
        $result = $db->query($query);
        echo "<h3>Western Conference</h3>";
        echo "<table class='table sortable'>";  
        echo "<tr class='TableHeaders'>";
        echo "<td class='hover'>Place</td>";
        echo "<td class='hover'>Team</td>";
        echo "<td class='hover'>Wins</td>";
        echo "<td class='hover'>Losses</td>";
        echo "<td class='hover'>OTL</td>";
        echo "<td class='hover'>Points</td>";
        echo "<td class='hover'>GF</td>";
        echo "<td class='hover'>GA</td>";
        echo "<td class='hover'>+ / -</td>";
        echo "</tr>";   

        $i = 0;
        foreach ($result as $row) {
        $i++;
        $plusMinus = ($row['gf'] - $row['ga']); 
          echo "<tr>";
            echo "<td>";
            echo $i;
            echo "</td><td class='hover2'>";
            echo stripslashes($row['name']); 
            echo "</td><td>";
            echo stripslashes($row['wins']);
            echo "</td><td>";    
            echo stripslashes($row['losses']);  
            echo "</td><td>";    
            echo stripslashes($row['otl']); 
            echo "</td><td>";    
            echo stripslashes($row['pts']);
            echo "</td><td>";
            echo stripslashes($row['gf']);
            echo "</td><td>";
            echo stripslashes($row['ga']);
            echo "</td><td>";
            echo $plusMinus;
            echo "</td>";
         echo "</tr>";
          }
        echo "</table>";

So echo stripslashes($row['name']); 所以echo stripslashes($row['name']); is the team name I want to click on. 是我要点击的团队名称。 Can this be done with an onclick event to prompt a query and a self post? 这可以通过onclick事件来完成,以提示查询和自我发布吗?

In order to do what you want, we can use jQuery and Ajax . 为了做你想做的事,我们可以使用jQueryAjax

First is to download jQuery here . 首先是在这里下载jQuery。

Then create a link element for each team name that has class and data-artid tag: 然后为每个具有classdata-artid标记的团队名称创建一个link元素:

echo '<a href="#" class="teamname" data-artid="'.$row["teamid"].'">'.stripslashes($row['name']).'</a>'; 

Then create the script: 然后创建脚本:

<script src="jquery-1.9.1.min.js"></script> <!-- REPLACE NECESSARY JQUERY FILE DEPENDING ON THE VERSION YOU HAVE DOWNLOADED -->
<script type="text/javascript">

  $(document).ready(function(){ /* PREPARE THE SCRIPT */
    $(".teamname").click(function(){ /* WHEN A TEAM IS CLICKED */
      var elem = $(this); /* STORE THE CLICKED TEAM */
      var teamid = elem.attr("data-artid"); /* GET THE ID OF THE CLICKED TEAM NAME */

      $.ajax({ /* PREPARE THE AJAX */
        type: "POST", /* METHOD TO BE USED TO PROCESS THE PASSED DATA */
        url: "action.php", /* THE PAGE WHERE THE DATA WILL BE PROCESSED */
        data: {"teamid": teamid}, /* THE DATA WE WILL PASS */
        success: function(result){
          $("#content").html(result); /* WE WILL SHOW THE RETURNED DATA TO YOUR CONTENT DIV */
        } /* END OF SUCCESS */

      }); /* END OF AJAX */
    });
  });

</script>

And for your action.php, which will process the passed on data using Ajax: 对于你的action.php,它将使用Ajax处理传递的数据:

<?php

  /* INCLUDE YOUR DB CONNECTION HERE */

  if(!empty($_POST["teamid"])){

    $tr = '
          <table class="table sortable">
            <tr class="TableHeaders">
              <td class="hover">Team</td>
              <td class="hover">Wins</td>
              <td class="hover">Losses</td>
              <td class="hover">OTL</td>
              <td class="hover">Points</td>
              <td class="hover">GF</td>
              <td class="hover">GA</td>
            </tr>
          ';

    $result = $db->query("SELECT name, wins, losses, otl, pts, gf, ga FROM standings2014_2015 WHERE teamid = '".$_POST["teamid"]."'");
    foreach ($result as $row) {
      $tr .= '
             <tr>
               <td>'.$row["name"].'</td>
               <td>'.$row["wins"].'</td>
               <td>'.$row["losses"].'</td>
               <td>'.$row["otl"].'</td>
               <td>'.$row["pts"].'</td>
               <td>'.$row["gf"].'</td>
               <td>'.$row["ga"].'</td>
             </tr>
             ';
    } /* END OF LOOP */

    $tr .= '</table>';

    echo $tr; /* RETURN THIS TO AJAX */

  } /* END OF NOT EMPTY teamid */

?>

You can assign an ID to each one of the teams in your database and then use that ID to pull the information in one php file. 您可以为数据库中的每个团队分配一个ID,然后使用该ID将信息提取到一个php文件中。 In that file you can just get the ID perform the query search and display the information in a different page. 在该文件中,您只需获取ID执行查询搜索并在不同页面中显示信息。

In the index.php where you display all the teams you can add a link to the team.php. 在您显示所有团队的index.php中,您可以添加指向team.php的链接。

 echo "<a href=\"team.php?Id=".$team['Id']."\">";

team.php team.php

    if (isset($_GET['Id']) && is_numeric($_GET['Id'])) 
    {
    // Perform database query
    $teamID = mysql_escape_string($_GET['Id']);

    // Assign the query to a variable
    $query = "SELECT name, wins, losses, otl, pts, gf, ga FROM standings2014_2015 WHERE Id=".$teamID.";";
    $result = $conn->query($query);

    // If the user exists
    if ($result) 
    {
    while ($team = $result->fetch_assoc()) 
    {
       echo $team["name"];
       echo $team["wins"];
       ....
// Display the information in the table
}
}

I suggest this code: 我建议这段代码:

For standings.php modify this: 对于standings.php修改:

 echo stripslashes($row['name']); 

by: 通过:

 echo "<a href=\"standings.php?name=".stripslashes($row['name'])."\">";

Then standings.php should view like this: 然后standings.php应该像这样查看:

    if (isset($_GET['name']) && !is_empty($_GET['name'])) 
    {

    $kname = mysql_escape_string($_GET['name']);

    $query = "SELECT name, wins, losses, otl, pts, gf, ga FROM standings2014_2015 WHERE name=".$kname.";";
    $result = $db->query($query);

    if ($result) 
    {  echo "<table class='table sortable'>";  
       foreach ($result as $row) 
       {
         $tr .= '
           <tr>
             <td>'.$name.'</td>
             <td>'.$wins.'</td>
             <td>'.$losses.'</td>
             <td>'.$otl.'</td>
             <td>'.$pts.'</td>
             <td>'.$gf.'</td>
             <td>'.$ga.'</td>
           </tr>
           ';
         echo $tr;

       }
       echo "</table>";  
    }

   }else{ 
     //YOUR ACTUAL CODE
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM