Accessing an array

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

    Accessing an array

    Following discussion in a previous post, i have used an array inside a loop to gather results:

    [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]

    I want to add the values to the array in the first function, then use it in the second (at the moment i'm justing displaying them to make sure it works).
    I can't seem to pass it between them - would this have something to do with the fact that i have 1 function that says if submit, do this function, else do that function (and it cant pass the array/isnt returning it to this 1st function)?

    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 Quest ions2.php on line 78

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

    I have tried it using the following layout as well with no luck:
    [PHP]
    if (!isset($_POST['Submit'])){
    //all the stuff from show_form function here
    } else {
    //all the stuff from process_form here
    [/PHP]
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    I am not entirely sure what you are doing here. Perhaps you could put together a simple test case that shows the problem you are having?

    Comment

    • Franky
      New Member
      • Mar 2007
      • 14

      #3
      It is displaying a number of questions sourced from the database (in the example, it selects 5 from the db and loops through and displays them).

      The process_form function takes the user's answers to these questions and (when its working) will enter these into the db. These can then be checked against the correct answer and the user scored appropriately.

      But in order to store them, i need the question number (not 1 - 5 from the form, but the number of the task as it is in the db). That is why i have the array in the loop to collect the "Q_Num". I then need this in the process_form function to use in the update query. (At the moment they are just being printed onto the screen to show it works).

      The issue is accessing the array in the process_form function.

      So it should go:
      - user comes to page
      - query run and question data returned in variable $result
      - loop used to display question, opt_1...opt_4 for each row of $result
      - in this loop, array $QuestionNum captures the db q_num for later use
      - user completes test and clicks submit
      - array $QuestionNums is passed to process_form
      - this array and $_POST is used in db query to update table with answer ($_POST) in the relevant question row ($questionnums) .

      hope that makes sense!

      Comment

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

        #4
        $QuestionNums is empty in process_form(). I am confused over which $QuestionNums you are hoping to use. You have it as a function argument and a global. But it is not declared as a global, only local in displayQuestion s(), but from there you call process_form() but pass an empty array. I would re-design your functions a little better. Also I was taught that use of globals was a crime.

        Comment

        • Franky
          New Member
          • Mar 2007
          • 14

          #5
          I've tried so many different things that i had it declared in many places. I have changed it so it is now:
          [PHP]
          function displayQuestion s($ModNum){
          if (!isset($_POST['Submit'])){
          show_form($ModN um);
          }else{
          process_form($Q uestionNums);
          }//end if
          }//end function "displayQuestio ns"
          [/PHP]

          I want the array $QuestionNums to be created and filled in show_form and then passed to process_form to use the data placed in it - so the same array. Can i do that by simply putting return $QuestionNums at the end of the function show_form and then calling process_form as above?

          I'm pretty new to PHP so all help is great. thanks

          Comment

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

            #6
            You are trying to run before you can walk. Lets take it step by step.
            I want the array $QuestionNums to be created and filled in show_form and then passed to process_form to use the data placed in it
            Unfortunately, that is not what your code says:
            [PHP]if (!isset($_POST['Submit'])){
            show_form($ModN um);
            }else{
            process_form($Q uestionNums);[/PHP]You call show_form() OR call process_form()
            I think this is what you mean
            [PHP]if (!isset($_POST['Submit'])){
            $QuestionNums = show_form($ModN um);
            process_form($Q uestionNums);
            }[/PHP]
            Can i do that by simply putting return $QuestionNums at the end of the function show_form
            Yes.

            [PHP]function show_form($ModN um)
            {

            $QuestionNums = array();
            //fill the array etc
            return $QuestionNums;
            }[/PHP]
            This will take you a little closer

            Comment

            • Franky
              New Member
              • Mar 2007
              • 14

              #7
              OK thats cool. But the bit at the start does both of them regardless:
              [PHP]
              if (!isset($_POST['Submit'])){
              $QuestionNums = show_form($ModN um);
              process_form($Q uestionNums);
              }
              [/PHP]
              I need it to display the questions (the show_form function) and when submit is pressed it processes the form (process_form function).

              With the previous suggestion, it does both function and then when submit is pressed it just presents a page with the standard page template, but nothing in the content part, as it sees the array $QuestionNums past to it as empty (and hence has nothing to print to the screen).

              Comment

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

                #8
                I need it to display the questions (the show_form function) and when submit is pressed it processes the form (process_form function).
                As I previously said, you need to look at your design. Why do you want to pass the questions to the process() function? Surely you want to pass the answers.
                But the bit at the start does both of them regardless:
                Regardless of what? If you want the questions displayed upon initial entry to the script then the answers processed upon form submission this is quite easy to achieve, but I am still not sure what you are trying to do .

                Comment

                Working...