trevzilla Posted November 5, 2014 Share Posted November 5, 2014 (edited) 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 November 5, 2014 by trevzilla Quote Link to comment https://forums.phpfreaks.com/topic/292298-why-is-array-being-echoed-out/ Share on other sites More sharing options...
ginerjm Posted November 5, 2014 Share Posted November 5, 2014 Remove the $I. You're not setting so why use it in a loop? Quote Link to comment https://forums.phpfreaks.com/topic/292298-why-is-array-being-echoed-out/#findComment-1495832 Share on other sites More sharing options...
trevzilla Posted November 5, 2014 Author Share Posted November 5, 2014 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; } } } Quote Link to comment https://forums.phpfreaks.com/topic/292298-why-is-array-being-echoed-out/#findComment-1495848 Share on other sites More sharing options...
ginerjm Posted November 5, 2014 Share Posted November 5, 2014 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; Quote Link to comment https://forums.phpfreaks.com/topic/292298-why-is-array-being-echoed-out/#findComment-1495850 Share on other sites More sharing options...
Psycho Posted November 5, 2014 Share Posted November 5, 2014 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); Quote Link to comment https://forums.phpfreaks.com/topic/292298-why-is-array-being-echoed-out/#findComment-1495851 Share on other sites More sharing options...
trevzilla Posted November 5, 2014 Author Share Posted November 5, 2014 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? SF6NitrogenHydrogenH2S (The correct answer is Nitrogen and Hydrogen) Quote Link to comment https://forums.phpfreaks.com/topic/292298-why-is-array-being-echoed-out/#findComment-1495855 Share on other sites More sharing options...
trevzilla Posted November 5, 2014 Author Share Posted November 5, 2014 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? SF6NitrogenHydrogenH2S 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... Quote Link to comment https://forums.phpfreaks.com/topic/292298-why-is-array-being-echoed-out/#findComment-1495858 Share on other sites More sharing options...
Psycho Posted November 5, 2014 Share Posted November 5, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292298-why-is-array-being-echoed-out/#findComment-1495872 Share on other sites More sharing options...
trevzilla Posted November 5, 2014 Author Share Posted November 5, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292298-why-is-array-being-echoed-out/#findComment-1495873 Share on other sites More sharing options...
Psycho Posted November 6, 2014 Share Posted November 6, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/292298-why-is-array-being-echoed-out/#findComment-1495894 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.