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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

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.