Jump to content

Recommended Posts

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']].">";

 

Link to comment
https://forums.phpfreaks.com/topic/48445-solved-random-gif-images-in-a-test/
Share on other sites

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.