stevepatd Posted March 6, 2023 Share Posted March 6, 2023 I have a database table with 52 values (one for every week of the year). The column names are Week1, Week2, etc. When processing this I would like to use a 'for' loop from 1 to 52 and create something like this: for ($n=1; $n<=52; $n++) { $variable = '$Week' . $n; if ($variable == something) { do the thing; } }// for then if the value in that weeks cell matches the expected value I do my thing. How can I dynamically create my "variable" that is the name of the variable so I can test the value of it? I don't want to write a little snipet of code 52 times to see if $Week1 == something, $Week2 == something...... Quote Link to comment https://forums.phpfreaks.com/topic/315977-dynamically-naming-a-variable/ Share on other sites More sharing options...
Solution stevepatd Posted March 6, 2023 Author Solution Share Posted March 6, 2023 Sorry to have bothered you all. I figured it out. Instead of using a variable for each column of the database I can use the $row array after the mysqli_fetch_array. That makes it really sweet. I really appreciate this site and all the effort you experts put into answering our questions. Quote Link to comment https://forums.phpfreaks.com/topic/315977-dynamically-naming-a-variable/#findComment-1606229 Share on other sites More sharing options...
requinix Posted March 6, 2023 Share Posted March 6, 2023 2 hours ago, stevepatd said: Instead of using a variable for each column of the database I can use the $row array after the mysqli_fetch_array. That makes it really sweet. That is also very, very much the more appropriate solution than creating dynamically-named variables. So that's great 👍 Quote Link to comment https://forums.phpfreaks.com/topic/315977-dynamically-naming-a-variable/#findComment-1606230 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.