Jump to content

[SOLVED] How to count and total data from database query


unrelenting

Recommended Posts

I need to do some adding in my query or maybe afterwards would be better.

 

I have a table consisting of a football schedule. It contains columns of year, date, opponent, location, win_loss, points_for, and points_against, etc.

 

I want to create a query that will allow me to display for instance:

 

    Your team has played this team 26 times and have a 18-11-1 record all-time against them.

 

What I need is the total times that 'this team' (which would be how many times they are listed as opponent) has played us and even more total the wins and losses via the win_loss column. The win loss column consists of a 0 for a loss, a 1 for a win and a 2 for a tie.

I would go for something like this:

 

<?php
$wins = 0; $loss = 0; $tie = 0;
$results = mysql_query("SELECT win_loss FROM schedule WHERE opponent='someteam'");
$total = mysql_num_rows($results);
while($values = mysql_fetch_array($results)){
     if($values['win_loss'] == 0){ $loss++; }
     if($values['win_loss'] == 1){ $wins++; }
     if($values['win_loss'] == 2){ $tie++; } 
}
echo "Your team has played $total times and have a $wins-$loss-$tie record all-time";
?>

 

I can't think of a better way, but maybe someone else can contribute.

I would go for something like this:

 

<?php
$wins = 0; $loss = 0; $tie = 0;
$results = mysql_query("SELECT win_loss FROM schedule WHERE opponent='someteam'");
$total = mysql_num_rows($results);
while($values = mysql_fetch_array($results)){
     if($values['win_loss'] == 0){ $loss++; }
     if($values['win_loss'] == 1){ $wins++; }
     if($values['win_loss'] == 2){ $tie++; } 
}
echo "Your team has played $total times and have a $wins-$loss-$tie record all-time";
?>

 

I can't think of a better way, but maybe someone else can contribute.

 

That is nice. I wish I could think things through that well.

 

Thank you.

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.