scdas Posted April 24, 2007 Share Posted April 24, 2007 Hi I am writing a maths test which makes up "random" questions. As part of this I wish to choose a random .gif image (eg an equation obtained using the equation editor in MS Word.) from three stored images named in an array: $q6image=array("/q6_1image.gif","/q6_2image.gif","/q6_3image.gif"); The aim is to pass a random .gif image in this First program to Second program (via use of a form & PHP). Question 6 is a question, one of many questions, as part of the test. Below are relevant snippets of code. Skipping down to the end of this my basic question is why does the line: echo "<img src=$q6image[1] >"; works, while the line below doesn't echo "<img src=".$q6image[$_SESSION['value_of_q6rn']].">"; Any hints/suggestions, greatfully received. David First program: //HTML interspersed with <? PHP ?> $q6rn=rand(0,2); //gets a random number in range 0 to 2 $q6image=array("/q6_1image.gif","/q6_2image.gif","/q6_3image.gif"); $_SESSION['value_of_q6rn'] = $q6rn; <!-- question 6 //--> <tr> <td colspan="2" >6. Simplify the expression: <img src=<? echo $q6image[$q6rn] ?> > <br> </td> </tr> Second program: $q6image=array("../q6_1image.gif","../q6_2image.gif","../q6_3image.gif"); // Below works * * * * * * * * * * * * * * * * * * * * * * * * * //echo "<img src=$q6image[1] >"; //Below doesn't work; $_SESSION['value_of_q6rn'] appears to have a NULL value echo "<img src=".$q6image[$_SESSION['value_of_q6rn']].">"; Quote Link to comment https://forums.phpfreaks.com/topic/48445-solved-random-gif-images-in-a-test/ Share on other sites More sharing options...
AndyB Posted April 24, 2007 Share Posted April 24, 2007 ... because you need to have a line like <?php session_start(); ?> to get sessions started before you can set a session variable. Quote Link to comment https://forums.phpfreaks.com/topic/48445-solved-random-gif-images-in-a-test/#findComment-236874 Share on other sites More sharing options...
scdas Posted April 24, 2007 Author Share Posted April 24, 2007 Thanks for the advice re the 'session_start();' code, but I already had that in both programs. Any other suggestions? ... because you need to have a line like <?php session_start(); ?> to get sessions started before you can set a session variable. Quote Link to comment https://forums.phpfreaks.com/topic/48445-solved-random-gif-images-in-a-test/#findComment-237152 Share on other sites More sharing options...
scdas Posted April 24, 2007 Author Share Posted April 24, 2007 FYI Got it to work - the problem appears to be that the array variable won't take a subscripted varaiable. Here is my workaround. $temp=$_SESSION['value_of_q6rn']; //echo $tab4."<img src=$q6image[$_SESSION['value_of_q6rn']] >"; NB Won't accept variable subscript echo $tab4."<img src=$q6image[$temp] >"; Cheers David Quote Link to comment https://forums.phpfreaks.com/topic/48445-solved-random-gif-images-in-a-test/#findComment-237213 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.