PHP - Testing A Quiz Question
I am creating a quiz, with Multiple choice, true and false, and fill in the blanks questions.
The first 2 types are working fine, and I am working on tesing the fill the blank questions...
See code:
//QUESTION 9 if (empty($_POST["ix"])) { $ixErr = "Please select an answer."; } else { $ix = test_input($_POST["ix"]); // check if question only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$ix)) { $ixErr = "Only letters and white space allowed."; else ($ix == "charming") { $score = $score+10; echo "<p>9.) " . $ix . " is correct.</p>\n"; } else { echo "<p>9.) " . $ix . " is incorrect.</p>\n"; } } }First, I am testing to see if it is empty, if yes, display error, otherwise use the test_input function on the entered information. Then I want to test and verify that the entered data only contains characters and/or blank spaces, if that returns yes, then I want to test the content for the correct answer. But for some reason the code gets stuck upon testing for the correct answer, please note: I did try elseif, but same issue, please help, thank you. Similar TutorialsWow, am I glad I found this forum! I spent the past few hours trying to modify a PHP/mySQL script in order to test it. What this script does is basically look up a mySQL database (based on ip2nation) to find out what country and currency the IP address belongs to. This is the code sample: Code: [Select] $sql = 'SELECT c.country, c.curr_code FROM ip2nationcountries c, ip2nation i WHERE i.ip < INET_ATON("24.24.24.24") AND c.code = i.country ORDER BY i.ip DESC LIMIT 0,1'; $countryName = $db->get_sql_row($sql); I tested the code in PHPmyAdmin and it returns 2 rows: country ------- curr_code (row names) United States ------- USD (actual values) So basically 2 variables, United States and USD. Now comes the problem, in order to test the script for several countries, I need to be able to "override" the result in the PHP script I tried to do that by commenting out the part that returns the result from the mySQL query and modifying it with the following style, but I think I'm doing something wrong: Code: [Select] ### $countryName = $db->get_sql_row($sql); ### $countryName = "United States USD"; Is there any way that I can temporarily override the result returned by mySQL and "fake it" so to say? I understand the mySQL query returns 2 rows, so how would I go about overriding 2 rows? I hope this makes sense ...you're all my last hope. Hi, My name is Karel van der Veen. I am trying to creat a script for a quiz on my website but I am stuck. I was hoping you might have an idea for the solution. The idea is a page with a picture and multiple choice questions about that picture. For example a picture of a bike. Question 1: what colour is the bike? --> choices are green, red, blue. Question 2: what shape are the wheels? --> round, square. I have made a script that compares the answers given by the student, with the correct answers I put in the database corresponding with the bike on the picture. If the student has it right, than after clicking on "next", the "correct answer" page will load. If he made a mistake a "wrong answer" page will load. So far the script works. But what I would like to happen is if the student gives a wrong answer, that after clicking on the `next` button, he will go back to the front page where the picture and the questions are with a sign or mark around the question he answered wrong, so he knows what question he had wrong and has to answer again. Below is the script I have so far. Do you know what I have to change or to add to the script? regards Karel Code: [Select] script of the question page: <?php $array_vorm = Array("rond","vierkant"); $array_kleur= array("rood","blauw","groen"); $array_grootte = array("4x4","8x8"); ?> <form method="post" action="results.php"><br /> <div id="vorm"> <div id="vorm1"><img src="images/vorm.gif" alt="vorm" width="75" height="75" class="vorms" /></div> <div id="vorm2"> <table> <tr><td> <label class="vormhead">VORM</label><br> <select name="form_vorm[]" class="randmenu" size="4" multiple > <option><?php echo implode("</option><option>",$array_vorm); ?></option> </select> </td></tr> </table> </div> </div> <div id="kleur"> <div id="kleur1"><img src="images/kleur.gif" alt="vorm" width="75" height="75" class="kleur" /></div> <div id="kleur2"> <table> <tr><td> <label class="kleurhead">KLEUR</label><br> <select name="form_kleur[]" class="randmenu" size="4" multiple > <option><?php echo implode("</option><option>",$array_kleur); ?></option> </select> </td></tr> </table> </div> </div> <div id="grootte"> <div id=" grootte 1"><img src="images/ grootte .gif" alt="vorm" width="75" height="75" class=" grootte " /></div> <div id=" grootte2"> <table> <tr><td> <label class=" grootte head">GROOTTE</label><br> <select name="form_ grootte []" class="randmenu" size="4" multiple > <option><?php echo implode("</option><option>",$array_ grootte ); ?></option> </select> </td></tr> </table> </div> </div> <input type="submit" class="zoekbalk" value="zoeken" /> And this is the script of the result page: <?php error_reporting ( E_ALL | E_STRICT ); ini_set ( 'display_errors', 1 ); ?> <?php include 'connect.php'; ?> <?php $geenbeschrijving = "De beschrijving is onjuist. $geenselectie = "Je hebt geen selectie gemaakt, maak eerst een selectie en klik dan op zoeken. <br/><br/> $antwoord = "Antwoord: "; $diagnose = "Beschrijving: "; $diagnose2 = "Diagnose: "; $vormeggo = "Vorm: "; $kleureggo = "Kleur: "; $grootteeggo = "Grootte: "; $array_vorm = array("rond","vierkant"); $array_kleur= array("rood","blauw","groen"); $array_grootte = array("4x4","8x8"); if($_SERVER['REQUEST_METHOD'] == 'POST' ) { $query = "select * from foto1 where "; $query_init = $query; $first_option = true; if (isset($_POST["form_vorm"])){ if ($first_option == false){ $query .= " AND "; } else{ $first_option = false; } $query .= "("; for ($i=0; $i<=count($_POST["form_vorm"])-1; $i++){ if ($i == 0){ $query .= "(vorm LIKE '%".$_POST["form_vorm"][$i]."%') "; }else{ $query .= " OR (vorm LIKE '%".$_POST["form_vorm"][$i]."%') "; } } $query .= ")"; } if (isset($_POST["form_ kleur "])){ if ($first_option == false){ $query .= " AND "; } else{ $first_option = false; } $query .= "("; for ($i=0; $i<=count($_POST["form_ kleur "])-1; $i++){ if ($i == 0){ $query .= "( kleur LIKE '%".$_POST["form_ kleur "][$i]."%') "; }else{ $query .= " AND (kleur LIKE '%".$_POST["form_ kleur "][$i]."%') "; } } $query .= ")"; } if (isset($_POST["form_ grootte "])){ if ($first_option == false){ $query .= " AND "; } else{ $first_option = false; } $query .= "("; for ($i=0; $i<=count($_POST["form_ grootte "])-1; $i++){ if ($i == 0){ $query .= "( grootte LIKE '%".$_POST["form_ grootte "][$i]."%') "; }else{ $query .= " AND (grootte LIKE '%".$_POST["form_ grootte "][$i]."%') "; } } $query .= ")"; } if ($query == $query_init){ echo "<div class=\"geenbeschrijving\">" . $geenselectie . "</div>"; }else{ //echo $query."<br><br>"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)<>0) { while ( $row = mysql_fetch_assoc ( $result ) ) { echo "<div class=\"beschrijving\">" . $antwoord . "</div>"; echo "<div class=\"keuze\">" . $vormeggo . $row["VORM"]. "</div>"; echo "<div class=\"keuze2\">" . $kleureggo . $row["KLEUR"]. "</div>"; echo "<div class=\"keuze2\">" . $grootteeggo . $row["GROOTTE"]. "</div>"; </div>"; $beschrijvingquery = "select BESCHRIJVING from beschrijving where Catalog_ID=".$row["ID"]; if ($beschrijvingresult = mysql_query($beschrijvingquery)) { if (mysql_num_rows($beschrijvingresult)<>0) { while ( $beschrijvingrow = mysql_fetch_assoc ( $beschrijvingresult ) ) { echo "<div class=\"beschrijving\">" . $diagnose . "</div>"; echo "<div class=\"beschrijving2\">" . $beschrijvingrow["BESCHRIJVING"]."</div></br>"; } } else { echo "geen beschrijving<br />"; } } $diagnosequery = "select DIAGNOSE from diagnose where Catalog_ID=".$row["ID"]; if ($diagnoseresult = mysql_query($diagnosequery)) { if (mysql_num_rows($diagnoseresult)<>0) { while ( $diagnoserow = mysql_fetch_assoc ( $diagnoseresult ) ) { echo "<div class=\"beschrijving\">" . $diagnose2 . "</div>"; echo "<div class=\"beschrijving2\">".$diagnoserow["DIAGNOSE"]."<br /><br />"; } } else { echo "geen beschrijving<br />"; } } $fotoquery = "select FOTOthumb, FOTObig from foto where Catalog_ID=".$row["ID"]; if ($fotoresult = mysql_query($fotoquery)) { if (mysql_num_rows($fotoresult)<>0) { while ( $fotorow = mysql_fetch_assoc ( $fotoresult ) ) { echo "<div class=\"beschrijving4\">" .'<a title="' . $fotorow['FOTObig'] . '" href="images/big/' . $fotorow['FOTObig'] . '" rel="lightbox"><img src="images/thumb/' . $fotorow['FOTOthumb'] . '"></a></br></br><hr><hr> '; } } else { echo "<img src='geenfoto.jpg' alt='geen foto' /><br />"; } } } } else { echo "<div class=\"geenhuidaandoeningen\">" . $geenhuidaandoeningen . "</div>"; } } } } ?> Just wanted some general advice/guidance, I know there are plenty of articles and tutorials online about unit testing, but I just wanted to get a first hand perspective off of some of you guys... What are some real world examples of actual PHP Unit Testing? Also any unit testing frameworks you can recommend? Apologies if this has been covered before. Hello all, I have a script uploading files on the server. I would like to send a confirmation to user if the file had been uploaded well. Code: [Select] $targetFile = str_replace('//','/',$targetPath) . $filename; move_uploaded_file($tempFile,$targetFile); if (str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile)) { mail($to, 'upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been successfully uploaded"); }else { mail($to, ' upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been NOT uploaded"); } I presume I do something wrong because it doesn't work. The successfull email is sent each time. Thank you very much for helping, Eric Hi, I have a class XMLtool where one of the functions is to output a parameter-passed dom document and then output to the browser screen, so basically: function outputXML($domDocument) { if has attributes print cur nodes attributes for each child, if child is element node outputXML(cur node) else if child is text print text end for each } Please I am not sure what type of unit tests I can perform. I'd appreciate all the help! Can someone give me a simple socket script that I can copy and paste. It can return anything you like. I just want to make sure I have a working socket connection, and if not then you can maybe help me fix it. Hi. I am trying to test to see if a particular record id (int) is included in array. This is the code I am using $sql2 = "SELECT `id` FROM `orderitems` WHERE `orderid` = '" . $orderid . "'"; $orderlist = mysql_query($sql2) or trigger_error(mysql_error() . '<br />Query was:' . $sql2); $orderarray = array(); while ($orderitem = mysql_fetch_array($orderlist)); ( $orderarray[] = $orderitem['id']; ) $prodid=$row['prodid']; if (in_array($prodid, $orderarray)) { echo "<td> In.</td>"; } else { echo "<td> Out.</td>"; } Every case is outputting "Out" even though some should pass. I have verified that the variables are outputting the correct thing, but am not sure about the array. I am just a beginner, so i am hoping there is something really obvious I am doing wrong. Thanks for reading this. Hi All,
I have developed a project in PHP, it is a web application.
Now i want to do testing on it,can any one suggest me a free tool by which i can test the php website.
Thanks in advance.
This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=352939.0 I have a part of my code that requires php 5.3+ how can I test to see if the user has 5.3 or greater? At first I thought this would be a good idea, but then I realized that it isn't such as a version: 5.2.10 (not sure if that is an actual version or not). if((int)preg_replace("/\./", "", phpversion()) >= 530){ // Allow functionality } using my code, using 5.2.10 it would allow it, but it shouldn't because it isn't larger than 5.3. what can I do? these are sections of my profile, just plain right now so easier for u to read: Code: [Select] $notes = 'notes'; $signature = 'signatures'; $friends = 'friends'; $middle=array ( "0" => array ( "section" => $notes, "hide" => "0", "c" => "apple" ), "1" => array ("section" => $signature, "hide" => "0", "c" => "apple" ), "2" => array ("section" => $friends, "hide" => "0", "c" => "apple" ), ); $order = array(2,1,0); foreach ($order as $index) { echo '<br>'.$middle[$index]['section']; } If the hide = 1 how do i hide a array in my foreach? Hi, I just integrate my website with paypal test account. It works fine. now i want that if i pay on paypal website it should return to my website. i use following code to do that <input type="hidden" name="return" value="http://www.miricreation.ae.com/shopping-cart-details.php"> but it didn't working. i also enable auto return url into my test account. but no luck. is there anything need to do for this in code?????????? Please help Thanks So I built a tool for my cms a while back and have decided to share it. The only 2 things that I know of that are similar in nature is phpmyadmin or sql buddy. Now my tool is not nearly as in depth as either of those but what it lets you do is run any query string right from the webpage. But the cool thing that it has over phpmyadmin and sql buddy is that it has buttons for many of the common commands and a ajax select to get the available column names for the selected table. So now you don't have to try and remember what table had what column and "how did I spell that column again".
I use this to test sql strings for syntax and to quickly see if it returns the results I expect without having to edit the code and then save, upload or hit refresh or whatever. Since all the the stuff is done via ajax, it's all quickly displayed and you can change the query immediately and hit the Run Query button again.
I just thought that this could be helpful and useful for others. You can download it here http://amecms.com/ar...ckly-and-Easily
This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=326737.0 HI - I'm trying to write an expression which tests the existence of a value within a POST variable. I have tried every which way ie isset, empty, ==0.00, NULL and yet I'm doing something wrong as nothing works. I hope you can help this student of PHP ! The expression is testing whether a price per lb exists ie the price of chicken per pound. But some products are not priced this way eg a jar of jam has no $pricelb[$i]. Here's some detail. The expression I am testing is: Code: [Select] if (!isset($pricelb[$i])){ // FIRST HALF FAILS $ordervalue = $price[$i]; $weight = '0.00';} else // SECOND HALF WORKS $pricelb = $pricelb[$i];{ $weight = $weight[$i]; $ordervalue = $pricelb * $weight; The $pricelb[$i] refers to an array of product weights coming off of the array $pricelb = $_POST['pricelb'] which I then cycle through using a for Loop, hence $pricelb[$i]. Bottom Line: if there is no pricelb then the then the ordervalue for the purchase is the same as the starting unit price. Suppose I have 2 products on the order. One with a pricelb of 5 and one with none. Then I do print_r on $_POST['pricelb'] I get: Code: [Select] Array ( [0] => 5 ) I.E. the second value is absent. YET, if I look into the DB table I see pricelb 0.00 for the second value - very confusing ! MANY MANY thanks ! for all your help ! I want to give the visitors to my site the option of setting certain preferences and remembering what was chosen from one visit to the next. They might have the choice of setting the backgrond colour of the web page or the font or whatever.
I've already written and tested code to write standard cookies that preserve their choices. However, I'm wondering if that's the best way or if there is a better approach. I don't want visitors to have to re-choose their preferences every time they arrive at the site, which would appear to be the result of using a session cookie stored on the server.
If standard cookies ARE the best solution for my situation, how can I test if the visitor has cookies enabled on their browser? I know that some people definitely don't enable cookies and I'm quite prepared to advise them that they won't be able to save preferences until they enable cookies. However, I first need to know how I can tell definitively if cookies are enabled.
Most of the simple code examples I've found by googling simply try to write a cookie and then try to read it; if both succeed, they think the they've established whether cookies are enabled. The more sophisticated examples, particularly those at stackoverflow, come closer but all of them seem to be critiqued by someone to the effect that they won't work in this or that particular situation.
Given how long cookies and PHP have been in existence, I have to believe that SOMEONE has figured out a definitive way of testing whether cookies are enabled that works in all reasonable circumstances. Can anyone share that technique with me?
hi, Hi have a web application www.prakhargoel.com/projects/lms/ that is used by students for logging and requesting a computer system in the internet lab. There is a corresponding labadmin at www.prakhargoel.com/projects/lms/labadmin. Though I tried to make this web app as secure as possible, there might be some shortcomings in the security which could be used by hackers. Please analyse the vulnerabilities and tell where I need to improve. If anyone needs, I can provide the php code for further testing. I have: 1) applied sha1 encryption to all passwords. 2) timeout sessions 3) sql injection filters, etc I'm pulling my hair out trying to figure out a simple problem. I've got the following PHP code in an HTML page: $shutoff_date = strtotime($wkend_start_date) - 259200; if ($shutoff_date > $todays_date) { $current_month = date("m"); $current_year = date("y"); $freshman_eligible = 0; if ($current_month >= 2 && $current_month <=7) { $freshman_eligible = 1; } if ($current_month <= 7) { $seniors = $current_year ; } else {$seniors = $current_year + 1; } $juniors = $seniors + 1; $sophomores = $seniors + 2; $freshman = $seniors + 3; echo "HTML Stuff blah, blah, blah"; if ($freshman_eligible = 1) { echo "<option value=\"$freshman\">$freshman</option>";} } The last test to see if $freshman_eligible is equal to 1 is always testing true, and it's actually resetting $freshman_eligible to whatever I put inside the IF statement. No matter what the variable is set to previously, it will reset to whatever I test for in the last section, and hence always tests true. I thought maybe it was an issue with scope, but I'm not sure what the issue would be or how to resolve it. What am I missing??? I've finished the front end design of my application and now intend to provide a version to my potential users to gain feedback.
Does anyone have any experience with the best methods of capturing this feedback? What methods have you used in the past? Questionnaires?
I want feedback on things like the colour scheme, layouts, ease of use etc. From what I've read I can do things like a 1-5 satisfaction scale on multiple questions, but this seems a little vague
This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=320482.0 |