sdyates2001 Posted January 27, 2009 Share Posted January 27, 2009 Let me show you my script... <?php $QUESTIONS = array ( $_POST['ch1'], $_POST['ch2'], $_POST['ch3'], $_POST['ch4'], $_POST['ch5'], $_POST['ch6'], $_POST['ch7'], $_POST['ch8'], $_POST['ch9'], $_POST['ch10'],$_POST['ch11'], $_POST['ch12'], $_POST['ch13'], $_POST['ch14'], $_POST['ch15'],); print_r($QUESTIONS); # For the first 13 array elements, add 1 value to $PositiveFirstCriteria $count = ""; while ( $count < 13 ) { if ( $QUESTIONS['ch.$count'] == "Question" ) { $PositiveFirstCriteria += 1; echo "<p>Count: $count, $PositiveFirstCriteria"; } $count += 1; } # If $PositiveFirstCriteria > 6 (@ScreenPositive) # If yes to $PositiveSecondCriteria and moderate or serious to $PositiveThirdCriteria ?> The output from print_r is: Array ( [0] => Question [1] => Question [2] => [3] => [4] => [5] => [6] => [7] => Question 8 [8] => [9] => [10] => [11] => [12] => [13] => [14] => ) So, I know I am returning "Question" on the first two, but the if statement is never true . I even tried to echo each element in the array such as $Question[1], but it appears once again I am mixing up perl. Quote Link to comment Share on other sites More sharing options...
Philip Posted January 27, 2009 Share Posted January 27, 2009 Change if ( $QUESTIONS['ch.$count'] == "Question" ) { to if ( $QUESTIONS['ch'.$count] == "Question" ) { Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted January 27, 2009 Share Posted January 27, 2009 I don't see why you changed it after we showed you the correct way :s Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 27, 2009 Share Posted January 27, 2009 <?php //Make a loop to get the values $i = 1; while (isset($_POST['ch'.$i])) { $QUESTIONS[] = trim($_POST['ch'.$i]); $i++; } print_r($QUESTIONS); # For the first 13 array elements, add 1 value to $PositiveFirstCriteria $PositiveFirstCriteria = 0; for ($count=0; $count<13; $count++) { if ( $QUESTIONS['ch'.$count'] == "Question" ) { $PositiveFirstCriteria++; echo "<p>Count: $count, $PositiveFirstCriteria</p>"; } } # If $PositiveFirstCriteria > 6 (@ScreenPositive) # If yes to $PositiveSecondCriteria and moderate or serious to $PositiveThirdCriteria ?> Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted January 27, 2009 Share Posted January 27, 2009 To save time, and coding you could just give the inputs a name like name="questions[]" Which will automatically put it in an array Quote Link to comment Share on other sites More sharing options...
sdyates2001 Posted January 27, 2009 Author Share Posted January 27, 2009 I am beginning to wonder if netfirms is using some kind of modified strange version of PHP. I even copied the code word for word and it generated a Parse error: syntax error, unexpected '{' in ... on line 5. Are there different versions of commands I need to be aware of? I honestly can't believe I am having trouble with basic code >>thanks for the comment on name= "questions[]" - I'll try rewrite later once I get logic to work Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted January 27, 2009 Share Posted January 27, 2009 Can you show us the code which generated the error, Quote Link to comment Share on other sites More sharing options...
sdyates2001 Posted January 27, 2009 Author Share Posted January 27, 2009 The code that generated the error is immediately below which was recommended to me in a few previous posts. For some reason, it does not like the while command. <?php //Make a loop to get the values $i = 1; while (isset($_POST['ch'.$i])) { $QUESTIONS[] = trim($_POST['ch'.$i]); $i++; } print_r($QUESTIONS); # For the first 13 array elements, add 1 value to $PositiveFirstCriteria $PositiveFirstCriteria = 0; for ($count=0; $count<13; $count++) { if ( $QUESTIONS['ch'.$count] == "Question" ) { $PositiveFirstCriteria++; echo "<p>Count: $count, $PositiveFirstCriteria</p>"; } } # If $PositiveFirstCriteria > 6 (@ScreenPositive) # If yes to $PositiveSecondCriteria and moderate or serious to $PositiveThirdCriteria ?> Here is a snippet of the code that sends the info to this page: FORM NAME ="form1" METHOD ="POST" ACTION ="bob.php"> <Input type = 'Checkbox' Name ='ch1' value ="Question" <?PHP print $ch1; ?> >Has there ever been a period of time when you were not your usual self and... <P> <Input type = 'Checkbox' Name ='ch2' value="Question" <?PHP print $ch2; ?> >...you felt so good or so hyper tat other people thought you were not your normal self or you were so hyper that you got into trouble? <P> <Input type = 'Checkbox' Name ='ch3' value="Question" <?PHP print $ch3; ?> >...you were so irritable that you shouted at people or started fights or arguments? Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted January 27, 2009 Share Posted January 27, 2009 Hmm weird, it all looks fine Quote Link to comment Share on other sites More sharing options...
sdyates2001 Posted January 27, 2009 Author Share Posted January 27, 2009 Hence my head scratching... I wonder if it is Netfirms. Does anyone have an account with netfirms that has basic code doing what I am trying to do? Perhaps they have an older version of PHP - I would expect that they are using ver 5. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted January 27, 2009 Share Posted January 27, 2009 do echo php_version(); (i think) Quote Link to comment Share on other sites More sharing options...
sdyates2001 Posted January 27, 2009 Author Share Posted January 27, 2009 You'll love this, it says: Fatal error: Call to undefined function php_version() Quote Link to comment Share on other sites More sharing options...
Philip Posted January 27, 2009 Share Posted January 27, 2009 run <?php phpinfo(); ?> P.S. - it's phpversion(); Also, I am able to run the while loop on a 4.4.8 version. I think there is something else involved. Quote Link to comment Share on other sites More sharing options...
sdyates2001 Posted January 27, 2009 Author Share Posted January 27, 2009 The attached version info is in a jpg. Also attached is the first file - it is too big to just paste in here and I am not very happy with the efficiency. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Philip Posted January 28, 2009 Share Posted January 28, 2009 Try this for your PHP: <?php if(isset($_POST['Submit1'])) { for($i=1; $i<=15; $i++) { $ch = 'ch'.$i; if(isset($_POST['ch'.$i]) && $_POST['ch'.$i]) $$ch = 'checked'; else $$ch = 'unchecked'; echo $$ch.'<br />'; } } ?> Quote Link to comment Share on other sites More sharing options...
sdyates2001 Posted January 28, 2009 Author Share Posted January 28, 2009 I get the same error as before "Parse error: syntax error, unexpected " Line two starts with if(isset... Quote Link to comment Share on other sites More sharing options...
sdyates2001 Posted January 28, 2009 Author Share Posted January 28, 2009 I get the same error message "Parse error: syntax error, unexpected " - from the line with the if statement Quote Link to comment Share on other sites More sharing options...
Philip Posted January 28, 2009 Share Posted January 28, 2009 Okay, I added in some comments. This is tested code... so, <html> <head> <title>Checkboxes</title> </head> <body> <?php // if it is submitted if(isset($_POST['Submit1'])) { // for loop, through each of the checkboxes for($i=1; $i<=15; $i++) { // get the variable name ready (ch#) $ch = 'ch'.$i; // if the checkbox is checked if(isset($_POST['ch'.$i]) && $_POST['ch'.$i]) $$ch = 'checked'; else $$ch = 'unchecked'; // tell us what it is! echo $$ch.'<br />'; } } ?> <FORM NAME ="form1" METHOD ="POST" ACTION =""> <Input type = 'Checkbox' Name ='ch1' value ="Question" <?PHP print $ch1; ?> >Has there ever been a period of time when you were not your usual self and... <P> <Input type = 'Checkbox' Name ='ch2' value="Question" <?PHP print $ch2; ?> >...you felt so good or so hyper tat other people thought you were not your normal self or you were so hyper that you got into trouble? <P> <Input type = 'Checkbox' Name ='ch3' value="Question" <?PHP print $ch3; ?> >...you were so irritable that you shouted at people or started fights or arguments? <P> <Input type = 'Checkbox' Name ='ch4' value="Question 4" <?PHP print $ch4; ?> >...you got much less sleep than usual and found you didnÕt really miss it? <P> <Input type = 'Checkbox' Name ='ch5' value="Question 5" <?PHP print $ch5; ?> >...you were much more talkative or spoke faster than usual? <P> <P> <Input type = 'Checkbox' Name ='ch6' value="Question 6" <?PHP print $ch6; ?> >...thoughts raced through your head or you couldnÕt slow your mind down? <P> <P> <Input type = 'Checkbox' Name ='ch7' value="Question 7" <?PHP print $ch7; ?> >...you were so easily distracted by things around you that you had trouble concentrating or staying on track? <P> <P> <Input type = 'Checkbox' Name ='ch8' value="Question 8" <?PHP print $ch8; ?> >...you had much more energy than usual? <P> <P> <Input type = 'Checkbox' Name ='ch9' value="Question 9" <?PHP print $ch9; ?> >...you were much more active or did many more things than usual? <P> <P> <Input type = 'Checkbox' Name ='ch10' value="Question 10" <?PHP print $ch10; ?> >...you were much more active or did many more things than usual? <P>...you were much more social or outgoing than usual, for example, you telephoned friends in the middle of the night? <P> <Input type = 'Checkbox' Name ='ch11' value="Question 11" <?PHP print $ch11; ?> >...you were much more interested in sex than usual? <P> <P> <Input type = 'Checkbox' Name ='ch12' value="Question 12" <?PHP print $ch12; ?> >...you did things that were unusual for you or that other people might have thought were excessive, foolish, or risky? <P> <P> <Input type = 'Checkbox' Name ='ch13' value="Question 13" <?PHP print $ch13; ?> >...spending money got you or your family into trouble? <P> <P> <Input type = 'Checkbox' Name ='ch14' value="Question 14" <?PHP print $ch14; ?> >If you checked more than one checkbox above, have several of these ever happened during the same period of time? <P> <P> <Input type = 'Checkbox' Name ='ch15' value="Question 15" <?PHP print $ch15; ?> >If you checked more than one checkbox above, have several of these ever happened during the same period of time? <P> <INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Press to Submit"> </FORM> </body> </html> Quote Link to comment Share on other sites More sharing options...
.josh Posted January 28, 2009 Share Posted January 28, 2009 going back to your OP, your condition isn't working because $QUESTIONS is a numeric array. You assign various posted values to it. Notice on your print_r how the keys are 0,1,2, etc...so when you do this: if ( $QUESTIONS['ch.$count'] == "Question" ) { You are checking for positions in your questions array using keys $QUESTIONS['ch1'], $QUESTIONS['ch2'] etc.. which are associative keys. You can mix keys in an array, but more to the point, those keys don't exist in your array. Remove the 'ch' from the mix. Quote Link to comment Share on other sites More sharing options...
sdyates2001 Posted January 28, 2009 Author Share Posted January 28, 2009 Folks, it was the file encoding... despite setting UTF-8, it was encoding in another format! Everything works well now! Quote Link to comment 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.