0

I looked around before PHP - How to send an array to another page?, but my situation is different.

I have this program on a page, this is the query block:

while ($row = sqlsrv_fetch_array($query))
{   
    $questions[] = "$row[Question]";
    $optionA[] = "$row[OptionA]";
    $optionB[] = "$row[OptionB]";
    $optionC[] = "$row[OptionC]";
    $optionD[] = "$row[OptionD]";
}

Then I do something like this to echo questions on screen

$lengthquestion = count($questions);
$_SESSION['length'] = $lengthquestion;
for($i = 0; $i < $lengthquestion; $i++)
{
    echo $questions[$i];
}

Now I want to be able to access this questions array in a different php file. But where I get confused/stuck with what I have been trying is that I would still need to access each element of the array on the next page. How do I go about it?

So far I have been playing with sessions, but then I get array to string conversion error.

Second php page:

$UpperLimit = $_SESSION['length']; 
for($i = 0; $i < $UpperLimit; $i++)
{

}
Community
  • 1
  • 1
USER420
  • 337
  • 3
  • 12
  • as your already using sessions why not add the `$questions` and options array in to the session? –  Aug 10 '15 at 21:38
  • 1
    Each time $i can be different. This is what i had tried but did not work. So maybe i did this wrong. In my first page i had $_SESSION['Questions'] = $questions; In my second page i had $QUEST = $_SESSION['Questions']; – USER420 Aug 10 '15 at 21:47
  • i don't see the bigger picture here, you can away just run the query again. –  Aug 10 '15 at 21:49
  • Yes, but that was my last resort. Its just query i run depends on a different variable. So, if I cannot accomplish this, then i was going to run queries. – USER420 Aug 10 '15 at 21:57
  • In order to put an array in a session, you need to `serialize()` it and then `unserialize()` it in the other script where you want to use the array. – Charlotte Dunois Aug 10 '15 at 22:11
  • you could also consider JSON, maybe one day you rewrite one side in another language one day. (json_encode and json_decode) – maraca Aug 10 '15 at 23:09
  • @CharlotteDunois no you dont, $_SESSIONS is already an array –  Aug 11 '15 at 01:31

1 Answers1

0

Posting an array to another page with Sessions is easy but I would also recommend using multidimensional arrays instead.

Example (tested code):

<?php
session_start();
$i = 0;
$questions = Array();

for($i = 0; $i < 5; $i++){
    $questions[$i]['question'] = "test  ". $i;
    $questions[$i]['option'][] = "optionA ". $i;
    $questions[$i]['option'][] = "optionB ". $i;
    $questions[$i]['option'][] = "optionC ". $i;
    $questions[$i]['option'][] = "optionD ". $i;
}

$_SESSION['questions'] = $questions;

// ******* from here down can go on another page.  Don't forget session_start();

$questions_session = $_SESSION['questions'];

if(is_array($questions_session)){
    foreach($questions_session as $key => $questions){
        foreach($questions as $q => $question){
            if(is_array($question)){
                // This is an option, loop through and post each option.
                foreach($question as $key => $option){
                   echo " - ".$option."<br />";
                }
            } else {
                // This is the question, post it to the screen
                echo "Question : ". $question . "<br />";
            }
        }
    }
}
?>
  • This did not work. But this issue is resolved. For someone coming in future i used serialize and unserialize. – USER420 Aug 11 '15 at 19:36