Solved

accessing dynamic var

Posted on 2004-04-07
5
186 Views
Last Modified: 2006-11-17
in a form i use this code

// $numbOpt is set else where for this lets say $numbOpt = 5
$num=0;
while($num < $numbOpt) {
$num++;
print'
input name="opt$num" type="text">';
}      

in the next page is use this code which doesn't work

while($numbOpt > 0){
$Opt = "$opt".$numbOpt;
$query = "insert into PollOptions"
." ( `id` , `Qnumb` , `option` ) values "
."('','$Qid','$Opt')";
mysql_query($query);              
$numbOpt--;
}  

does anyone know how to do this, i can't figure it out. thanks

0
Comment
Question by:ocsurf
5 Comments
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 10779733
Try this:


while($numbOpt > 0){
$Opt = "$opt".$numbOpt;
$query = "insert into PollOptions('id','Qnumb','option') values ('','$Qid','$Opt')";
mysql_query($query);            
$numbOpt--;
}  
0
 
LVL 1

Accepted Solution

by:
ocsurf earned 0 total points
ID: 10779800
you just reposted my code that was cleaned up.

I figured it out  myself

I will post the code for everyone else:

            foreach($_POST as $key => $value)
            {
               if (strpos($key, "opt") === false) {
               }else{
                  $query = "insert into PollOptions"
                  ." ( `id` , `Qnumb` , `option` ) values "
                  ."('','$Qid','$value')";
                  mysql_query($query);                
               }
            }
0
 
LVL 19

Expert Comment

by:Michael701
ID: 10780400
i think you could have also used

$Opt = "$opt".$numbOpt;

echo $Opt.'='.$$Opt;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now