uladk Posted October 2, 2007 Share Posted October 2, 2007 Hello, I'm relatively new to php and wondered if it's possible to call a previously set variable(s) by using an array position. For example if I have variables myvar01, myvar02, myvar03 can I somehow call these in a loop, interchanging the numbers? Here is a simplified version of my code: <?php $title_set = Array( "Title One", "Title Two", "Title Three", ); $q_set1 = Array( "Question one in set one", "Question two in set one", "Question three in set one", ); $q_set2 = Array( "Question one in set two", "Question two in set two", ); $q_set3 = Array( "Question one in set three", "Question two in set three", "Question three in set three", ); echo "<form method='POST' action='?action=send'>\n"; for ($i2 = 0; $i2 < count($title_set); $i2++) { echo "<h1>$title_set[$i2]</h1>"; $q_set_num = $i2 + 01; for ($i = 0; $i < count($q_set#); $i++) { $i_new = $i + 01; echo "<p>$i_new. $q_set#[$i]</p>\n"; echo "<p><textarea></textarea></p>\n"; } } echo "<input type='submit' value='Submit' name='submit'>"; echo '</form>'; ?> There are three sets of Questions (Arrays q_set1, q_set2, q_set3), I want these to be dynamically set in this line for ($i = 0; $i < count($q_set#); $i++) using the title_set array. I've already got $i2 to equal 1, 2 and 3 based on the title_set array, using the for loop. How do I call q_set1, q_set2, q_set3 from that. I tried something like the following: $test = "\$q_set".$i2.""; and calling that like so for ($i = 0; $i < count($test); $i++) . I wasn't surprised when it didn't work... Quote Link to comment Share on other sites More sharing options...
shocker-z Posted October 2, 2007 Share Posted October 2, 2007 Dont know if this is the cleanest and shortest way of doing it (infact im sure it aint either) but this works <?php $title_set = Array( "Title One", "Title Two", "Title Three", ); $q_set1 = Array( "Question one in set one", "Question two in set one", "Question three in set one", ); $q_set2 = Array( "Question one in set two", "Question two in set two", ); $q_set3 = Array( "Question one in set three", "Question two in set three", "Question three in set three", ); echo "<form method='POST' action='?action=send'>\n"; $i=1; foreach ($title_set as $title) { echo "<h1>$title</h1>"; switch ($i) { case 1: foreach ($q_set1 as $q) { echo "<p>$i. $q</p>\n"; echo "<p><textarea></textarea></p>\n"; } break; case 2: foreach ($q_set2 as $q) { echo "<p>$i. $q</p>\n"; echo "<p><textarea></textarea></p>\n"; } break; case 3: foreach ($q_set3 as $q) { echo "<p>$i. $q</p>\n"; echo "<p><textarea></textarea></p>\n"; } break; } $i++; } echo "<input type='submit' value='Submit' name='submit'>"; echo '</form>'; ?> remember you need to give the textarea a name="" too Regards Liam Quote Link to comment Share on other sites More sharing options...
Rithiur Posted October 2, 2007 Share Posted October 2, 2007 How about storing all the questions in single two dimensional array? That is, you could store them as $q_set[$n][$q], where the first index is the number of the question set and the second index is the number of the question in the set. Here is example of what I mean. <?php $title_set = Array( "Title One", "Title Two", "Title Three", ); $q_set[1] = Array( "Question one in set one", "Question two in set one", "Question three in set one", ); $q_set[2] = Array( "Question one in set two", "Question two in set two", ); $q_set[3] = Array( "Question one in set three", "Question two in set three", "Question three in set three", ); echo "<form method='POST' action='?action=send'>\n"; for ($i2 = 0; $i2 < count($title_set); $i2++) { echo "<h1>$title_set[$i2]</h1>"; $q_set_num = $i2 + 01; for ($i = 0; $i < count($q_set[$q_set_num]); $i++) { $i_new = $i + 01; echo "<p>$i_new. {$q_set[$q_set_num][$i]}</p>\n"; echo "<p><textarea></textarea></p>\n"; } } echo "<input type='submit' value='Submit' name='submit'>"; echo '</form>'; ?> Alternatively, if this is not an option for you, you can also use variable variables to refer to variable. For example $varname = "q_set$i2"; for ($i = 0; $i < count($$varname); $i++) Quote Link to comment Share on other sites More sharing options...
uladk Posted October 2, 2007 Author Share Posted October 2, 2007 Now that is beautiful, thank you for giving me an insight into dimensional arrays and variable variables. I was on the right lines just not the syntax etc. I can now have a dynamically generated form based on how many questions I put into the arrays without having a for loop for each question set, that was the goal. It saves a lot of time and html coding if you have long questionnaires. I went in and added name="" to the textarea fields. I made it automated and unique for each by using the unique array index for each question: echo "<p><textarea class='required' name='set".$q_set_num."_".$i_new."' rows='2' cols='100'></textarea> </p>\n"; I'll then be able to use one line of code to POST the form field values. This is the beginning though, it will surely have more options! Thanks to all! By the way, could someone explain the difference between these: echo "<p>$i_new. {$q_set[$q_set_num][$i]}</p>\n"; echo "<p>$i_new. ".$q_set[$q_set_num][$i]."</p>\n"; Both work but I'm interested in how each work. Is it sort of like using brackets in multiplication to determine what order something is calculated but the concatenated method is isolated from the string so it doesn't need them? Quote Link to comment 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.