Jump to content

Recommended Posts

I am trying to create a function that will sort 10 user name / value pairs by the value.

I am currently doing so by inputing values into an array, and then using ksort(), with an SORT_NUMERIC flag.

Some of the code:

[code]
<?php

// Here is the not fully developed function.
function order_movies($name1, $gross1, $name2, $gross2, $name3, $gross3, $name4, $gross4, $name5, $gross5, $name6, $gross6, $name7, $gross7, $name8, $gross8, $name9, $gross9, $name10, $gross10) {
$array = array("$name1" => $gross1, "$name2" => $gross2);
ksort($array, SORT_NUMERIC);
return $array;
}

// First I get the users input.
$movie_1_name = $_POST['movie_1_name'];
$movie_1_prediction = $_POST['movie_1_prediction'];
$movie_2_name = $_POST['movie_2_name'];
$movie_2_prediction = $_POST['movie_2_prediction'];
$movie_3_name = $_POST['movie_3_name'];
$movie_3_prediction = $_POST['movie_3_prediction'];
$movie_4_name = $_POST['movie_4_name'];
$movie_4_prediction = $_POST['movie_4_prediction'];
$movie_5_name = $_POST['movie_5_name'];
$movie_5_prediction = $_POST['movie_5_prediction'];
$movie_6_name = $_POST['movie_6_name'];
$movie_6_prediction = $_POST['movie_6_prediction'];
$movie_7_name = $_POST['movie_7_name'];
$movie_7_prediction = $_POST['movie_7_prediction'];
$movie_8_name = $_POST['movie_8_name'];
$movie_8_prediction = $_POST['movie_8_prediction'];
$movie_9_name = $_POST['movie_9_name'];
$movie_9_prediction = $_POST['movie_9_prediction'];
$movie_10_name = $_POST['movie_10_name'];
$movie_10_prediction = $_POST['movie_10_prediction'];

// Now I pass the values to the function, and get my ksort array back
$array = order_movies($movie_1_name, $movie_1_prediction, $movie_2_name, $movie_2_prediction, $movie_3_name, $movie_3_prediction, $movie_4_name, $movie_4_prediction, $movie_5_name, $movie_5_prediction, $movie_6_name, $movie_6_prediction, $movie_7_name, $movie_7_prediction, $movie_8_name, $movie_8_prediction, $movie_9_name, $movie_9_prediction, $movie_10_name, $movie_10_prediction);

print_r($array);
?>

 

If the user enters 100 for the first movie, "movie a", and 12 for the second movie, "movie b", then.

 

Output:

 

Array ( [movie b] => 12 [movie a] => 100 )

 

If I then switch the input, and make movie a's value lower, like 11, it will still be in the second position.  Any help on how I can get a function like this working?[/code]

OK, so I fixed the above issue, but am now having a different problem:

 


<?php
// Returns the movies in reverse order, starting with the smallest, and going to the biggest.
function order_movies($name1, $gross1, $name2, $gross2, $name3, $gross3, $name4, $gross4, $name5, $gross5, $name6, $gross6, $name7, $gross7, $name8, $gross8, $name9, $gross9, $name10, $gross10) {
$array = array("$name1" => $gross1, "$name2" => $gross2, "$name3" => $gross3, "$name4" => $gross4, "$name5" => $gross5, "$name6" => $gross6, "$name7" => $gross7, "$name8" => $gross8, "$name9" => $gross9, "$name10" => $gross10);
asort($array, SORT_NUMERIC);

$movie_rank = 10
        // The next line is # 263 throwing an error
foreach ($array as $name => $gross) {
	if ($movie_rank == 10) {
		$movie_10_name = $name;
		$movie_10_prediction = $gross;
	}
	else if ($movie_rank == 9) {
		$movie_9_name = $name;
		$movie_9_prediction = $gross;
	}
	else if ($movie_rank ==  {
		$movie_8_name = $name;
		$movie_8_prediction = $gross;
	}
	else if ($movie_rank == 7) {
		$movie_7_name = $name;
		$movie_7_prediction = $gross;
	}
	else if ($movie_rank == 6) {
		$movie_6_name = $name;
		$movie_6_prediction = $gross;
	}
	else if ($movie_rank == 5) {
		$movie_5_name = $name;
		$movie_5_prediction = $gross;
	}
	else if ($movie_rank == 4) {
		$movie_4_name = $name;
		$movie_4_prediction = $gross;
	}
	else if ($movie_rank == 3) {
		$movie_3_name = $name;
		$movie_3_prediction = $gross;
	}
	else if ($movie_rank == 2) {
		$movie_2_name = $name;
		$movie_2_prediction = $gross;
	}
	else if ($movie_rank == 1) {
		$movie_1_name = $name;
		$movie_1_prediction = $gross;
	}
}
}


$movie_1_name = $_POST['movie_1_name'];
$movie_1_prediction = $_POST['movie_1_prediction'];
$movie_2_name = $_POST['movie_2_name'];
$movie_2_prediction = $_POST['movie_2_prediction'];
$movie_3_name = $_POST['movie_3_name'];
$movie_3_prediction = $_POST['movie_3_prediction'];
$movie_4_name = $_POST['movie_4_name'];
$movie_4_prediction = $_POST['movie_4_prediction'];
$movie_5_name = $_POST['movie_5_name'];
$movie_5_prediction = $_POST['movie_5_prediction'];
$movie_6_name = $_POST['movie_6_name'];
$movie_6_prediction = $_POST['movie_6_prediction'];
$movie_7_name = $_POST['movie_7_name'];
$movie_7_prediction = $_POST['movie_7_prediction'];
$movie_8_name = $_POST['movie_8_name'];
$movie_8_prediction = $_POST['movie_8_prediction'];
$movie_9_name = $_POST['movie_9_name'];
$movie_9_prediction = $_POST['movie_9_prediction'];
$movie_10_name = $_POST['movie_10_name'];
$movie_10_prediction = $_POST['movie_10_prediction'];


$array = order_movies($movie_1_name, $movie_1_prediction, $movie_2_name, $movie_2_prediction, $movie_3_name, $movie_3_prediction, $movie_4_name, $movie_4_prediction, $movie_5_name, $movie_5_prediction, $movie_6_name, $movie_6_prediction, $movie_7_name, $movie_7_prediction, $movie_8_name, $movie_8_prediction, $movie_9_name, $movie_9_prediction, $movie_10_name, $movie_10_prediction);
?>

 

The above is throwing an error:

 

Parse error: syntax error, unexpected T_FOREACH in /home/worldofk/public_html/bonanza_entryVal.php on line 263

Yeah, thanks.  Silly mistake.

 

Now it's throwing another error:

 

<?php
function order_movies($name1, $gross1, $name2, $gross2, $name3, $gross3, $name4, $gross4, $name5, $gross5, $name6, $gross6, $name7, $gross7, $name8, $gross8, $name9, $gross9, $name10, $gross10) {
$array = array("$name1" => $gross1, "$name2" => $gross2, "$name3" => $gross3, "$name4" => $gross4, "$name5" => $gross5, "$name6" => $gross6, "$name7" => $gross7, "$name8" => $gross8, "$name9" => $gross9, "$name10" => $gross10);
asort($array, SORT_NUMERIC);

print_r($array);
echo "<br /><br />";
}

if (isset($_SESSION['user_name'])) {
$user_name = $_SESSION['user_name'];
$user_id = $_SESSION['user_id'];
$week_id = $_POST['week_id'];

$movie_1_name = $_POST['movie_1_name'];
$movie_1_prediction = $_POST['movie_1_prediction'];
$movie_2_name = $_POST['movie_2_name'];
$movie_2_prediction = $_POST['movie_2_prediction'];
$movie_3_name = $_POST['movie_3_name'];
$movie_3_prediction = $_POST['movie_3_prediction'];
$movie_4_name = $_POST['movie_4_name'];
$movie_4_prediction = $_POST['movie_4_prediction'];
$movie_5_name = $_POST['movie_5_name'];
$movie_5_prediction = $_POST['movie_5_prediction'];
$movie_6_name = $_POST['movie_6_name'];
$movie_6_prediction = $_POST['movie_6_prediction'];
$movie_7_name = $_POST['movie_7_name'];
$movie_7_prediction = $_POST['movie_7_prediction'];
$movie_8_name = $_POST['movie_8_name'];
$movie_8_prediction = $_POST['movie_8_prediction'];
$movie_9_name = $_POST['movie_9_name'];
$movie_9_prediction = $_POST['movie_9_prediction'];
$movie_10_name = $_POST['movie_10_name'];
$movie_10_prediction = $_POST['movie_10_prediction'];


$array = order_movies($movie_1_name, $movie_1_prediction, $movie_2_name, $movie_2_prediction, $movie_3_name, $movie_3_prediction, $movie_4_name, $movie_4_prediction, $movie_5_name, $movie_5_prediction, $movie_6_name, $movie_6_prediction, $movie_7_name, $movie_7_prediction, $movie_8_name, $movie_8_prediction, $movie_9_name, $movie_9_prediction, $movie_10_name, $movie_10_prediction);

$movie_rank = 10;
        // The next line is 265 and throwing an error
foreach ($array as $name => $gross) {
	if ($movie_rank == 10) {
		$movie_10_name = $name;
		$movie_10_prediction = $gross;
	}
	else if ($movie_rank == 9) {
		$movie_9_name = $name;
		$movie_9_prediction = $gross;
	}
	else if ($movie_rank ==  {
		$movie_8_name = $name;
		$movie_8_prediction = $gross;
	}
	else if ($movie_rank == 7) {
		$movie_7_name = $name;
		$movie_7_prediction = $gross;
	}
	else if ($movie_rank == 6) {
		$movie_6_name = $name;
		$movie_6_prediction = $gross;
	}
	else if ($movie_rank == 5) {
		$movie_5_name = $name;
		$movie_5_prediction = $gross;
	}
	else if ($movie_rank == 4) {
		$movie_4_name = $name;
		$movie_4_prediction = $gross;
	}
	else if ($movie_rank == 3) {
		$movie_3_name = $name;
		$movie_3_prediction = $gross;
	}
	else if ($movie_rank == 2) {
		$movie_2_name = $name;
		$movie_2_prediction = $gross;
	}
	else if ($movie_rank == 1) {
		$movie_1_name = $name;
		$movie_1_prediction = $gross;
	}
	$movie_rank--;
}

echo $movie_1_name . " " . $movie_2_name . " " . $movie_3_name . " " . $movie_4_name. " " . $movie_5_name . " " . $movie_6_name . " " . $movie_7_name . " " . $movie_8_name . " " . $movie_9_name . " " . $movie_10_name;
?>

 

Error:

 

Warning: Invalid argument supplied for foreach() in /home/worldofk/public_html/bonanza_entryVal.php on line 265

OK, Now I am wondering, how do I sort DESC?

 

My sort is working fine, but it's sorting smallest to biggest, I want it to sort biggest to smallest.  Is this possible?

 

Current code:

 

<?php
asort($scores_array, SORT_NUMERIC);

$rank = 0;
foreach ($scores_array as $user_id => $score) {
echo "UID: " . $user_id . " = Score: ." . $score . " Rank: " . $rank . "<br />";
$rank++;
}?>

 

Prints:

 

UID: 2529 = Score: .8.65 Rank: 0
UID: 539 = Score: .10.172 Rank: 1
UID: 1140 = Score: .10.717 Rank: 2
UID: 74 = Score: .13.333 Rank: 3
UID: 2 = Score: .14.444 Rank: 4
UID: 4150 = Score: .21.381 Rank: 5
UID: 107 = Score: .21.692 Rank: 6
UID: 2809 = Score: .30.333 Rank: 7
UID: 66 = Score: .32.477 Rank: 8
UID: 2519 = Score: .35.167 Rank: 9
UID: 4153 = Score: .35.775 Rank: 10
UID: 3200 = Score: .38.684 Rank: 11
UID: 52 = Score: .40.638 Rank: 12
UID: 268 = Score: .40.881 Rank: 13
UID: 149 = Score: .46.728 Rank: 14
UID: 34 = Score: .52.452 Rank: 15
UID: 13 = Score: .53.271 Rank: 16
UID: 33 = Score: .62.935 Rank: 17
UID: 7 = Score: .65.381 Rank: 18
UID: 2400 = Score: .71.954 Rank: 19
UID: 535 = Score: .83.06 Rank: 20
UID: 38 = Score: .86.27 Rank: 21

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.