Jump to content

[SOLVED] Question on looping variables - Variable won't increment!


kjtocool

Recommended Posts

I want to increment a php variable in a loop ... hard to explain ... let me show you

 

$rank = 1;

foreach (something) {

$variable[$rank] = something;

$rank++;

}

 

So basically, I want it to assign the first value of an array to  $variable1, the second to $variable2, etc.

 

I can't figure out the correct syntax?

<?php
$rank = 1;
$variable = "movie";
foreach($actuals_array AS $name => $gross) {
$variable . $rank = $name;
echo $variable1;
}
?>

 

I tried the above, it echo's a blank.

 

I want $variable1 then $variable2 then $variable3, etc.

Having an issue with the loop now:

 

<?php
while ($row) {
     $${'place_' . $place . '_score'} = $row['score'];
     echo $place_1_score . "<br />";

     $place++;
     $row = mysqli_fetch_assoc($result);
?>

 

The above will echo blank lines all the way down, but if I replace $place_1_score in the echo statement with: $${'place_' . $place . '_score'}, then it echo's fine.

 

What is going wrong?

Maybe I should fully show my intentions:

 

<?php
... code

// Gets the last week_id with actuals, and the actual values
$query = "SELECT * FROM bonanza_actuals ORDER BY week_id DESC LIMIT 1";
$result = mysqli_query($databaseConnect, $query);
$row = mysqli_fetch_assoc($result);
$week_id = $row['week_id'];
$movie_1_actual_name = $row['movie_1_name'];
$movie_1_actual_gross = $row['movie_1_gross'];
$movie_2_actual_name = $row['movie_2_name'];
$movie_2_actual_gross = $row['movie_2_gross'];
$movie_3_actual_name = $row['movie_3_name'];
$movie_3_actual_gross = $row['movie_3_gross'];
$movie_4_actual_name = $row['movie_4_name'];
$movie_4_actual_gross = $row['movie_4_gross'];
$movie_5_actual_name = $row['movie_5_name'];
$movie_5_actual_gross = $row['movie_5_gross'];
$movie_6_actual_name = $row['movie_6_name'];
$movie_6_actual_gross = $row['movie_6_gross'];
$movie_7_actual_name = $row['movie_7_name'];
$movie_7_actual_gross = $row['movie_7_gross'];
$movie_8_actual_name = $row['movie_8_name'];
$movie_8_actual_gross = $row['movie_8_gross'];
$movie_9_actual_name = $row['movie_9_name'];
$movie_9_actual_gross = $row['movie_9_gross'];
$movie_10_actual_name = $row['movie_10_name'];
$movie_10_actual_gross = $row['movie_10_gross'];
mysqli_free_result($result);

$actuals_array = array("$movie_1_actual_name" => $movie_1_actual_gross, "$movie_2_actual_name" => $movie_2_actual_gross, "$movie_3_actual_name" => $movie_3_actual_gross, "$movie_4_actual_name" => $movie_4_actual_gross, "$movie_5_actual_name" => $movie_5_actual_gross, "$movie_6_actual_name" => $movie_6_actual_gross, "$movie_7_actual_name" => $movie_7_actual_gross, "$movie_8_actual_name" => $movie_8_actual_gross, "$movie_9_actual_name" => $movie_9_actual_gross, "$movie_10_actual_name" => $movie_10_actual_gross);

asort($actuals_array, SORT_NUMERIC);

$rank = 10;
foreach($actuals_array AS $name => $gross) {
	$${'movie_' . $rank . '_actual_name'} = $name;
	$${'movie_' . $rank . '_actual_gross'} = $gross;
	$rank--;
}
?>

 

Essentially I do the following:

 

First I get the movie names and how much money they made from the database.  They are stored there in no particular order.  Second, I sort the array numerically (it will give the lowest number first, so they come out in reverse order, thus why $rank starts at 10).

 

I want to loop through each item of the array, in reverse order, set the first item out of the array to the previously defined variable, $movie_10_actual_name, and $movie_10_actual_gross.  And then so on through the 10 values in the array all the way down to #1.  Thus giving me the values, ordered properly from highest to lowest.

 

But it doesn't assign the value to the variable, and that's my issue.

you have one $ much

try

...
foreach($actuals_array AS $name => $gross) {
	${'movie_' . $rank . '_actual_name'} = $name;
	${'movie_' . $rank . '_actual_gross'} = $gross;
	$rank--;
}

 

Indeed, now it's working!  I could kiss you  ;D

Archived

This topic is now archived and is closed to further replies.

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