Jump to content

What's the best way to do this ? MySQL db


sws

Recommended Posts

Hi,

I've got a site where users can enter their hockey picks via a form. There are a series of drop down menus that allow them to select their roster. I have a players table that contain the stats for every nhl player.

Now when the user submits their picks via the form it populates a managers table with their user id and the player id for every player they have selected.

Now I'm trying to make a web page that displays the contest entrants and their points based on the user stats.

What's the best way to handle that ?

I tried this but am getting the following error:

code: [code]<?php
  $standings_sql = "SELECT * FROM nhl_players a, nhl_managers b WHERE a.player_id = b.lwl OR a.player_id = b.lw2 ORDER BY points DESC";
$standings_query = mysql_query($standings_sql);

while ($table_row = mysql_fetch_array($standings_query))
{
  ?>
  <td colspan="2"><font face="Verdana, Times New Roman, Times, serif" size="-2" color="#990000">
  <?=$table_row['handle']?>
  </font>
  </td><?
}

?>[/code]

Error:  Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on Line 66

Could it be because I'm using $table_row while trying to select from 2 different tables ?

Link to comment
https://forums.phpfreaks.com/topic/27029-whats-the-best-way-to-do-this-mysql-db/
Share on other sites

I've done some tweaking. Here's my new query that works.

SELECT nhl_managers.handle, nhl_players.points
FROM nhl_players, nhl_managers
WHERE nhl_players.id = nhl_managers.lw1 OR nhl_players.id = nhl_managers.lw2
ORDER BY nhl_players.points DESC;

This is working but it's returning one line for every player.

What I need is to return the sum of all of the players points on one line with the nhl_managers.handle.

Can someone please show me how to do this ?
Something like...

[code]SELECT m.handle AS manager, SUM(p.points) AS total FROM nhl_managers AS m LEFT JOIN nhl_players AS p ON(m.lw1 = p.id OR m.lw2 = p.id) GROUP BY manager ORDER BY total DESC;[/code]

That should return... (manager => $table_row['manager'], total => $table_row['total']), one row for each manager

return example might be...

[code]manager    total (points)
Manager One 26
Manager Two 23
Manager Three 20
Manager Four 17[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.