Jump to content

[SOLVED] Ok, head is beginning to hurt...


sdyates2001

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/142705-solved-ok-head-is-beginning-to-hurt/
Share on other sites

<?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 

?>

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 ;)

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?

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>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.