www.webdeveloper.com
Results 1 to 7 of 7

Thread: Concatenating two variables

  1. #1
    Join Date
    Mar 2006
    Location
    Liverpool, UK
    Posts
    278

    Concatenating two variables

    Hi all as you can probably see im having a nightmare at the moment with a particular project.

    On a form i have a number of radio buttons named question1, question2, question3 ........ question n.

    Now what Im trying to do is request the values stored in each of these variables. What Im tring to do is the following:

    PHP Code:
    $question=$_REQUEST['question']; /This is how many questions are in the form

    for ($i=1;$i<=$questions;$i++) {
    $t $_REQUEST['question$i'];

    What Im basically tring to do is request the values from the number of variables that are existing. However, the above doesnt seem to work and I have tried all different ways of coding it. Has anyone got any ideas?

  2. #2
    Join Date
    Nov 2006
    Location
    Springfield, MO
    Posts
    1,541
    Inside your for loop I think your variable that's set from the $_REQUEST is wrong, it should be question, not questions. I don't think you need to reference the counter inside the $_REQUEST either. These are merely suggestions as to what I saw wrong.

  3. #3
    Join Date
    Mar 2006
    Location
    Liverpool, UK
    Posts
    278
    I think I may have explained that wrong. Inside the counter I am trying to extract the values stored in the variable question1, question2, question3, question ..... upto the total number of questions. I need a way so that in the for loop the value $t requests the question the $i corresponds to. Hope this explains a little better.

    Thanks anyway

  4. #4
    Join Date
    Mar 2006
    Posts
    413
    PHP Code:
    for ($i=1;$i<=$questions;$i++) {
    $t $_REQUEST['question'.$i];

    maybe? i havent checked.

  5. #5
    Join Date
    Mar 2006
    Posts
    413
    actually after looking at your code, i can see that $t is never going to hold more than 1 value unless you make it an array.

    on another note, why aren't you just using an array of radio buttons instead of just naming them 1,2,3,4,5....n

  6. #6
    Join Date
    Mar 2006
    Location
    Liverpool, UK
    Posts
    278
    because each array (i.e. question number) would have four answers and Im not entirely sure how I would do this.

  7. #7
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062
    I see a couple of issues with your original code.

    PHP Code:
    <?php 

    $t 
    "Questions: ";
    $question $_REQUEST['question']; //This is how many questions are in the form 

    for($i=1;$i<=$question;$i++){ // your '$questions' has an S which did not match your veriable you set.
        
    $t .= $_REQUEST['question{$i}']; // To concatinate the $t vaiable,  and the . befoier the =.
        
    if($i == $question){
            
    $t .= ".";
        }else{
            
    $t .= ", "
        }
    }

    echo(
    $t);
    // "Questions: Question one, Question Two, Question Three."

    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles