Re: Simple newbie problem, please help!
| From: | Urs van Binsbergen | Date: | Fri, 17 Nov 2000 16:21:18 +0000 |
| Subject: | Re: Simple newbie problem, please help! | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-25916@lists.php.net to get a copy of this message | ||
Hi
either
<?
$q1answer = "some answer";
$q2answer = "another answer";
// and so on..
for ($i = 1; $i <= 10; $i++) {
$varname = "q".$i."answer;
echo "<td width=\"25%\" bgcolor=\"#66CCFF\">";
echo "<p align=\"center\"><font face=\"Arial\"
size=\"2\">$$varname</font></td>";//will call the variable named like
the
string in $varname
?>
or
<?
$arr = array("some answer","another answer","the third
answer",...);//replace the ... by the other 10 answers
reset($arr);
while(list($key,$value)=each($arr))
{
echo "<td width=\"25%\" bgcolor=\"#66CCFF\">";
echo "<p align=\"center\"><font face=\"Arial\"
size=\"2\">$value</font></td>";
}
?>
As you can see the second solution is much more elegant.
You can also use arrays in forms:
<input type=checkbox name="var[0]" value="anything">
<input type=checkbox name="var[1]" value="anything">
<input type=checkbox name="var[2]" value="anything">
This will make the $HTTP_POST_VARS["var"] an array containing the checked
boxes.
Regards,
Urs
> Hi everyone, I have a simple loop problem.
>
> Let's say I have 10 quiz questions, and for each one I have a variable,
>i.e. q1answer, q2answer, etc... and in a for loop, I want to print them
>out in a table. So I do something like this:
>
>for ($i = 1; $i <= 10; $i++) {
>echo "<td width=\"25%\" bgcolor=\"#66CCFF\">";
>echo "<p align=\"center\"><font face=\"Arial\"
>size=\"2\">VARIABLE
>HERE</font></td>";
>
>My problem is I don't know how to construct the VARIABLE HERE part. I
>tried doing some kind of concatentation like q.$i.ans but that is
>obviously wrong. Can someone please steer me in the right direction...
>if an array is necessary, what needs to be done to get the 10 vars into
>the array?
>
> Thanks so much in advance,
> Omar