难以从两个表中获取数据

时间:2012-01-25 00:19:20

标签: php mysql html-table

作为一名自学成才的(糟糕的)PHP程序员,我已经用尽了所有试错的可能性,所以我谦卑地请求你帮助解决我在编写社区在线“竞标”游戏时遇到的一个小问题。 / p>

我的数据库中有两个表 - 一个名为DCO_sponsors,列出了ID列表,相关数量的(虚拟!)资金,可用性计数器和(如果它们已经与团队关联),团队的ID

结构:id,amount,available,team_id

第二张表DCO_bids列出了团队的“出价”,以赢得上述赞助商。它包括出价ID,团队ID,“part_b”,在这种情况下是赞助商ID(同一表由其他进程使用),概念字段,其中描述了出价类型(例如“赞助”)和最后两个字段用于出价金额和当前状态(A表示有效,W表示等待/待处理等)。

结构:id,team_id,part_b,概念,出价,状态

我想要实现的是向我(管理员)显示DCO_sponsors表中所有赞助商的列表的网站,按金额排序,并在其旁边显示最高出价的团队的team_id(团队只获得四个“赞助商”,并且首先得到最高票房,所以我按照数量顺序看到它们非常重要。

我制作的代码,以及我希望它能够运作的代码如下:

            echo "<table>";
        echo "<tr><th width=30>Id</th><th width=100>Amount</th><th width=220>Team</th><th width=100>Offer</th><th></th><th></th><th></th></tr>";

        $data = mysql_query("SELECT id, amount FROM DCO_sponsors WHERE available='1' ORDER BY amount DESC", $CONNECTW);
        while($row=mysql_fetch_row($data))
        {
            $sp_id = "$row[0]";
            $sp_amount = number_format($row[1]);

            $teamdata = mysql_query("SELECT team_id, bid FROM DCO_bids WHERE concept='sponsorship' AND part_b='$sp_id' ORDER BY bid DESC", $CONNECTW);
            while($row=mysql_fetch_row($teamdata))
            {
                $team_id = "$row[0]";
                $team_bid = number_format($row[1]);                     
            }               

            // I call the name of the team from another table

            $teamlabel = mysql_query("SELECT completename FROM DCO_teams WHERE id='$team_id'", $CONNECTW);
            while($row=mysql_fetch_row($teamdata))
            {
                $teamname = "$row[0]";                      
            }

            echo "<tr><td>$sp_id</td><td>&pound;$sp_amount</td><td>$teamname</td><td>$team_bid</td><td><a href='$RKP/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A1&item_id=$item_id'><div id='IC_Tick' title='Sign Round 1 deal with $teamname'></div></a></td><td><a href='$RKP/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A2&item_id=$item_id'><div id='IC_Tick' title='Sign Round 2 deal with $teamname'></div></a></td><td><a href='$RKP/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A3&item_id=$item_id'><div id='IC_Tick' title='Sign Round 3 deal with $teamname'></div></a></td></tr>";

        }

        echo "</table>";

行的最后一个单元格中的链接是与修改其他表的操作相关联的复选框。

目前,我得到的结果是按金额订购的赞助商列表(应该是这样),但是所有行旁边出现的团队名称是相同的,它不是最高出价者之一并且出价金额不是最高出价者之一,而只是第一个出价的人。

我花了最后三个晚上的大部分时间试图弄清楚我错了...我希望你能帮忙!

2 个答案:

答案 0 :(得分:2)

<table>
  <tr>
    <th width=30>Id</th>
    <th width=100>Amount</th>
    <th width=220>Team</th>
    <th width=100>Offer</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
<? $data = mysql_query("SELECT id, amount FROM DCO_sponsors WHERE available='1' ORDER BY amount DESC", $CONNECTW); ?>

<? while($row=mysql_fetch_row($data)):
    $sp_id = $row['id'];
    $sp_amount = number_format($row['amount']);

    $teamdata = mysql_query("SELECT team_id, bid FROM DCO_bids WHERE concept='sponsorship' AND part_b='$sp_id' ORDER BY bid DESC", $CONNECTW);
    while($row=mysql_fetch_row($teamdata))
    {
        $team_id = $row['id'];
        $team_bid = number_format($row['bid']);                     
    }               

        $teamlabel = mysql_query("SELECT completename FROM DCO_teams WHERE id='$team_id'", $CONNECTW);
        while($row=mysql_fetch_row($teamlabel))
        {
            $teamname = $row['completename'];                      
        }
?>
    <tr>
      <td><?=$sp_id;?></td>
      <td>&pound;<?=$sp_amount;?></td>
      <td><?=$teamname;?></td>
      <td><?=$team_bid;?></td>
      <td><a href='<?=$RKP;?>/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A1&item_id=<?=$item_id;?>'>
        <div id='IC_Tick' title='Sign Round 1 deal with <?=$teamname;?>'></div></a></td>
      <td><a href='<?=$RKP;?>/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A2&item_id=<?=$item_id;?>'>
        <div id='IC_Tick' title='Sign Round 2 deal with <?=$teamname;?>'></div></a></td>
      <td><a href='<?=$RKP;?>/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A3&item_id=<?=$item_id;?>'>
        <div id='IC_Tick' title='Sign Round 3 deal with <?=$teamname;?>'></div></a></td>
    </tr>

<? endwhile; ?>
</table>

您的问题在您上次通话时发挥作用。您再次尝试使用mysql_fetch_row($ teamdata)而不是$ teamlabel。

另外,作为一项规则,你应该写出任何不需要由php生成的html。所以我为你打破了那些东西。

答案 1 :(得分:1)

解决了问题 - 这是有效的代码:

echo "<table>";
    echo "<tr><th width=30>Id</th><th width=100>Amount</th><th width=220>Team</th><th width=100>Offer</th><th></th><th></th><th></th></tr>";

    $query = mysql_query("SELECT id, amount FROM DCO_sponsors WHERE available = '1' ORDER BY amount DESC");
    while($row = mysql_fetch_assoc($query)){
        $sp_id = $row['id'];
        $sp_amount = $row['amount'];
        $sp_amount = number_format($sp_amount);
        $tQuery = mysql_query("SELECT team_id, bid, id FROM DCO_bids WHERE concept='sponsorship' AND part_b='$sp_id' AND status='W' ORDER BY bid DESC");
        $trow = ' ';
        $trow = mysql_fetch_assoc($tQuery);
        $team_id = $trow['team_id'];
        $team_bid = $trow['bid'];
        $team_bid = number_format($team_bid);
        $item_id = $trow['id'];
        $nquery = mysql_query("SELECT completename FROM DCO_teams WHERE id='$team_id'");
        $nrow = mysql_fetch_assoc($nquery);
        $teamname = $nrow['completename'];


        echo "<tr><td>$sp_id</td><td>&pound;$sp_amount</td><td>$teamname</td><td>&pound;$team_bid</td><td><a href='$RKP/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A1&item_id=$item_id'><div id='IC_Tick' title='Sign Round 1 deal with $teamname'></div></a></td><td><a href='$RKP/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A2&item_id=$item_id'><div id='IC_Tick' title='Sign Round 2 deal with $teamname'></div></a></td><td><a href='$RKP/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A3&item_id=$item_id'><div id='IC_Tick' title='Sign Round 3 deal with $teamname'></div></a></td></tr>";
    }

     echo "</table>";