xn1 Posted March 22, 2011 Share Posted March 22, 2011 As you can see here I have the following code <?php session_start(); session_register('hazard2'); //INITIALIZE ERROR VARIABLES $errorFound = false; $errorIcon['hazard2'] = ''; if (isset($_POST['submitbutton'])) { //SET SESSION VARIABLES $_SESSION['hazard2'] = $_POST['hazard2']; //TEST FORM INFORMATION if(count($_POST['hazard2']) != 3) { $errorFound = true; $errorIcon['hazard2'] = 'Error Question 2'; } //IF NO ERRORS WERE FOUND, CONTINUE PROCESSING if (!$errorFound) { header( "Location: hazardresult.php" ); } } ?> It's lazy I know, but the script is on a file called hazard2.php, is there anyway to have all of the hazard2's in the code come from the file name. So when I make hazard3, I don't have to change all the hazard2's to hazard3's Thank you Quote Link to comment https://forums.phpfreaks.com/topic/231432-define-variable-names-by-file-name/ Share on other sites More sharing options...
kenrbnsn Posted March 22, 2011 Share Posted March 22, 2011 If you're duplicating all the code in similarly named files, you're doing something wrong. You should be able to make one php script to handle the work. Why do you think you have to make many scripts? Also, you shouldn't be using the session_register() function. Ken Quote Link to comment https://forums.phpfreaks.com/topic/231432-define-variable-names-by-file-name/#findComment-1191039 Share on other sites More sharing options...
RussellReal Posted March 22, 2011 Share Posted March 22, 2011 As you can see here I have the following code <?php session_start(); session_register('hazard2'); //INITIALIZE ERROR VARIABLES $errorFound = false; $errorIcon['hazard2'] = ''; if (isset($_POST['submitbutton'])) { //SET SESSION VARIABLES $_SESSION['hazard2'] = $_POST['hazard2']; //TEST FORM INFORMATION if(count($_POST['hazard2']) != 3) { $errorFound = true; $errorIcon['hazard2'] = 'Error Question 2'; } //IF NO ERRORS WERE FOUND, CONTINUE PROCESSING if (!$errorFound) { header( "Location: hazardresult.php" ); } } ?> It's lazy I know, but the script is on a file called hazard2.php, is there anyway to have all of the hazard2's in the code come from the file name. So when I make hazard3, I don't have to change all the hazard2's to hazard3's Thank you as Ken stated, but you could do it like this! <?php session_start(); $file = $_GET['file']; session_register($file); //INITIALIZE ERROR VARIABLES $errorFound = false; $errorIcon[$file] = ''; if (isset($_POST['submitbutton'])) { //SET SESSION VARIABLES $_SESSION[$file] = $_POST[$file]; //TEST FORM INFORMATION if(count($_POST[$file]) != 3) { $errorFound = true; $errorIcon[$file] = 'Error Question 2'; } //IF NO ERRORS WERE FOUND, CONTINUE PROCESSING if (!$errorFound) { header( "Location: hazardresult.php" ); } } ?> and then instead of http://xyz.com/hazard2.php do: http://xyz.com/hazard.php?file=hazard2 Quote Link to comment https://forums.phpfreaks.com/topic/231432-define-variable-names-by-file-name/#findComment-1191043 Share on other sites More sharing options...
xn1 Posted March 22, 2011 Author Share Posted March 22, 2011 First question, what am I doing wrong? The aim of the functions is to, test the answer to the question on the page, to make sure an option is selected, if selected move on to the next while storing the answer into a session. What's the easier way? Second question, can you link me a guide to the ?file=hazard2 way of doing things. And thank you for the file name help. Quote Link to comment https://forums.phpfreaks.com/topic/231432-define-variable-names-by-file-name/#findComment-1191053 Share on other sites More sharing options...
RussellReal Posted March 22, 2011 Share Posted March 22, 2011 I gave you code that makes it work, it SHOULD work, but its untested as far as the sessions, yes its a good idea to store the answers to the questions in a session, makes it alot easier obviously, HOWEVER, you don't need a new session PER answer, store it all in an array, in the user's default session Quote Link to comment https://forums.phpfreaks.com/topic/231432-define-variable-names-by-file-name/#findComment-1191060 Share on other sites More sharing options...
xn1 Posted March 22, 2011 Author Share Posted March 22, 2011 Had to change it slightly to remove the .php but it did work perfectly thank you. So I'd only need session start in the first page? And would I only need to define the array on the first page too? On the results page I plan on explaining all of the answers that were wrong by checking them all. Would I be able to do this with the array? Quote Link to comment https://forums.phpfreaks.com/topic/231432-define-variable-names-by-file-name/#findComment-1191069 Share on other sites More sharing options...
RussellReal Posted March 23, 2011 Share Posted March 23, 2011 yes, on every page that you are wanting to ACCESS/MODIFY the session variable you will call session_start, it will then load whatever session that the user's session id represents! You only need to define something once in a session variable, after that it will exist in any further interactions with that particular session, unless ofcourse you unset that particular variable so for example.. session_start(); $file = $_GET['file']; if (!isset($_SESSION['answers'])) $_SESSION['answers'] = array(); $_SESSION['answers'][$file] = $_POST['answer']; Quote Link to comment https://forums.phpfreaks.com/topic/231432-define-variable-names-by-file-name/#findComment-1191159 Share on other sites More sharing options...
xn1 Posted March 23, 2011 Author Share Posted March 23, 2011 Ok, what I've done then is create a starting page where I've set a session array called answers and a session variable to count the number of questions aswered (this will be so that eventually I can keep loading random questions until i hit a target then go to a results page) called qcount. Am I doing it right now? This is after you click on start test in the start page. (This is question1.php) <?php session_start(); $errorFound = false; $errorIcon['question1'] = ''; if(isset($_SESSION['qcounter'])){ if(isset($_POST['submitbutton'])){ if($_POST['question1'] = ''){ $errorFound = true; $errorIcon['question1'] = 'Please Select An Answer'; } if(!$errorFound){ $_SESSION['answers'][] = $_POST['question1']; $_SESSION['qcounter'] = $_SESSION['qcounter']+1; header ("Location: question2.php"); } } } else { header ("Location: teststart.php");} ?> Quote Link to comment https://forums.phpfreaks.com/topic/231432-define-variable-names-by-file-name/#findComment-1191216 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.