Hi! I read somewhere it's ok to post really newbie questions (viewtopic.php?t=43152&), so I'll try my best:
I need to build a simple standings page in php for a small sports site. It only has to deal with 6 teams and 4 variables: name, games played, games lost and points. No need to say that I'm less than newbie with php and I've wasted a bunch of time trying to figure it out on my own, and that's the only thing I don't have right now. That's why I'm only using php, don't want to make it even more complicated with a db or something, at least for a little while.
What I've done so far is that I've created 6 functions, one for each team, with different variables that do the math just fine, the problem is that when I have to print them in the page, it always returns the values in the same order (team a first, team b second.....), and I need them to be returned in descending order, depending on who has more points...
I've been trying with different functions (if, while, etc.) but it seems to me than in order to make those work, I would have to do all the scenarios, which means repeating the code for each probability. That is 6*5*4*3*2*1. That seems even stupid to me! I'm sure there has to be a better way.
I don't know if arrays would help. Not knowing much, it's almost impossible for me to understand what exaclty they do. Is that the way to go?
Heeeelp!
Thanks for your time!
warning: patience required / need to build simple standings
Moderator: General Moderators
Use google to search PHP tutorials, PHP arrays and other concepts you don' t understand (there are many good tutorials out there) and this forum.
Try to solve it yourself if don' t succeed post a specific question here: what you want to do, the code for that operation and what you think doesn' t work.
Try to solve it yourself if don' t succeed post a specific question here: what you want to do, the code for that operation and what you think doesn' t work.
Rovas: That's not very helpful.
ioko: I think you need to think of it a little more abstractly. The benefit of using a function is that you can pass some information to it: for example, some team data. If you wanted something to calculate the points a team has for example:
The function expects either 2 or 3 arguments (variables that are passed to it). The first two arguments are required, you'll get an error if you miss them out. The third one is optional. If you don't pass it then it'll default to 3.
If you wanted to draw a table of all the teams points then you could do something like:
That's a really basic example. If you're having problems with something specific then post what you've got so far and someone will probably offer some advice.
ioko: I think you need to think of it a little more abstractly. The benefit of using a function is that you can pass some information to it: for example, some team data. If you wanted something to calculate the points a team has for example:
Code: Select all
function calculatePoints($gamesPlayed, $gamesLost, $pointsPerGame=3) {
$gamesWon = $gamesPlayed-$gamesLost;
return $gamesWon*$pointsPerGame;
}
If you wanted to draw a table of all the teams points then you could do something like:
Code: Select all
$arrTeams[0]['name'] = "Newcastle United";
$arrTeams[0]['gamesPlayed'] = 42;
$arrTeams[0]['gamesLost'] = 0;
$arrTeams[1]['name'] = "Manchester United";
$arrTeams[1]['gamesPlayed'] = 42;
$arrTeams[1]['gamesLost'] = 12;
$arrTeams[2]['name'] = "Reading";
$arrTeams[2]['gamesPlayed'] = 42;
$arrTeams[2]['gamesLost'] = 40;
echo "<table>";
foreach ($arrTeams as $id => $data) {
echo "<tr>";
echo "<td>".$data['name']."</td>";
echo "<td>".$data['gamesPlayed']."</td>";
echo "<td>".$data['gamesLost']."</td>";
echo "<td>".calculatePoints($data['gamesPlayed'], $data['gamesLost'])."</td>";
echo "</tr>";
}
echo "</table>";
function calculatePoints($gamesPlayed, $gamesLost, $pointsPerGame=3) {
$gamesWon = $gamesPlayed-$gamesLost;
return $gamesWon*$pointsPerGame;
}rovas, that's what I first did. I've been trying since last Friday, but it just seems that in order for me to understand that, and I believe it's obvious, I've got to do php from the beginning and frankly I don't have the time at this point. It's something like I need a chair, but don't have time to leanr about wood selection, paint, tools... so it's not like i'm being lazy here
Trust me, I'm interested in doing the whole thing, this is not the right timing, though...
onion2k, you've probably saved the day. I'll give it a try and a couple aspirins in a few hours. I'll keep you posted!
onion2k, you've probably saved the day. I'll give it a try and a couple aspirins in a few hours. I'll keep you posted!