Forum Moderators: coopster

Message Too Old, No Replies

php coding posting sports stats

php coding posting sports stats

         

vegas1122

10:06 am on Dec 31, 2006 (gmt 0)

10+ Year Member



I have a high school sports site for basketball.

I would like to display stats and totals. I have created the data base with three tables Players,games,stats. the bulk of the stats are entered in the stats table.

The stats table is like this.
id ¦ pid ¦ gid ¦ pts ¦ thr ¦ rbs ¦ blks ¦ ast ¦ etc.
1----10-----1----19-----2-----2-----3-----3----etc.
2-----5-----1----9------0-----1-----1-----1----etc.
3-----7-----1----5------1-----0-----2-----6----etc.
4----10-----2----12-----1-----2-----4-----2----etc.

The Id is unique but the pid and gid are unique for players and games.

All I need to do is display the sum of all unique gid on each line on one stats page. Then for each players page display the sum for unique gid on each line for that certain pid (player).
I need help on the php code to use to display results.

Please help. much gratitude

eelixduppy

2:46 pm on Dec 31, 2006 (gmt 0)



By reading this thread [webmasterworld.com] you can get the basic on how to query the database and print out the information. As far as queries go, however, I'm thinking something like this would work for you:

$query = "SELECT pid,COUNT(gid) FROM table_name GROUP BY pid LIMIT $start, $length";

Then to get the sum of all games played for that page:

$query = "SELECT gid FROM table_name GROUP BY gid LIMIT $start, $length";
//query database
$number = mysql_num_rows($result);

I hope this has helped a little bit :)

Good luck!

vegas1122

11:37 pm on Dec 31, 2006 (gmt 0)

10+ Year Member



Is this what I put on page? I get error at line starting with $number

this is what's in header?
<?php
$query = "SELECT gid FROM table_name GROUP BY gid LIMIT $start, $length";
//query database
$number = mysql_num_rows($result);
?>

This is what's in body?

<?php echo $number['Pts'];?>

eelixduppy

12:15 am on Jan 1, 2007 (gmt 0)



I left out some things figuring that you would fill them in. Maybe I wasn't clear. Here's a more thorough example:
header

<?php
$link = mysql_connect("localhost","username","password");
mysql_select_db("db_name");
$query = "SELECT gid FROM table_name GROUP BY gid"; //try this at first without the limit
$result = mysql_query($query,$link);
$number = mysql_num_rows($result);
echo 'Number of games played: '.$number;
$query = "SELECT pid,COUNT(gid) AS CTgid FROM table_name GROUP BY pid LIMIT $start, $length"
$result = mysql_query($query,$link);
while($row = mysql_fetch_array($result)) {
echo 'Player ID: '.$row['pid'].'<br/>';
echo 'Number of games: '.$row['CTgid'].'<br/><br/>';
}
mysql_close($link);
?>

I hope this is a little more clear :)

vegas1122

2:29 am on Jan 1, 2007 (gmt 0)

10+ Year Member



Sorry I actually have this on top of page. Connections is what connects to mysql database.

<?php require_once('../Connections/Connection.php');?>
<?php
mysql_select_db($database_Connection, $Connection);
$query_statz = "SELECT * FROM stats ORDER BY ID ASC";
$statz = mysql_query($query_statz, $Connection) or die(mysql_error());
$row_statz = mysql_fetch_assoc($statz);
$totalRows_statz = mysql_num_rows($statz);

mysql_select_db($database_Connection, $Connection);
$query_playerz = "SELECT * FROM players";
$playerz = mysql_query($query_playerz, $Connection) or die(mysql_error());
$row_playerz = mysql_fetch_assoc($playerz);
$totalRows_playerz = mysql_num_rows($playerz);

mysql_select_db($database_Connection, $Connection);
$query_gamez = "SELECT * FROM games";
$gamez = mysql_query($query_gamez, $Connection) or die(mysql_error());
$row_gamez = mysql_fetch_assoc($gamez);
$totalRows_gamez = mysql_num_rows($gamez);

mysql_select_db($database_Connection, $Connection);
$query_gidstatz = "SELECT ID, gid, `Date`, Player, `Result`, Opponent, Location, Oppscore, Ourscore, Pts, `2FG`, `3FG`, FTM, FTA, Offreb, Defreb, Totreb, `TO`, Stl, Asst, Blk, PF FROM stats ORDER BY gid ASC";
$gidstatz = mysql_query($query_gidstatz, $Connection) or die(mysql_error());
$row_gidstatz = mysql_fetch_assoc($gidstatz);
$totalRows_gidstatz = mysql_num_rows($gidstatz);
?>

The code you just gave me is that in the <body> </body> or at top of page?
If what you gave me is what goes on top which I assume.

What code do I put in <td> </td> body to actually display the results?

<snip>

the link above shows what looked like last season. The difference last season was I made a table for each player and all I did in Dreamweaver mx 2004 was just use the echo command to display in result in body. It was suggested that I not make so many tables and just make three tables. Games,Players, and Stats. All the stats go into stats table.

I want to thankyou for your help. I don't do this as carreer I just volunteered to do site for the varsity high school basketball team. Happy New Year

[edited by: encyclo at 3:59 am (utc) on Jan. 1, 2007]
[edit reason] no URLs please, see TOS [webmasterworld.com] [/edit]

vegas1122

9:14 am on Jan 1, 2007 (gmt 0)

10+ Year Member



I put this code in and get error:
Parse error: syntax error, unexpected T_VARIABLE in /home/Mypath/testing3.php on line 35
I marked line 35 starting with $result

<?php
mysql_select_db($database_Connection, $Connection);
$query = "SELECT gid FROM stats GROUP BY gid"; //try this at first without the limit
$result = mysql_query($query,$link); //this is line 35
$number = mysql_num_rows($result);
echo 'Number of games played: '.$number;
$query = "SELECT pid,COUNT(gid) AS CTgid FROM stats GROUP BY pid LIMIT $start, $length"
$result = mysql_query($query,$link);
while($row = mysql_fetch_array($result)) {
echo 'Player ID: '.$row['pid'].'<br/>';
echo 'Number of games: '.$row['CTgid'].'<br/><br/>';

?>

eelixduppy

3:48 pm on Jan 1, 2007 (gmt 0)



Take out the $link variable so that it reads just this, for both cases:

$result = mysql_query($query);

Also, unless you have that variables $start and $length defined, you will need to either replace those with actual numbers or take the LIMIT out completely. I'd suggest the latter just to see how everything works first.

vegas1122

6:25 am on Jan 2, 2007 (gmt 0)

10+ Year Member



I placed this below and recieved error. line 68
Parse error: syntax error, unexpected T_VARIABLE in /home/path/testing4.php on line 68

<body>
<?php
mysql_select_db($database_Connection, $Connection);
$query = "SELECT gid FROM stats GROUP BY gid"; //try this at first without the limit
$result = mysql_query($query); $number = mysql_num_rows($result);
echo 'Number of games played: '.$number;
$query = "SELECT pid,COUNT(gid) AS CTgid FROM stats GROUP BY pid LIMIT $start, $length"
$result = mysql_query($query); //this is line 68
while($row = mysql_fetch_array($result)) {
echo 'Player ID: '.$row['pid'].'<br/>';
echo 'Number of games: '.$row['CTgid'].'<br/><br/>';

?>

</body>

Is this the code I put in body if not what do I put in body? thankyou

eelixduppy

2:16 pm on Jan 2, 2007 (gmt 0)



This line here has three errors

$query = "SELECT pid,COUNT(gid) AS CTgid FROM stats GROUP BY pid LIMIT $start, $length"

Two of them are because you do not ave $start and $length defined (or do you?). So I'd take those out until you need to limit the results. And the third error is that the line does not end with a semicolon. So it would then look like this:


$query = "SELECT pid,COUNT(gid) AS CTgid FROM stats GROUP BY pid";

>>>Is this the code I put in body if not what do I put in body?

Yes this is the code you would put in the body because this is going to echo you player stats. Of course if you have more player stats you are going to have to select them and echo them here, too, but we need to have something working first ;)

vegas1122

9:47 am on Jan 8, 2007 (gmt 0)

10+ Year Member



Ok I have this working But it is not repeating for each game. This is only displaying the first game. I need it to repeat and show totals for each game.

<?php
mysql_select_db($database_Connection, $Connection);
$sql = "SELECT pid, SUM(pts) AS Sum_pts FROM `stats` GROUP BY gid";
$result = mysql_query($sql) or die(mysql_error());
$i = mysql_fetch_array($result);
?>

<?php echo $i['Sum_pts'];?>

Also this is only show pts I need it to show all stats.

I need select * sum * from stats group by gid
something like that.

Thankyou for any help.

jatar_k

3:18 pm on Jan 8, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you're only selecting Sum_pts in your query so to show more stats you need to select and output more from the table

you also need to loop this
$i = mysql_fetch_array($result);

something more like

while ($row = mysql_fetch_array($result)) {
echo '<p>',$row ['Sum_pts'];
}

that will echo all the results from your query. You can then add more to your query and then add more output inside that loop.

vegas1122

2:37 am on Jan 10, 2007 (gmt 0)

10+ Year Member



I put the code below in and get this error.
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/path/testing13.php on line 86
0

<?php
$query2 = "SELECT SUM(distinct Ourscore) from `stats` GROUP BY gid";
$result18 = mysql_query($query2);
$row2 = mysql_fetch_array($result18);
echo "".(round($row2[0]))." ";
?>

I want to sum all records by unique gid and show the total. Thankyou.

eelixduppy

1:15 pm on Jan 10, 2007 (gmt 0)



It is not working because your query is failing. Change it to this:

$result18 = mysql_query($query2) [b]or die(mysql_error());[/b]

This will give you the error from mysql so that you can correct it :)

jatar_k

2:05 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



another thing that seems strange is the error message says "mysql_fetch_assoc()" and your snippet is using "mysql_fetch_array()"

very strange

you also didn't add the loop as I showed