Page 1 of 1
php variable question
Posted: Mon Jul 11, 2005 5:33 pm
by snvivie
hello, I have a problem because I need to collect information from a form and put it into an array, but I can't seem to be able to store something like $_POST. Here is part of the code, please help:
Code: Select all
$sec1 = array(0 => "N/A");
while($q <= 30)
{
$sec1['$q'] = "$_POST['q2_$q']";
$q++;
}
foreach ($sec1 as $q => $value)
{
echo "$q and $value <br />";
}
JCART | Please use Code: Select all
tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Mon Jul 11, 2005 5:39 pm
by Burrito
use php tags when posting code in the forum.
try getting rid of your single quotes in the key for the array...you're not creating an associative array.
Posted: Mon Jul 11, 2005 5:41 pm
by John Cartwright
Code: Select all
$sec1 = array();
for ($q=0; $q <= 30; $q++) {
$sec1[$q] = $_POST['q2_'.$q];
}
foreach ($sec1 as $q => $value) {
echo "$q and $value <br />";
}
You cannot parse variables inside single quotes.
I see...
Posted: Tue Jul 12, 2005 10:45 am
by snvivie
Oh thank you. But I need to have this statement:
in order to collect data from a form. The multiple radio buttons are named from q2_1 to q2_30, and I want to able to assign them to an array without having to list them all. Is that possible?
Once again, thanks for the help so far. ^_^
Posted: Tue Jul 12, 2005 12:14 pm
by Burrito
if you must use that bit of code, then replace those single quotes with doulbe quotes...
Posted: Tue Jul 12, 2005 12:43 pm
by John Cartwright
Jcart wrote:Code: Select all
for ($q=0; $q <= 30; $q++) {
$sec1[$q] = $_POST['q2_'.$q];
}
snvivie wrote:
Oh thank you. But I need to have this statement:
I don't get it?
thanks!
Posted: Thu Jul 14, 2005 1:02 pm
by snvivie
That worked! Thanks so much, I appreciate all your help on this topic. I do have another questions, however. So far, the script can collect all the data from the form and print it on the page, but I need to have it emailed. However, how would it be possible to email the process below. I tried to assign the results of the foreach function to a variable or an array, but it would not loop, so I all I got was the last answer.
Code: Select all
<?php
$t1s2 = array(1 => "D", 2 => "B", 3 => "E", 4 => "C", 5 => "A", 6 => "B", 7 => "A", 8 => "D", 9 => "E", 10 => "B", 11 => "D", 12 => "D", 13 => "E", 14 => "D", 15 => "B", 16 => "A", 17 => "A", 18 => "C", 19 => "A", 20 => "B", 21 => "B", 22 => "C", 23 => "B", 24 => "A");
$t1s2c = 0;
$t1s2s = array(0 => "N/A");
while($q <= 30)
{
$var = $_POST["q2_$q"];
$t1s2s["$q"] = "$var";
$q++;
}
echo "<P><table border=1><tr><td align=left colspan=4><b>Section 2</b></td>"
."<tr><td align=center>#</td><td align=center>R</td>"
."<td align=center>A</td><td align=center>W</td></tr>";
// I would like the result of this to be printed in an email message
foreach ($t1s2s as $n => $ss)
{ foreach ($t1s2 as $k => $sw)
{if ($n == $k)
{ if ($ss == $sw)
{ $t1s2c++;
echo "<tr><td>$n.</td><td>$sw</td><td>$ss</td><td> </td></tr>";
}
else
echo "<tr><td>$n.</td><td>$sw</td><td>$ss</td><td>x</td></tr></tr>";
} }
}
echo "<tr><td colspan=4>$t1s2c correct.</td></tr></table><P>";
Thanks again.
-Vi
Posted: Thu Jul 14, 2005 2:21 pm
by titaniumdoughnut
The best way to modify your existing code to email the result instead of printing it is to add this line near the top (before the first echo command):
Then change each occurrence of "echo" like this:
Code: Select all
$txt .= "<tr><td>$n.</td><td>$sw</td><td>$ss</td><td> </td></tr>";
To send mail use this:
Code: Select all
mail("to address", "subject", $txt, "from address");
I wouldn't recommend using the tables though, best to arrange the output a different way for email, seeing as HTML email is often irritating.