使用mysql中的数据填充多个表

时间:2012-12-31 16:12:11

标签: php mysql html-table

我正在尝试从mysql填充多个表,按id排序。我不知道该怎么做,在这里找不到多少。

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

在预测表中,我有一个名为'accid'的列。我希望这个专栏将一组结果与相同的'accid'组合在一起并插入到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>';

2 个答案:

答案 0 :(得分:0)

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

哦 - 然后切换到PDO / MySQLi。 ..并使用准备好的陈述..

答案 1 :(得分: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

}

关于通过accid分割的一些想法

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
}