Appending a variable name with another variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Franky
    New Member
    • Mar 2007
    • 14

    Appending a variable name with another variable

    Is there anyway i can append a variable name with another variable? For example:

    $a = 1;

    While ($a <= 5){
    does something in here
    $result$a = $some result from within this loop
    $a++;
    }

    the idea being that at the end i will have $result1, $result2...$res ult5 with values saved in them.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Franky
    Is there anyway i can append a variable name with another variable? For example:

    $a = 1;

    While ($a <= 5){
    does something in here
    $result$a = $some result from within this loop
    $a++;
    }

    the idea being that at the end i will have $result1, $result2...$res ult5 with values saved in them.
    Are you sure that your design is correct? Wouldn't an array be the appropriate data type to use here?

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      Create a CONSTANT
      [PHP]define ('RESULT','$res ult');[/PHP]Concatenate with the '.' operator
      [PHP]$a = 1;

      While ($a <= 5){
      does something in here
      RESULT.$a = $some result from within this loop
      $a++;
      }[/PHP]
      I think this works
      Last edited by code green; Apr 24 '07, 01:10 PM. Reason: mistake

      Comment

      • devsusen
        New Member
        • Feb 2007
        • 136

        #4
        Hi,

        u can use Variable variables in php, though it leads some ambiguity problem. Other way of doing this thing is the associative array. Where variable name cane be the key and value be the value.

        susen

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by devsusen
          Hi,

          u can use Variable variables in php, though it leads some ambiguity problem. Other way of doing this thing is the associative array. Where variable name cane be the key and value be the value.

          susen
          I'd prefer the associative array for this.

          Comment

          • Franky
            New Member
            • Mar 2007
            • 14

            #6
            Thanks for the tips. I tried both, and ended up with the code at the bottom. The problem i have now, is i can't seem to access the data in the array ($QuestionNums) outside the function it is created within. I need it for the process_form function in order to update the db (for now i am just printing them to screen to get it working).
            As well as the main code at the bottom, i have tried the following layout:
            [PHP]if (!isset($_POST['Submit'])){
            //all the stuff from show_form function here
            } else {
            //all the stuff from process_form here[/PHP]

            [PHP]
            //Function called from another page
            function displayQuestion s($ModNum){ //modnum is used in the db query
            global $QuestionNums;

            if (!isset($_POST['Submit'])){
            show_form($ModN um);
            }else{
            process_form($Q uestionNums);
            }//end if
            }//end function "displayQuestio ns"


            function show_form($ModN um)
            {
            global $result, $QuestionNums;
            dbConnect();
            getQuestion($Mo dNum); //call to query in other file. result is $result?>

            <p>Introducti on text here</p>
            <TABLE>
            <FORM method="post" name="Answers" action="<?php echo $PHP_SELF; ?>">

            <?php
            $QuestionNums = array();
            $a=1;
            while ($row = mysql_fetch_ass oc($result))
            { ?>
            <TABLE>
            <TR><TD><b><? echo "$a. ", $row ['Question']. "<br/>" ?></b></TD></TR>
            <TR><TD><inpu t type="radio" name="<?php print 'Question'.$a ?>" value="1"> <? print $row ['Opt_1']. "<br/>" ?></TD></TR>
            <TR><TD><inpu t type="radio" name="<?php print 'Question'.$a ?>" value="2"> <? print $row ['Opt_2']. "<br/>" ?></TD></TR>
            <TR><TD><inpu t type="radio" name="<?php print 'Question'.$a ?>" value="3"> <? print $row ['Opt_3']. "<br/>" ?></TD></TR>
            <TR><TD><inpu t type="radio" name="<?php print 'Question'.$a ?>" value="4"> <? print $row ['Opt_4']. "<br/>" ?></TD></TR>
            </TABLE>
            <?php

            $i=$a;
            $num.$i = $row['Q_Num'];
            $QuestionNums[]=$num.$i;
            print "<br/>";
            $a++;
            }//end while
            //This is just printing them on screen to show they are there - it works
            foreach ($QuestionNums as $value) {
            echo $value."<br>";
            }
            ?>
            <TR><TD><inpu t type="submit" name="Submit" value="Submit"> </TD></TR>
            </FORM>
            </TABLE>
            <?php
            }//end function "show_form"


            function process_form($Q uestionNums)
            {
            global $result, $QuestionNums;

            //Call to ModDBQueries to connect to the database
            dbConnect();

            //TEST TO SEE IF IT WORKS
            foreach ($QuestionNums as $value) {
            echo $value."<br>";
            }
            }//end function "process_fo rm"
            [/PHP]

            With the code above, i get the array printing fine just above the submit button, but when submit is pressed i get:
            Warning: Invalid argument supplied for foreach() in C:\Program Files\xampp\htd ocs\Lilly\PageF iles\ModDisplay Questions2.php on line 78

            Line 78 is "foreach ($QuestionNums as $value) {" in the process_form function.


            Any ideas why i can not get to the data and how to rectify the issue?
            thanks again

            Comment

            • devsusen
              New Member
              • Feb 2007
              • 136

              #7
              Hi Franky,

              u r populating the $QuestionNums array within the function show_form(). Now when u click the submit button then this function show_form() is not called. So u r not populating the array. As a result u r getting the error of - Invalid argument supplied for foreach()

              Populate the array using a function called globally, so while processing the form in the server side (after clicking the submit button) u can populate the array once again.

              susen

              Comment

              • psrinivas33
                New Member
                • Jan 2013
                • 1

                #8
                hi franky At first you have to take a variable like

                Code:
                $result1=$_POST['tot1'];
                $result2=$_POST['tot2'];
                [B].
                .
                .[/B]
                
                $result="result";
                for($i=1 $i<=5 $i++)
                {
                $sql=mysql_query("INSERT INTO <TAB-NAME> VALUES ('$$result[B].[/B]$i'[B],[/B].....)");
                
                }
                enjoyy..
                Last edited by Rabbit; Jan 24 '13, 06:08 PM. Reason: Please use code tags when posting code

                Comment

                Working...