Results 1 to 11 of 11

Thread: Unclassified question :)

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    37

    Unclassified question :)

    I don't know under what category to classify this question.

    I have say 17 variables in a HTML page, now they all start with "ans" and have the format "ans1", "ans2", "ans3"....

    I want to read these variables (and perform certain operations) one by one in a single while loop. How can it be done? I have tried various methods but nothing seems to be working.

    I want something like $ans$c where c is incremented.

    Thx.

  2. #2
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Can't you just use an array? $ans[1], $ans[2], etc. That way you could substitute the number for $c.

  3. #3
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    make the variable names

    myvariable[1]= blah
    myvariable[2]= blah blah

    etc...

    Then you can loop it like an array

    for ($x=1; $x<=17; $x++){

    print ($myvariable[$x]);

    }

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    37
    I didn't know we could use arrays in HTML. I'm using this vars to get info from an HTML page. For eg,

    Code:
    <html>
    <form>
    <h2><b>Would you like to : </b></h2>
    1. Do embroidery work.<br>
    <input type="radio" name=ans1 value="Yes">
    Yes
    <input type="radio" name="ans1" value="No">
    No
    <input type="radio" name="ans1" value="Not Sure">
    Not Sure
    <br><br>
    2. Play with the children.<br>
    <input type="radio" name=ans2 value="Yes">
    Yes
    <input type="radio" name="ans2" value="No">
    No
    <input type="radio" name="ans2" value="Not Sure">
    Not Sure
    <br><br>
    Now I have 17 such radio buttons. I want to read what value has been inputted by the user for each and every one of the buttons. Get me?

  5. #5
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    Ok try

    PHP Code:

    for ($x=1$x<=17$x++){

    print (
    $_REQUEST["ans$x"]);



  6. #6
    Lively Member scoutt's Avatar
    Join Date
    Aug 2002
    Posts
    84
    well you could do something like this. and then you could call it like what gimlin had

    Code:
    <html>
    <form>
    <h2><b>Would you like to : </b></h2>
    1. Do embroidery work.<br>
    <input type="radio" name=ans[] value="Yes">
    Yes
    <input type="radio" name="ans[]" value="No">
    No
    <input type="radio" name="ans[]" value="Not Sure">
    Not Sure
    <br><br>
    2. Play with the children.<br>
    <input type="radio" name=ans[] value="Yes">
    Yes
    <input type="radio" name="ans[]" value="No">
    No
    <input type="radio" name="ans[]" value="Not Sure">
    Not Sure
    <br><br>
    or you can try this

    PHP Code:
    while (list ($key$value) = each ($_POST['ans'])){
      echo 
    "Answer #".$key." ="$value ."<br>";

    something like that.
    Just Imagine what you can do with the power of php.

    257 Tutorials and counting.
    529 Snippets and growing.

    Add yours today @
    SnippetLibrary.com

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    37
    Originally posted by Gimlin
    Ok try

    PHP Code:

    for ($x=1$x<=17$x++){

    print (
    $_REQUEST["ans$x"]);


    Thx but it doesnt work.

  8. #8
    Lively Member scoutt's Avatar
    Join Date
    Aug 2002
    Posts
    84
    try

    PHP Code:
    for ($x=1$x<=17$x++){

    print (
    $_REQUEST["ans"][$x]);


    Just Imagine what you can do with the power of php.

    257 Tutorials and counting.
    529 Snippets and growing.

    Add yours today @
    SnippetLibrary.com

  9. #9
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    or maybe?

    PHP Code:
    $var "ans".$x;
    print (
    $_REQUEST[$var]); 
    I'm not entirely sure what you are trying to achieve

  10. #10
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141
    I believe that $_REQUEST[] won't work in earlier PHP versions.

    As long as you put [] after the names of your <input > which share the same name (like scoutt suggested), then PHP will will create an array with that name. You can then use a for() loop to run through the array,whether you use the new $_REQUEST[] collection or not.

  11. #11
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    Originally posted by da_silvy
    or maybe?

    PHP Code:
    $var "ans".$x;
    print (
    $_REQUEST[$var]); 
    I'm not entirely sure what you are trying to achieve
    He wants a form with, for example 4 inputs ans1 ans2 ans3 ans4. He do whatever with them, using a loop.

    I dont know why mine didnt work, I tested it myself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width