Jump to content

[SOLVED] Call variable based on array position


uladk

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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++)

Link to comment
Share on other sites

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?

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.