Jump to content

[SOLVED] Sort Options in php ?


srhino

Recommended Posts

Hey everyone!

 

I am trying to build a really simple golf leaderboard. Is there a way to build it so when they data records show it will automatically sort the records in order? I would like it to sort similar to below

 

 

1  John  -4

2  Scott  -3

2  Fred  -3

2  xxxx  -3

5  xxxx  -2

5  xxxx  -2

7  xxxx  -1

8  xxxx  0

 

Thanks in advance for your input

Link to comment
Share on other sites

I will query the following from a database.

 

team

player1

player2

player3

player4

player5

score

thru

 

Here is the code that I have started. I have not finished writing yet because I wasn't sure where to go.

 

<?php

$con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); 
mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase');

$query = "SELECT * FROM scoring order by rank" ;

$result = mysql_query($query);

echo "
<table width=\"100%\" border=\"1\" align=\"center\">
<tr> <br>
<td>Rank</td>
<td>Team Name</td>
<td>Player 1</td>
<td>Player2</td>
<td>Player3</td>
<td>Player4</td>
<td>Player5</td>
<td>Score</td>
<td>Thru</td></TR>
<tr>
<td>$rank</td>
<td>$team</td>
<td>$p1</td>
<td>$p2</td>
<td>$p3</td>
<td>$p4</td>
<td>$p5</td>
<td>$score</td>
<td>$th</td>
</tr>
</table>";
?>

 

 

 

 

Link to comment
Share on other sites

Ok you just need to include the loop in there to retrieve the info and it should be fine.

 

<?php

$con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); 
mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase');

$query = "SELECT * FROM scoring order by rank" ;

$result = mysql_query($query) or die(mysql_error());

echo "
<table width=\"100%\" border=\"1\" align=\"center\">
<tr>
<td>Rank</td>
<td>Team Name</td>
<td>Player 1</td>
<td>Player2</td>
<td>Player3</td>
<td>Player4</td>
<td>Player5</td>
<td>Score</td>
<td>Thru</td></TR>";

while($row = mysql_fetch_array($result)) {

echo "<tr>
<td>".$row['rank']."</td>
<td>".$row['team']."</td>
<td>".$row['p1']."</td>
<td>".$row['p2']."</td>
<td>".$row['p3']."</td>
<td>".$row['p4']."</td>
<td>".$row['p5']."</td>
<td>".$row['score']."</td>
<td>".$row['thru']."</td>
</tr>

}

echo "</table>";

?>

 

Hopefully that should do what you want, make sure you edit the formatting of the HTML to how you want to though, and sorry if there's any errors in there (I'm a bit tired).

Link to comment
Share on other sites

ok, query and sort by score ascending

 

when displaying, start with 1 and as the score changes, add to rank.

 

ex.

$rank=0;

$r="";

while($row = mysql_fetch_array($result)) {

if ($r!=$row['score']){$rank++}

$r=$row['score'];

echo "<tr>

<td>".$rank."</td>

<td>".$row['team']."</td>

<td>".$row['p1']."</td>

<td>".$row['p2']."</td>

<td>".$row['p3']."</td>

<td>".$row['p4']."</td>

<td>".$row['p5']."</td>

<td>".$row['score']."</td>

<td>".$row['thru']."</td>

</tr>";

 

}

 

this should be close to what you want.

 

 

Link to comment
Share on other sites

I am getting there. One more thing. where would I put that code...

 

Here is how I wrote it so far...

 

<?php

$con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); 
mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase');

$query = "SELECT * FROM scoring order by score" ;
$result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved');

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$rank = $row['rank'];
$team = $row['team'];
$p1 = $row['p1'];
$p2 = $row['p2'];
$p3 = $row['p3'];
$p4 = $row['p4'];
$p5 = $row['p5'];
$score = $row['score'];
$th = $row['th'];


echo "$rank, $team, $score, $th";}

?>

Link to comment
Share on other sites

I have tried that but it doesn't work.

 

I'm not very good at this I guess.

 

<?php

$con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); 
mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase');

$query = "SELECT * FROM scoring order by score" ;
$result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved');



while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$rank = $row['rank'];
$team = $row['team'];
$p1 = $row['p1'];
$p2 = $row['p2'];
$p3 = $row['p3'];
$p4 = $row['p4'];
$p5 = $row['p5'];
$score = $row['score'];
$th = $row['th'];

// where does the following go?
$rank=0;
$r="";

// with this while statement does it go at the top where I have the other while statement?
while($row = mysql_fetch_array($result)) {
if ($r!=$row['score']){$rank++}
$r=$row['score'];


echo "$rank, $team, $score, $th<br>";}

?>

 

Link to comment
Share on other sites

<?php

$con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); 
mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase');

$query = "SELECT * FROM scoring order by score" ;
$result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error());
#$row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved');


$rank=0;
$r="";

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
#$rank = $row['rank'];
$team = $row['team'];
$p1 = $row['p1'];
$p2 = $row['p2'];
$p3 = $row['p3'];
$p4 = $row['p4'];
$p5 = $row['p5'];
$score = $row['score'];
$th = $row['th'];

if ($r!=$score){$rank++}
$r=$score;


echo "$rank, $team, $score, $th<br>";}

?>

Link to comment
Share on other sites

OK, thats what I did the first time and it didn't work and it still doesn't work.

 

Where else could I be going wrong?

 

When I run the program nothing shows not even an error message.

 

<?php

$con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); 
mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase');

$query = "SELECT * FROM scoring order by score" ;
$result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error());
#$row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved');


$rank=0;
$r="";

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
#$rank = $row['rank'];
$team = $row['team'];
$p1 = $row['p1'];
$p2 = $row['p2'];
$p3 = $row['p3'];
$p4 = $row['p4'];
$p5 = $row['p5'];
$score = $row['score'];
$th = $row['th'];

if ($r!=$score){$rank++}
$r=$score;


echo "$rank, $team, $score, $th<br>";}

?>

Link to comment
Share on other sites

lul.

 

try

 

<?php

$con = mysql_connect("localhost", "test", "test") or die('Could not connect to database');
mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase');

$query = "SELECT * FROM scoring order by score";
$result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error
    ());


$rank = 1;

echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<tr><td>Rank</td><td>Team</td><td>Score</td><td>Th</td></tr>\n";

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    #$rank = $row['rank'];
    $team = $row['team'];
    for($i=1;$i<=5;$i++){ $p[$i] = $row['p' . $i]; }
    $score = $row['score'];
    $th = $row['th'];

    echo "<tr><td>".$rank."</td><td>".$team."</td><td>".number_format($score)."</td><td>".$th."</td></tr>\n";
    
    $rank++;
}

echo "</table>\n";

?>

Link to comment
Share on other sites

lul.

 

try

 

<?php

$con = mysql_connect("localhost", "test", "test") or die('Could not connect to database');
mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase');

$query = "SELECT * FROM scoring order by score";
$result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error
    ());


$rank = 1;

echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<tr><td>Rank</td><td>Team</td><td>Score</td><td>Th</td></tr>\n";

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    #$rank = $row['rank'];
    $team = $row['team'];
    for($i=1;$i<=5;$i++){ $p[$i] = $row['p' . $i]; }
    $score = $row['score'];
    $th = $row['th'];

    echo "<tr><td>".$rank."</td><td>".$team."</td><td>".number_format($score)."</td><td>".$th."</td></tr>\n";
    
    $rank++;
}

echo "</table>\n";

?>

 

not quite. his rank changes as score changes, not by player returned.

 

Link to comment
Share on other sites

First of all thank all of you for your help. I hope I am not being a pain. I am just trying to learn this stuff and my teachers are not that good at explaining this. I am learning more from you guys than I am in School!!

 

Yeah score is one of the rows. That time it worked.  It output the following...but

 

Is there a way for it to show ties?

 

Rank Team Score Th

1 Modells     -3       9

2 GolfSmith -3 8

3 Happy China -2 5

4 Ground Round -1 5

 

so it would read like this

 

Rank

1    Modells

1    Golfsmith

3

4

 

Link to comment
Share on other sites

like this? I put it there but it didn't work.

 

<?php

$con = mysql_connect("localhost", "test", "test") or die('Could not connect to database');
mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase');

$query = "SELECT * FROM scoring order by score";
$result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error
    ());


$rank = 1;

echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<tr><td>Rank</td><td>Team</td><td>Score</td><td>Th</td></tr>\n";

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    #$rank = $row['rank'];
$score_array = array();
    $score_array[] = $row['scores'];

if(!in_array($row['scores'],$score_array)){ $rank++; }
    $team = $row['team'];
    for($i=1;$i<=5;$i++){ $p[$i] = $row['p' . $i]; }
    $score = $row['score'];
    $th = $row['th'];

    echo "<tr><td>".$rank."</td><td>".$team."</td><td>".number_format($score)."</td><td>".$th."</td></tr>\n";
    
    $rank++;
}

echo "</table>\n";

?>

Link to comment
Share on other sites

OK I would like to thank all of you for taking your time helping. I figured it out!!! In case anyone ever needs to know. Here is how I did it...

 

<?php

$con = mysql_connect("XXXXX", "XXXXXXX", "XXXXXX")
or die('Could not connect to database');
mysql_select_db("bestball", $con) or die('Sory could not connect to the
dadtabase');

$query = "SELECT * FROM scoring order by score";
$result = mysql_query($query) or die('Could not find Tournament: ' .
mysql_error
   ());


$rank = 1;
$recordCounter=1;
$previousScore=0;

echo "<table border=\"1\" cellspacing=\"3\" cellpadding=\"3\" align=\"center\">\n";
echo "<tr>
<td>Rank</td>
<td>Team</td>
<td>Score</td>
<td>Thru</td>
<td>Player 1</td>
<td>Player 2</td>
<td>Player 3</td>
<td>Player 4</td>
<td>Celebrity</td>
</tr>\n";

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   $score_array = array();
   $score_array[] = $row['scores'];

//comment out the following line.
//if(!in_array($row['scores'],$score_array)){ $rank++; }

   $team = $row['team'];
   for($i=1;$i<=5;$i++){ $p[$i] = $row['p' . $i]; }
   $score = $row['score'];
   $th = $row['th'];
   $p1 = $row['p1'];
   $p2 = $row['p2'];
   $p3 = $row['p3'];
   $p4 = $row['p4'];
   $p5 = $row['p5'];

if ($previousScore != $score){$rank = $recordCounter;}

   echo "
   <tr>
   <td>".$rank."</td>
   <td>".$team."</td>
   <td><h9 align=\"right\">".number_format($score)."</h9></td>
   <td>".$th."</td>
   <td>".$p1."</td>
   <td>".$p2."</td>
   <td>".$p3."</td>
   <td>".$p4."</td>
   <td>".$p5."</td>
   </tr>\n";

$recordCounter++;
$previousScore = $score;
}

echo "</table>\n";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.