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++)
{

}
7
  • as your already using sessions why not add the $questions and options array in to the session? Commented Aug 10, 2015 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']; Commented Aug 10, 2015 at 21:47
  • i don't see the bigger picture here, you can away just run the query again. Commented Aug 10, 2015 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. Commented Aug 10, 2015 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. Commented Aug 10, 2015 at 22:11

1 Answer 1

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 />";
            }
        }
    }
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

This did not work. But this issue is resolved. For someone coming in future i used serialize and unserialize.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.