简体   繁体   中英

Difficulty in getting data from two tables

As a self-taught (poorly) PHP coder, I have exhausted all the trial-and-error possibilities so I humbly ask your help for a small problem I have been having while coding a community online "bidding" game.

I have two tables in my DB - one, called DCO_sponsors, listing a list of IDs, an associated amount of (virtual!) money, an availability counter and (if they are already associated with a team), the team's id.

Structure: id, amount, available, team_id

The second table, DCO_bids, lists the team's "bids" to clinch the above mentioned sponsors. It includes a bid id, the team id, a "part_b" which in this case is the sponsor id (the same table is used by other processes), a concept field where the kind of bid is described (eg "sponsorship") and finally two fields for the bid amount and the current status (A for active, W for waiting/pending, etc).

Structure: id, team_id, part_b, concept, bid, status

What I want to achieve is the site to show me (the admin) a list of all the sponsors in the DCO_sponsors table, ordered by the amount, and next to it the team_id of the team which bid the highest for it (teams only get four "sponsors" and get the highest grossing first, so it's important I see them in order of amount).

The code I produced, and that I hoped it would work, is as follows:

            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>";

The links in the last cells of the row are tick-boxes linked to actions that modify other tables.

At the moment, the result I get is the list of the sponsors ordered by the amount (as it should be), but the team name appearing alongside it is the same for all rows, it's NOT the one of the highest bidder and the bid amount is NOT the one of the highest bidder but simply the first one to put one in.

I spent the best part of the last three nights trying to work out where I am getting this wrong... I hope you can help!

<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>

Your issue came into play with your last while call. You were attempting to mysql_fetch_row($teamdata) again instead of $teamlabel.

Also, as a rule, you should just write out any html that doesn't need to be generated by php. So I broke that stuff out for you.

Fixed the problem - here is the code that works:

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>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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