Qu-ko Posted January 14, 2007 Share Posted January 14, 2007 Hi, yo, total noob here. *waves*Okay, so, I'm trying to program a Mary Sue litmus test in which you utilize checkmark boxes (so there's multiple answers, each with its own number of points), then there's supposed to be a script in which all the points (in the "value="x"" attribute) are added together and then displayed on a results page (which is supposed to have an answer key to go with it; it's not like Quizilla in that there are not multiple results to be displayed, only the total number of points).(I most likely just completely confused you with that explanation as well. Ask for further details if you need to.)Problem is, I [i]fail[/i] at PHP.I asked my friend who codes as a profession about it; she's only good at editing existing scripts, though. She tried to edit a PHP script and then tried to make a Javascript script for it, but failed with both.I'm frustrated about this because I know it's SO SIMPLE. But neither my friend nor I have had any luck so far in figuring anything out.But since I don't know a single thing about PHP, I've been trying to edit existing scripts. One is far too complicated and not coded in a way that it would work for me; the other is a tutorial that is extremely vague and does not go into detail or give you a full code snippet.Don't laugh at how utterly [i]awful[/i] this code is, but so far I have:[code]<?include("header.php");$score=0;if ($q=="-5") $score=$score-5;if ($q=="-4") $score=$score-4;if ($q=="1") $score=$score+1;//[etc., etc., goes on until I've covered all the numbers used in the "value" tags, positive and negative. Hell, I'm not even sure if you can do "$score-1" in PHP, I'm that uneducated about it.//echo "<h1>Results</h1><p align=\"center\">Your Mary Sue score is <b>".$score."</b>.</p><p align=\"center\"><b>Score Key:</b></p><p align=\"center\"><b>0-100:</b> Score. Text blah blah blah.</p>";include("footer.php");?>[/code]Each input checkbox looks like this:[code]<input type="checkbox" value="3" name="q">[/code]or[code]<input type="checkbox" value="-3" name="q">[/code]So basically, I have no idea what I'm doing, someone save me please? ;-;(If this is in the wrong forum... er, sorry... as I said, total noob. >_>;) Link to comment https://forums.phpfreaks.com/topic/34079-quiz-script-thing/ Share on other sites More sharing options...
DarkendSoul Posted January 14, 2007 Share Posted January 14, 2007 Okay, setting up the form, each set of check boxes should have a separate name. As such.[code]<form action="whatever.php" method="post"><input type="hidden" name="total" value="100010"><div style="display:none"><textarea name="scoreKey"><p align="center"><b>100010:</b> Will you marry me?</p><p align="center"><b>-1000009 to 100009:</b> Gah.</p><p align="center"><b>-1000010:</b> I HATE YOU GO AWAY!</p></textarea></div>Do you like pie:<input type="radio" name="1" value="10"> Yes<input type="radio" name="1" value="-1000000"> NoAm I hot:<input type="radio" name="2" value="100000"> Yes<input type="radio" name="2" value="-10"> No<input type="submit" name="submit" value="I'm Done"></form>[/code]Then your php would be as such.whatever.php[code]<?phpinclude("header.php");$score=0;$total=$_POST['total'];$i=1;while ($_POST[$i]) {$score=$score+$_POST[$i];$i++}echo "<h1>Results</h1><p align=\"center\">Your Mary Sue score is <b>$score</b>.</p><p align=\"center\"><b>Score Key:</b></p>".$_POST['scoreKey'];include("footer.php");?>[/code] Link to comment https://forums.phpfreaks.com/topic/34079-quiz-script-thing/#findComment-160228 Share on other sites More sharing options...
Qu-ko Posted January 14, 2007 Author Share Posted January 14, 2007 I don't know why, but that didn't work... It would either say "Your Mary Sue score is 0" every time I checked boxes in different sections or just nothing at all. I ran it by my father, who's more proficient at PHP than I am, and he wasn't really sure what the problem was either. (Then again, he only took a quick glance at it, but... :|)He told me to try combining the "foreach ($_POST['question'] as $value)" statement and the "array_sum" statement, so I'm trying to figure out how to go about doing that... Link to comment https://forums.phpfreaks.com/topic/34079-quiz-script-thing/#findComment-160264 Share on other sites More sharing options...
corbin Posted January 14, 2007 Share Posted January 14, 2007 Modified the code a bit and combined the pages... Tested this, so it should work...:[code=php:0]<?php$scale[] = "-20 - 0 You're not nice.";$scale[] = "0 - 10 I hope you like pie.";$scale[] = "10-20 Mmmm pie and hotness.";$scale1 = implode("<br />\r\n", $scale);if(!$_POST) {?><form action="" method="post">Do you like pie:<input type="radio" name="1" value="10"> Yes<input type="radio" name="1" value="-10"> NoAm I hot:<input type="radio" name="2" value="10"> Yes<input type="radio" name="2" value="-10"> No<input type="submit" name="submit" value="I'm Done"></form><?php}else {$score = 0;$i = 1;while($_POST[$i]) {$score += $_POST[$i];$i++;}echo <<<EOPYour score was <b>$score</b>!<br />Scale:<br />$scale1EOP;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/34079-quiz-script-thing/#findComment-160279 Share on other sites More sharing options...
Qu-ko Posted January 14, 2007 Author Share Posted January 14, 2007 ...Even when I delete the headers and footers from the mix and triple check to make sure everything has been changed to suit my quiz and nothing is wrong script-wise, it STILL refuses to add the numbers. >_<It might be a problem with the actual quiz rather than the processing... I'm not sure what it might be, but I'm formatting my quiz a little like this:[code]<form action="" method="post"><h1>Pie</h1><p><input type="checkbox" value="5" name="question1">Do you like it?<br> <input type="checkbox" value="10" name="question1">Love it?<br> <input type="checkbox" value="15" name="question1">Worship it?<br></p><h1>More Pie</h1><p><input type="checkbox" value="5" name="question2">Do you like apple pie? <input type="checkbox" value="6" name="question2">How about chocolate pie? <input type="checkbox" value="7" name="question2">Lemon pie? <input type="checkbox" value="8" name="question2">Pumpkin pie?</p></form>[/code]I'm getting the feeling either the <p>s or the <h1>s are getting in the way of something, but I don't know how to keep the formatting otherwise... Link to comment https://forums.phpfreaks.com/topic/34079-quiz-script-thing/#findComment-160313 Share on other sites More sharing options...
corbin Posted January 14, 2007 Share Posted January 14, 2007 I think that if you check more than 1 check box and you have one name for both of them, they conflict. Try renaming them all to be unique. Link to comment https://forums.phpfreaks.com/topic/34079-quiz-script-thing/#findComment-160321 Share on other sites More sharing options...
DarkendSoul Posted January 14, 2007 Share Posted January 14, 2007 You have to name the checkboxes numbers only. -_- And yeah I didnt test the script before it was all just off the top of my head and im proud of that. :PNumbers only for the names of check boxes!!! and go up from 1, dont skip any numbersbed time Link to comment https://forums.phpfreaks.com/topic/34079-quiz-script-thing/#findComment-160417 Share on other sites More sharing options...
Barand Posted January 14, 2007 Share Posted January 14, 2007 try this which pases the results as an array. The hidden fields are to pass a 0 value if no answer is given for a question.Also use radio instead of checkbox to prevent multiple answers to each question.[code]<?phpif (isset ($_POST['question']) ) { echo '<pre> Question scores ', print_r($_POST['question'], true), '</pre>'; // testing only - remove $score = array_sum ($_POST['question']) ; echo "<p>Score : $score</p>";}?><form action="" method="post"><h1>Pie</h1><p><input type="hidden" name="question[1]" value="0"> <input type="radio" value="5" name="question[1]">Do you like it?<br> <input type="radio" value="10" name="question[1]">Love it?<br> <input type="radio" value="15" name="question[1]">Worship it?<br></p><h1>More Pie</h1><p><input type="hidden" name="question[2]" value="0"> <input type="radio" value="5" name="question[2]">Do you like apple pie?<br> <input type="radio" value="6" name="question[2]">How about chocolate pie?<br> <input type="radio" value="7" name="question[2]">Lemon pie?<br> <input type="radio" value="8" name="question[2]">Pumpkin pie?<br></p><input type="submit" name="action" value="Submit"></form>[/code] Link to comment https://forums.phpfreaks.com/topic/34079-quiz-script-thing/#findComment-160424 Share on other sites More sharing options...
Qu-ko Posted January 14, 2007 Author Share Posted January 14, 2007 Alrighty, I finally got it working. My father wrote a script like this, which I modified:[code]<?phpinclude("header.php");if (!isset($_POST['submit'])){echo '<h1>Test of Doom</h1><p>Insert random test description here.</p>';echo '<form action="" method="POST"><h1>Name</h1><p><input type="checkbox" value="15" name="question[]">Is the character named after yourself (including first, middle, and last names, plus online aliases)?<br><input type="checkbox" value="2" name="question[]">Is the character\'s name an uncommon spelling of an existing word or name?<br><input type="checkbox" value="3" name="question[]">Is the character\'s name a noun or adjective not usually used as a name?<br><input type="checkbox" value="3" name="question[]">Does the character have more than one first, middle, or last name for no real reason?<br><input type="checkbox" value="2" name="question[]">Does the character have a name you really like?<br> <input type="checkbox" value="4" name="question[]">Is it Serena or any other form of Serena?<br><input type="checkbox" value="3" name="question[]">Does the character\'s name describe their personality or appearance?<br><input type="checkbox" value="2" name="question[]">Is the character\'s name unusual in comparison to where they had lived/currently live or grew up?<br><input type="checkbox" value="4" name="question[]">Does the character have a nickname or pseudonym that isn\'t a derivative of their real name?<br> <input type="checkbox" value="5" name="question[]">Is it a "dangerous" nickname? (Ex. Silver Death, The Reaper, etc.)<br><input type="checkbox" value="3" name="question[]">Is the character\'s name foreign, even if the character themself isn\'t?<br> <input type="checkbox" value="4" name="question[]">Is it Japanese?<br> <input type="checkbox" value="2" name="question[]">Is it American?<br> <input type="checkbox" value="4" name="question[]">Is it Egyptian?<br><input type="checkbox" value="2" name="question[]">Does the character have a name taken from mythology/literature/history/etc.? (Ex. Venus, Lestat, Caesar, etc.)<br><input type="checkbox" value="2" name="question[]">Is it a name you would/have named your child or pet?<br></p>// And so on and so forth, until...<p align="center"><input type="submit" name="submit" value="Submit"></p></form>';}else{if (isset($_POST['question'])){// This is really the part I wanted. It totals the values in the $_POST['numbers'] array that have been passed.$a = $_POST['question'];echo '<h1>Results</h1><p align="center">Your Mary Sue score is <b>' . array_sum($a) . '</b>.</p>';echo '<p align="center"><b>Score Key:</b></p><p align="center"><b>Any negative number:</b> Test result 0, insert description here.<br><b>0-30:</b> Test range 1, insert description here.<br><b>31-60:</b> Test range 2, insert description here.</p>';}else{echo 'You did not select any answers. Please <a href="javascript:history.back(1)">go back</a> and select some.';}}include("footer.php");?>[/code]Thanks for your help. It definitely taught me how awful I am at this and how much more I really have to study. Sorry about all the trouble... ^^; Link to comment https://forums.phpfreaks.com/topic/34079-quiz-script-thing/#findComment-160600 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.