Jump to content

Why is "Array" Being Echoed out


trevzilla

Recommended Posts

So I'm kind of new to PHP, and have been having lots of success, but I have no idea why this is occuring. It's probably a super easy answer...

 

I have a form that has checkboxes. Essentially I am echoing out the results of which ever checkboxes the user checks. Here's my code...

if(!empty($_POST[$i])){
        foreach($_POST[$i] as $check){
	$answer .= $check;
}
echo $answer;

 And the result that is echoed out says this..

 

ArraySF6Nitrogen

 

(SF6 is one of the checkboxes that was selected, and Nitrogen is the other checkbox that was selected)

 

I would be incredibly happy if the echoed out result just said SF6Nitrogen

 

My question is why does it say "Array" first?

Edited by trevzilla
Link to comment
Share on other sites

Actually it is in a loop...Sorry, I thought this bit of code was unnessary to include, but here is the entire loop, etc.

for($i=1; $i<=$totalquest; $i++){
		
if(!empty($_POST[$i])){
	foreach($_POST[$i] as $check){
	$answer .= $check;
	}
	echo $answer;
	if($check!=$correct[$i]){
	$WrongAnswers .= "<b>You answered Question $i incorrectly:</b><br>$question[$i]<br>You answered: $answer<br>The correct answer is: $correct[$i]<br>$explanation[$i]<p>";

	$score=$score-1;

	}
			
}
		
		

	}
Link to comment
Share on other sites

You are looping thru the entire POST array, not just the checkboxes array.  Assuming that your html name field for the checkboxes is "cbox[]" this is all the code you need to list the checkboxes that are checked:

 

$ans = '';
foreach ($_POST['cbox'] as $cb_value)
   $ans  .= $cb_value .'<br>'; 
 
echo $ans;
Link to comment
Share on other sites

It says array first because of two possible reasons that I can think of:

 

1. There is an echo/print somewhere before this code that is outputting that content OR

 

2. The first element of $_POST[$i] is a sub-array.

 

You should almost never have logic that runs through an array and *assumes* the array contains specific vales and in a specific order. You should explicitly echo the indexes you want from that array. But, first thing is first. Let's check exactly what is contained in that array. Use this:

 

 

var_dump($_POST);
Link to comment
Share on other sites

So in response to ginerjm, Yes, I'm looping through the entire POST array because I actually have several questions with checkboxes....

 

In not exactly HTML terms, it looks like this.

 

Question 1: "Blah blah blah"

Answer 1: Has input type=checkbox, name=1 value=answer[1][1]

Answer 2: Has input type=checkbox, name=1 value=answer[1][2]

Answer 3: Has input type=checkbox, name=1 value=answer[1][3]

 

Question 2: "Blah blah blah"

Answer 1: Has input type=checkbox, name=2 value=answer[2][1]

Answer 2: Has input type=checkbox, name=2 value=answer[2][2]

Answer 3: Has input type=checkbox, name=2 value=answer[2][3]

 

This way I can loop through the questions, and within that loop I can loop through the answers.

 

In response to Psyco, here is what was output when I did the var_dump.

 

array(1) { [1]=> array(2) { [0]=> string(3) "SF6" [1]=> string(3) "H2S" } }

 

(I had SF6 checked, and H2S checked, which was the 1st, and 4th checkbox in that certain grouping of "answers")

 

The reason I have it run through and "assume" a specific value in a specific order is because this is a testing scenario. I essentially have the correct answer in the correct order, and if the user clicks the correct checkboxes, it creates a string that compares with my "correct answer" string. If the two strings match, the user gets points. If the two strings don't match, the user loses points.

 

I'm all ears if there is a better way to do a multiple choice test where the user is allowed to select multiple answers from one group. Here's the actual question that we are dealing with right now, so you're aware...

 

1. Select the two (2) main sources of non-introduced atmospheric hazards in a wind turbine hub?

SF6
Nitrogen
Hydrogen
H2S

 

(The correct answer is Nitrogen and Hydrogen)

Link to comment
Share on other sites

I guess this problem might be going a different direction anyway...so this might not even matter. But I could still potentially use some help...I'm having a tricky time thinking through this one. Say we have the same question:

 

1. Select the two (2) main sources of non-introduced atmospheric hazards in a wind turbine hub?

SF6
Nitrogen
Hydrogen
H2S

 

I have a seperate page that simply lists the questions, correct answer etc, and it's included in my page that generates the test. The code question looks like this:

//question 1
$question[$i] 	= 'Select the two (2) <i>main</i> sources of <i>non-introduced</i> atmospheric hazards in a wind turbine hub?';
$answer[$i][1] 	= 'SF6';
$answer[$i][2] 	= 'Nitrogen';
$answer[$i][3] 	= 'Hydrogen';
$answer[$i][4] 	= 'H2S';
$correct[$i][1]	= $answer[$i][2]; //Only edit the number to reflect which selection is the correct answer
$correct[$i][2]	= $answer[$i][3];
$type[$i]		= 'checkbox';
$explanation[$i]	= 'All turbine hubs have either a Nitrogen Accumulator or Battery back-up system which must be considered a hazard until proven otherwise by atmospheric testing and visual inspection. ';

$i++; //this increments the counter by 1. Do not edit this line, yet make sure this is after each question "block."

Now, before, I thought I was making this so that either the user got it all right, or all wrong. Nothing in between. So I figured (and I've changed the code a little since then) that I could just have $correct[$i] = $answer[$i][2].$answer[$i][3] which creates a string called "NitrogenHydrogen"

 

If the user selected any boxes other that Nitrogen and Hydrogen, their string would be different when it compares it to $correct[$i].  (This method has worked tried and true for me with radio buttons. I'm running into the issues with checkboxes)

 

Lately I just found out that this question will actually be worth 4 pts. Each correct state of a checkbox is worth 1 pt. So If they DON'T check SF6, they get a point, another 2 pts are awarded for checking both Nitrogen and Hydrogen, and the last point awarded for NOT checking H2S. That means I have to check every single state of each checkbox and compare it to my $correct[$i] answers.

 

If there are any ideas how to do this I'm all ears. Otherwise I think I just have to think through it and see if can figure this out. I THINK I'm on the right track...

Link to comment
Share on other sites

I think that's a bad idea. You can make it complicated where the user has to choose multiple answers, but I think a better option is to create your answers in this format:

 

What are the two (2) main sources of non-introduced atmospheric hazards in a wind turbine hub?
1. Nitrogen & Hydrogen
2. Nitrogen & Helium
3. Hydrogen & H2S
4. Helium & Hydrogen
 
That way each question has one and only one answer. If you make it so there are multiple options to select for a question, the database configuration and the logic will be more complicated.
Link to comment
Share on other sites

I completely hear you. However this method has come down the pipeline, and alas, I'm just the lowly programmer. ;) The bosses are asking for the multiple checkbox type answer. Maybe I was silly in telling them that I could do it...but hey, I kind of enjoy the logic aspect.

 

If I absolutely can't figure it out, then yes, your solution was going to be my fall back. And I do already have that code working. Essentially if I have radio buttons instead of checkboxes, the problem is pretty straight forward.  But until I run out of time, I'm going to do my best to do the checkbox method to appease the boss so he doesn't have to rewrite his test. :)

Link to comment
Share on other sites

Well, it's not impossible. I don't know how your DB is currently constructed (and don't really want to). But, here is how I would tackle this problem.

 

I would have one table for the questions which includes at least a questionID and the question text.

I would then have an answers table. Each answer would have at least four fields: answerID, questionID (FK), question text and a Boolean field for whether the answer was correct or not.

 

Then when it comes time to display a question, I would select all the available answers. If only ONE of the answers is a "correct" answer, then I would display the answers with a radio group. However, if there are more than one rights answer then I would use checkboxes. Additionally, I would get the count of right answers and implement JavaScript on the checkbox group to limit the max selected to that number. On second thought, you coulod just always use checkboxes and use JS to limit the selection to one as well when needed.

 

As for what you do when processing the results, it all depends on whether you are giving partial credit for getting some of the multiple answer questions right or if they only get credit if they get them all right.

 

 

Also, I think your current method for the answer values is flawed

 

Question 1: "Blah blah blah"
Answer 1: Has input type=checkbox, name=1 value=answer[1][1]
Answer 2: Has input type=checkbox, name=1 value=answer[1][2]
Answer 3: Has input type=checkbox, name=1 value=answer[1][3]
 
Question 2: "Blah blah blah"
Answer 1: Has input type=checkbox, name=2 value=answer[2][1]
Answer 2: Has input type=checkbox, name=2 value=answer[2][2]
Answer 3: Has input type=checkbox, name=2 value=answer[2][3]

 

The NAME of the answer fields should identify the Question ID (and it should be an array so all answers results are in a single array. And the values should be the answer ID, not an arbitrary 1, 2, 3. Lastly, for those questions that will have multiple answers, the answer field name would be a sub array.

 

 

Question 1: "Blah blah blah"
Answer 1: Has input type=checkbox, name=question[1][] value=2
Answer 2: Has input type=checkbox, name=question[1][] value=4
Answer 3: Has input type=checkbox, name=question[1][] value=9
 
Question 2: "Blah blah blah"
Answer 1: Has input type=checkbox, name=question[2][] value=13
Answer 2: Has input type=checkbox, name=question[2][] value=6
Answer 3: Has input type=checkbox, name=question[2][] value=7
 
 
 

Question 3: "Pick two answers"
Answer 1: Has input type=checkbox, name=question[3][] value=22
Answer 2: Has input type=checkbox, name=question[3][] value=16

Answer 3: Has input type=checkbox, name=question[3][] value=5
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.