Page 1 of 1

Problem with redirecting to page in array

Posted: Sun Aug 20, 2006 3:26 pm
by PapiSolo
I need to cycle through pages, pose some questions, evaluate the answers and iterate to the next page in the array.

The problem is how can I redirect to the next page in my array. I tried inlcude(); all seems OK but then I end up in an endless loop.
With header(); I get an error since I have to point to a full URL.

Here is an exert of my code: (bear in mind that this is real newbie stuff :D)

Code: Select all

<!-- info.html >

<form action='valid_info.php' method='post'>
					
<!-- Form stuff goes here. >
			
</form> 

Code: Select all

<?php
//valid_info.php
	
	//Variables from info.html
	$_nome1=trim($_POST['nome1']);
	$_nome2=trim($_POST['nome2']);
	$_idade = trim($_POST['idade']);
	$_morada = trim($_POST['morada']);
	$_cod_post1 = trim($_POST['cod_post1']);
	$_cod_post2 = trim($_POST['cod_post2']);
	$_local = trim($_POST['local']);
	$_email = trim($_POST['email']);
	
	if (!empty($_nome1) && !empty($_nome2) && !empty($_idade) && !empty($_morada) && !empty($_cod_post1) && !empty($_cod_post2) && !empty($_local) && !empty($_email))
	{
		
		//send info to DB (will do that later)
		header('Location: array.php');
		exit;
	}
	else
	{
		//jump to error page
		header('Location: error1.html');
		exit;
	}
	

?>

Code: Select all

<?php
	//array.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;
	
	$_a = $_SESSION['rand'];
	shuffle($_a);
	
	//Problem start HERE:
	
	header('Location: $_a[0]');
	exit;
	
?>
I have another file which validates the info retrieved from foto*.html and if all is ok, jumps to the next page, much the same way I have above with

Code: Select all

header('Location: $_a[$_SESSION['i']]);
Obviously this isn't working, so any help is most welcome.

Thanks all, cheers
P.

Posted: Sun Aug 20, 2006 3:35 pm
by feyd
The problems toward the end of your post can be solved with greater understanding of how strings work in PHP. Have a read here for the details. If you have questions on that, ask away, we're not shy. :)

I don't have time to read through and understand the logic you're using elsewhere, the string issue just caught my eye. Sorry.

Posted: Sun Aug 20, 2006 4:23 pm
by PapiSolo
PLease forgive my ignorance feyd, but your link pointed to a page with the following message
Sorry, but the function langauge.types.string is not in the online manual....
and various possible options. I don't know which would solve my problem. However, from what I understood from your post... my problem is in the part where I point the location to an element in the array. Should I initialize a variable with that info in the array?

Once again, please forgive my lack of knowledge, but I'm still very new to programming in general and php in perticular.

Cheers and thanks for everything,
P.

Posted: Sun Aug 20, 2006 4:34 pm
by daedalus__
http://us2.php.net/manual/en/function.header.php will show you how to get an absolute path for use in the header() function.

Posted: Sun Aug 20, 2006 4:38 pm
by PapiSolo
Thanks... I will look into that!

Posted: Sun Aug 20, 2006 5:06 pm
by feyd

Posted: Sun Aug 20, 2006 5:39 pm
by griffinmt

Code: Select all

header('Location: $_a[$_SESSION['i']]);
First, you are missing (and mixing) your quotes incorrectly. Tyr using:

Code: Select all

header("Location: $_a[$_SESSION['i']]");
Better yet, wrap the array reference in curly braces:

Code: Select all

header("Location: {$_a[$_SESSION['i']]}");

Posted: Sun Aug 20, 2006 5:54 pm
by RobertGonzalez
Is that session var a full URI?

Posted: Mon Aug 21, 2006 2:42 am
by PapiSolo
No it's not. The sesion var $_SESSION['i'] is just a pointer to the current element of the array created above and points to foto1.html... foto2.html... I'm looking into creating a full URL from the info Daedalus pointed me to. I will also try griffinmt's sugestion. It could also be a problem with the quotes...

As for your pointer fetd... I will definetly give that a good read... specially for future projects.