Page 1 of 1

(SOLVED) Newbie problem with passing variables and arrays.

Posted: Wed Aug 09, 2006 12:19 pm
by PapiSolo
Hi all,

I recently start medling with PHP and so far all I can say is WOW! I'm enjoying it imensely!

i have however come into an obstacle that my newbie brain is having a hard time getting around. The problem is as follows:

I'm programming a psicological experiment for a friend where participants have to look at various images and rate them. (Info is sent to a DB for later analysis)
The first requirement is that the images apear in a random order for each new participant. I created an array with all 21 images/forms (html files), shuffled that array and displayed the file in array[$_i] ($_i = 0). The participant rates the image, hits the submit button and then...

What i would like is that $_i increment and after displaying the 21 images, all data be evaluated and sent to the DB.

How could I go about doing this. The array and index part. (I'll get the the evaluation and sending to the DB later... )

I've tried sessions, but for some reason it doesn't work.

If some kind soul could point me in the right direction I would really apreciate it.

Cheers,
P.

Posted: Wed Aug 09, 2006 1:20 pm
by feyd
sessions are the most simple way of getting the data across the submissions without the user knowing much of anything. Post your code.

Posted: Wed Aug 09, 2006 2:06 pm
by PapiSolo
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Index.php:

Code: Select all

<?php session_start(); 
	
	$_SESSION['rand']= array("foto1.html", "foto2.html","foto3.html", "foto4.html", "foto5.html", "foto6.html", "foto7.html", "foto8.html", "foto9.html","foto10.html", "foto11.html", "foto12.html", "foto13.html", "foto14.html", "foto15.html", "foto16.html","foto17.html", "foto18.html", "foto19.html", "foto20.html", "foto21.html");
	$_SESSION['i'] = 0;
?>

<html>
<head>
<title>Foto 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>
	
<form method='POST' action='random.php'>	
	<?php
		$_a = $_SESSION['rand'];
		$_i = $_SESSION['i'];		
		shuffle($_a);
		include($_a[$_i]);
	?>
</form>

</body>
</html>
Random.php:

Code: Select all

<?php

	session_start();
	
	$_a = $_SESSION['rand'];
	$_i = $_SESSION['i'];
	
	if($_i<20){
		include($_a[$_i]);
		$_i++;
	}
	else{
		echo"You have reached the end!";
		echo $_i;
	}

?>
foto*.html:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Foto 7</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>
	<div id="center" height:100%>
	
		<img src="Img/foto_7.JPG" alt="" width="600" height="480">
				
		<div class="form">
		
			<form name="form1" method="post" action="foto7.php">
			
				<p>Por favor avalie a seguinte imagem numa escala de 1 a 9, sendo que 1 &eacute; muito desagrad&aacute;vel e 9 muito agrad&aacute;vel</p>
			  	1 <input name="f1.1" type="radio" value="1">
			  	2 <input name="f1.1" type="radio" value="2"> 
			  	3 <input name="f1.1" type="radio" value="3"> 
			  	4 <input name="f1.1" type="radio" value="4"> 
			  	5 <input name="f1.1" type="radio" value="5"> 
			  	6 <input name="f1.1" type="radio" value="6"> 
			  	7 <input name="f1.1" type="radio" value="7"> 
			  	8 <input name="f1.1" type="radio" value="8"> 
			  	9 <input name="f1.1" type="radio" value="9">
			  	<hr width="600px" color:red>
			  	<p>Por favor avalie a seguinte imagem numa escala de 1 a 9, sendo que 1 &eacute; muito desagrad&aacute;vel e 9 muito agrad&aacute;vel</p>
			  	1 <input name="f1.2" type="radio" value="1">
			  	2 <input name="f1.2" type="radio" value="2"> 
			  	3 <input name="f1.2" type="radio" value="3"> 
			  	4 <input name="f1.2" type="radio" value="4"> 
			  	5 <input name="f1.2" type="radio" value="5"> 
			  	6 <input name="f1.2" type="radio" value="6"> 
			  	7 <input name="f1.2" type="radio" value="7"> 
			  	8 <input name="f1.2" type="radio" value="8"> 
			  	9 <input name="f1.2" type="radio" value="9">
			  	<hr width="600px">
				
			  	<div align="right">
			  		<input type="submit" name="Submit" value="Submit" text-align:right>
			  	</div>
			</form>
			
		</div>
	</div>
</body>
</html>
Each foto*.php contains the same code as random.php.

I'm sure there is a better and easier way, but being new to all this I'm having a hard time figuring it out. Thanks for any help you can give.

Cheers,
P.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Aug 11, 2006 4:00 am
by PapiSolo
I managed to solve this using Sessions and session variables. This way I could initiate the array and array index, and pass it from one page to another. A simple if increments and checks wether I've reached the end of array and Violá!!! Got it working.