Jump to content

sorting arrays


Woodburn2006

Recommended Posts

i use a function to generate arrays. the arrays are in the form of (as an example);

 

$driver_info = array(Dan Woodbury,1,1,3,4,2,3,38);

 

 

i then use the following code to put the arrays onto a page for viewing:

 

$position = 1;
while ($row_drivers = mysql_fetch_array($result_drivers)) {
extract($row_drivers);
$driver = "$firstname $surname";

$info = table_make_array($driver,$group_wins,$group_second,$group_third,$race_wins,$race_second,$race_third,$race_fourth,$race_fifth,$race_sixth,$race_seventh,$race_eigth,$race_ninth,$race_tenth);

echo "<tr><td class='contenttxt'>$position</td>";
foreach ($info as $x) {
        echo "<td class='contenttxt'>$x</td>";
    }
echo "</tr>";
		  
$position++;}

 

the code outputs fine but what i would like is for the arrays to be sorted so that when they are displayed on the page they are sorted in ascending order to the final value of the array (38 in the example).

 

ive tried working this out but seem to be getting knowhere

Link to comment
Share on other sites

the problem i have is that there are going to be loads of different arrays, 1 for each driver and there is about 50 drivers.

 

i am using these arrays to do a table: http://www.upks.co.uk/index1.php?go=table

 

as you can see i have the arrays working but i want to order the table by the 'Total Points' Column.

 

each driver is a seperate array and i need to arrange the different arrays so that i can have them in the needed order on screen.

 

how would i do this?

Link to comment
Share on other sites

ok i will try to explain how i have it set up.

 

i have 2 tables: drivers and results.

 

the drivers table is simple, it has literally just the drivers firstname and surname.

 

the results table has the columns: id, track, date, driver, race_position, fastest_lap, group_number, group_position

 

i use a form i created to input these values, using the form i enter every driver that entered, their race position, fastest lap etc.

 

once the data is in the form i use multiple functions to get the amount of times they have won the race, won the group etc:

 

function table_get_racewins($connection,$driver)
{
	$sql = "SELECT * FROM results WHERE driver='$driver' AND race_position='1'";

	$rs = mysql_query($sql, $connection);
    	if (mysql_error()) { print "Database Error: $sql " . mysql_error(); }

	$rows = mysql_num_rows($rs);

	return $rows;


}



function table_get_racerunnerup($connection,$driver)
{
	$sql = "SELECT * FROM results WHERE driver='$driver' AND race_position='2'";

	$rs = mysql_query($sql, $connection);
    	if (mysql_error()) { print "Database Error: $sql " . mysql_error(); }

	$rows = mysql_num_rows($rs);

	return $rows;


}

 

 

i use this code to get the data

while ($row_drivers = mysql_fetch_array($result_drivers)) {
extract($row_drivers);
$driver = "$firstname $surname";
$group_wins = table_get_groupwins($connection,$driver);
$group_second = table_get_grouprunnerup($connection,$driver);
$group_third = table_get_groupthird($connection,$driver);
$race_wins = table_get_racewins($connection,$driver);
$race_second = table_get_racerunnerup($connection,$driver);
$race_third = table_get_racethird($connection,$driver);
$race_fourth = table_get_racefourth($connection,$driver);
$race_fifth = table_get_racefifth($connection,$driver);
$race_sixth = table_get_racesixth($connection,$driver);
$race_seventh = table_get_raceseventh($connection,$driver);
$race_eigth = table_get_raceeigth($connection,$driver);
$race_ninth = table_get_raceninth($connection,$driver);
$race_tenth = table_get_racetenth($connection,$driver);

$info = table_make_array($driver,$group_wins,$group_second,$group_third,$race_wins,$race_second,$race_third,$race_fourth,$race_fifth,$race_sixth,$race_seventh,$race_eigth,$race_ninth,$race_tenth);

 

i then call the table_make_array function to make an array for each driver.

 

The table_make_array function:

function table_make_array($driver,$group_wins,$group_second,$group_third,$race_wins,$race_second,$race_third,$race_fourth,$race_fifth,$race_sixth,$race_seventh,$race_eigth,$race_ninth,$race_tenth)
{
	$group_wins_points = GROUP_WIN_POINTS * $group_wins;
	$group_second_points = GROUP_RUNNERUP_POINTS * $group_second;
	$group_third_points = GROUP_THIRD_POINTS * $group_third;
	$race_wins_points = RACE_WIN_POINTS * $race_wins;
	$race_second_points = RACE_RUNNERUP_POINTS * $race_second;
	$race_third_points = RACE_THIRD_POINTS * $race_third;
	$race_fourth_points = RACE_FOURTH_POINTS * $race_fourth;
	$race_fifth_points = RACE_FIFTH_POINTS * $race_fifth;
	$race_sixth_points = RACE_SIXTH_POINTS * $race_sixth;
	$race_seventh_points = RACE_SEVENTH_POINTS * $race_seventh;
	$race_eigth_points = RACE_EIGTH_POINTS * $race_eigth;
	$race_ninth_points = RACE_NINTH_POINTS * $race_ninth;
	$race_tenth_points = RACE_TENTH_POINTS * $race_tenth;

	$points = $group_wins_points + $group_second_points + $group_third_points + $race_wins_points + $race_second_points + $race_third_points + $race_fourth_points + $race_fifth_points + $race_sixth_points + $race_seventh_points + $race_eigth_points + $race_ninth_points + $race_tenth_points;

	$table = array($driver,$group_wins,$group_second,$group_third,$race_wins,$race_second,$race_third,$points);

	return $table;
}

then the results from this are used within a while loop so that for every driver this is output:
[code]
echo "<tr><td class='contenttxt'>$position</td>";
foreach ($info as $x) {
        echo "<td class='contenttxt'>$x</td>";
    }
echo "</tr>";
		  
$position++;

 

the final output is in the url above but i need it ordered by the total points column, but as there is no total points column within the DB i cannot do this, so in order to be able to do what i want with the site i need to work it out after i get it from the DB

[/code]

 

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.