0

我正在尝试从 mysql 填充多个表,并按 id 对它们进行排序。我不知道该怎么做,在这里也找不到太多。

目前我只从 mysql 填充一个表,但我已经在我的 mysql 表中添加了一个新列,这将是每个表的特定 id - 虽然不知道如何循环这个。

在预测表中,我有一列名为 'accid' 。我希望此列将一组具有相同“acid”的结果组合在一起并插入到 html 表中。我该怎么做呢?

这是我目前所拥有的:

    $result = mysql_query("SELECT results.home_team, results.away_team, results.home_id,    
       results.away_id, results.ht_score, results.ft_score, predictions.draw,    
       predictions.winningid 
          FROM results 
              LEFT JOIN predictions ON results.match_id = predictions.matchid 
          WHERE userid='".$_SESSION['id']."' ORDER BY home_team");

    $num_rows = mysql_num_rows($result);   

    if($num_rows) {
    $wonBet = true;  #ADDED
    $notlong = false;
    while ($row = mysql_fetch_assoc($result)) {
        echo "<tr>";
        if(!$row["ht_score"]) {
            $halftime = "-";
        } else {
            $halftime = $row["ht_score"];
        }
        if(!$row["ft_score"]) {
            $fulltime = "-";
        } else {
            $fulltime = $row["ft_score"];
        }

        echo "<td>" . $row["home_team"] . "</td>" . "<td>" . " vs " . "</td>" . "<td>" . $row["away_team"] . "</td>" . "<td>" . $halftime . "</td>" . "<td>" . $fulltime . "</td>";



        $value = $fulltime;
        $apart = explode('-',$value); 
        if($apart[0] > $apart[1] && $row["home_id"]==$row["winningid"]) {
            $hometeamwin = "Win";

        } else if($apart[0] < $apart[1] && $row["away_id"]==$row["winningid"]) {
            $hometeamwin = "Win"; 

        } else if($apart[0] == $apart[1] && !$row["winningid"]) {
            $hometeamwin = "Win"; 

        } else { 
            $hometeamwin = "lose";
               #ADDED 

        }



        if($fulltime!=$row["ft_score"]) {
             $hometeamwin = ""; 
             }

             if($hometeamwin == "lose"){   #ADDED
            $wonBet = false;         #ADDED
        } 

             if($hometeamwin == ""){   #ADDED
            $notlong = true;         #ADDED
        }       


        echo "<td>" . $hometeamwin . "</td>";
        echo "</tr>";
    }

    echo '</table>';
4

2 回答 2

0

要多次使用相同的查询结果,请将它们提取到数组中。然后,您可以在相同的结果上循环多次。

哦 - 并切换到 PDO / MySQLi。..并使用准备好的语句..

于 2012-12-31T16:23:06.037 回答
0
$data = array();

$result = mysql_query("SELECT results.home_team, results.away_team, results.home_id,    
   results.away_id, results.ht_score, results.ft_score, predictions.draw,    
   predictions.winningid 
      FROM results 
          LEFT JOIN predictions ON results.match_id = predictions.matchid 
      WHERE userid='".$_SESSION['id']."' ORDER BY home_team");
while($dbrow = mysql_fetch_assoc($result)) {
   $data[] = $dbrow;
}

foreach($data as $row) {
   //Table

}

foreach($data as $row) {
   //Table 2

}

关于酸裂的一些想法

while($dbrow = mysql_fetch_assoc($result)) {
     $accid = $dbrow['accid'];
     $data[$accid][] = $dbrow;
}
foreach($data as $accid -> rows) {
  //Table Header
  foreach($rows as $row) {
   //Table Row        
  }
  //Table Footer
}
于 2012-12-31T16:23:35.630 回答