Problem with redirecting to page in array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PapiSolo
Forum Newbie
Posts: 24
Joined: Wed Aug 09, 2006 12:16 pm

Problem with redirecting to page in array

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
PapiSolo
Forum Newbie
Posts: 24
Joined: Wed Aug 09, 2006 12:16 pm

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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.
PapiSolo
Forum Newbie
Posts: 24
Joined: Wed Aug 09, 2006 12:16 pm

Post by PapiSolo »

Thanks... I will look into that!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

griffinmt
Forum Commoner
Posts: 35
Joined: Sun Jul 16, 2006 8:37 pm
Location: Michigan

Post 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']]}");
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Is that session var a full URI?
PapiSolo
Forum Newbie
Posts: 24
Joined: Wed Aug 09, 2006 12:16 pm

Post 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.
Post Reply