php variable question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
snvivie
Forum Newbie
Posts: 3
Joined: Mon Jul 11, 2005 5:30 pm
Location: Chicago

php variable question

Post 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]
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
snvivie
Forum Newbie
Posts: 3
Joined: Mon Jul 11, 2005 5:30 pm
Location: Chicago

I see...

Post by snvivie »

Oh thank you. But I need to have this statement:

Code: Select all

$sec1['$q'] = $_POST['q2_$q'];
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. ^_^
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

if you must use that bit of code, then replace those single quotes with doulbe quotes...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:

Code: Select all

$sec1['$q'] = $_POST['q2_$q'];
I don't get it?
User avatar
snvivie
Forum Newbie
Posts: 3
Joined: Mon Jul 11, 2005 5:30 pm
Location: Chicago

thanks!

Post 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>&nbsp;</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
User avatar
titaniumdoughnut
Forum Commoner
Posts: 33
Joined: Wed Jul 13, 2005 2:02 pm
Location: Manhattan

Post 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):

Code: Select all

$txt = "";
Then change each occurrence of "echo" like this:

Code: Select all

$txt .= "<tr><td>$n.</td><td>$sw</td><td>$ss</td><td>&nbsp;</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.
Post Reply