0

I have an array like this:

$team_scores = array('Tigers' => 3, 'Spikes' => 6, 'Crashers' => 8, 'Fortress' => 2);

created from a MySQL query in which names are from one column and values are from another column. I want to create a page for each team that should indicate the name of the team and its score as well as position. So in this example I would have 4 pages and each page shows the name and score for a team without showing any information about other teams other than the total teams that participated. So it would show

Name: Spikes Score: 6 Position: 2/4,

For Crashers it would be

Name: Fortress Score: 2 Position: 4/4,

I am thinking a foreach loop would do but am only able to echo the teams and scores on a single page, when what I need is to create 4 pages, one for each item in the array. I am able to do that with data just being pulled from the database, but from such data I do not think its possible to determine the position of each team without having the data in one array.

Is that even possible or am I asking PHP to do what cannot be done? Any help will be deeply appreciated.

0

3 Answers 3

4

Don't do it in PHP. You want to use something like an ORDER BY clause in your SQL query to sort your results by score. You can then use a LIMIT on the query to only fetch one row at a time, and combine that with an offset to get exactly the row you need.

Sign up to request clarification or add additional context in comments.

4 Comments

That doesn't give you the position, does it?
Thanks @Obsidian. I am comfortable using the ORDER BY on the query but I don't think it will be able to tell at which position the score it has pulled from the db is. That is why I am thinking PHP may come to the rescue.
@Sylverdrag if you're going by offset in the query, you already know the position. Trying to think of how to get it without the offset though...give me a bit.
@Sylverdrag okay nevermind, screw that netcoder posted a good solution below. MySQL specific though. /me goes to upvote
1

In MySQL you'd do:

SELECT 
   @position:= @position + 1 AS position,
   score_table.*
FROM 
   score_table,
   (SELECT @position := 0) p 
ORDER BY score;

Then you could do:

$count = count($results);
foreach ($results as $result) {
    echo "Name: {$result['name']}";
    echo "Score: {$result['score']}";
    echo "Position: {$result['position']}/{$count}";
}

If you want to do it in plain SQL (e.g.: compatible with other SQL databases), you'd do:

SELECT 
   *
FROM 
   score_table
ORDER BY score;

Then, considering you have numerically indexed $results:

foreach ($results as $key => $result) {
    echo 'Position: '.($key+1);
}

6 Comments

Thanks, does this assume or require that there is a column in the table for positions? I cannot recognize the syntax for the query!
@Bululu: No it doesn't. @position is a MySQL variable.
@Bululu if you add a unique ID for each team by the way, you can just use the first example @netcoder just posted, and only grab the data for the team that you're looking at the page of. you just have to grab the data from something like _GET, typecast/escape it, and use it in your query to remove any need for iteration at all.
What goes at @position? I am familiar with the syntax like: "SELECT score_column FROM score-table"; so I could not see how to structure the query. Thanks anyway for your time.
@Bululu: Again, @position is a MySQL variable (link above). It is server-side (MySQL-side).
|
0

Use $_GET to figure out which team to display:

/teams.php?team=spikes
...
$i = 0;
$nbTeams = count($team_scores)
foreach ($team_scores as $team => $score)
{
   $i++;
   if ($team == $_GET[team])
   {
       echo "Name: $team Score:  $score Position: $i/$nbTeams";
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.