kjtocool Posted January 6, 2008 Share Posted January 6, 2008 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 https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/ Share on other sites More sharing options...
revraz Posted January 6, 2008 Share Posted January 6, 2008 You want $variable[1] or $variable1 ? Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-431896 Share on other sites More sharing options...
kjtocool Posted January 6, 2008 Author Share Posted January 6, 2008 <?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. Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-431907 Share on other sites More sharing options...
trq Posted January 6, 2008 Share Posted January 6, 2008 <?php $rank = 1; foreach($actuals_array AS $name => $gross) { $${'movie' . $rank} = $name; } echo $movie1; ?> Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-431909 Share on other sites More sharing options...
kjtocool Posted January 6, 2008 Author Share Posted January 6, 2008 Worked like a charm, thanks so much, never would have got that. Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-431916 Share on other sites More sharing options...
kjtocool Posted January 6, 2008 Author Share Posted January 6, 2008 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 https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-431979 Share on other sites More sharing options...
kjtocool Posted January 6, 2008 Author Share Posted January 6, 2008 Anyone? Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-432034 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 Question: what does this do: $${'place_' . $place . '_score'} ? ??? Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-432046 Share on other sites More sharing options...
kjtocool Posted January 6, 2008 Author Share Posted January 6, 2008 I can't say for sure, I got it from thorpe. It seems to create some type of concatenated variable, but I can't seem to call it. Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-432056 Share on other sites More sharing options...
revraz Posted January 6, 2008 Share Posted January 6, 2008 Where is your $place = 1 line to start it? Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-432057 Share on other sites More sharing options...
trq Posted January 6, 2008 Share Posted January 6, 2008 Question: what does this do: $${'place_' . $place . '_score'} ? It creates a variable variable. What exactly are you trying to do? It seems to me you may simply be going about it all the wrong way. Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-432058 Share on other sites More sharing options...
kjtocool Posted January 6, 2008 Author Share Posted January 6, 2008 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 https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-432060 Share on other sites More sharing options...
kjtocool Posted January 6, 2008 Author Share Posted January 6, 2008 any ideas? Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-432169 Share on other sites More sharing options...
kjtocool Posted January 7, 2008 Author Share Posted January 7, 2008 thorpe, did you get a chance to look at this again? Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-432721 Share on other sites More sharing options...
sasa Posted January 7, 2008 Share Posted January 7, 2008 you have one $ much try ... foreach($actuals_array AS $name => $gross) { ${'movie_' . $rank . '_actual_name'} = $name; ${'movie_' . $rank . '_actual_gross'} = $gross; $rank--; } Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-432764 Share on other sites More sharing options...
kjtocool Posted January 7, 2008 Author Share Posted January 7, 2008 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 Link to comment https://forums.phpfreaks.com/topic/84756-solved-question-on-looping-variables-variable-wont-increment/#findComment-432913 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.