5 Replies - 548 Views - Last Post: 23 April 2013 - 01:05 PM Rate Topic: -----

#1 ferdinandmucos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-April 13

array submission to Php server side script with forms

Posted 23 April 2013 - 11:13 AM

I have a text file:
$string = 
'1)	The most important feature of spiral model is: requirement analysis. risk management. quality management. configuration management 2)	The worst type of coupling is: Data coupling. control coupling. stamp coupling. content coupling 3)	One of the fault base testing techniques is: unit testing. beta testing. Stress testing. mutation testing 4)	A fault simulation testing technique is: Mutation testing. Stress testing. Black box testing. White box testing 5)	RS is also known as: specification of White box testing. Stress testing. Integrated testing. Black box testing 6)	Which of these terms is a level name in the Capability Maturity Model?: Ad hoc. Repeatable. Reusable. Organized 7)	FP-based estimation techniques require problem decomposition based on?: information domain values. project schedule. software functions. process activities 8)	What types of models are created during software requirements analysis?: Functional and behavioral. Algorithmic and data structure. Architectural and structural. Usability and reliability 9)	Which of the following is not an attribute of software engineering: Efficiency. Scalability. Dependability. Usability 	


A code that reads the text file and puts into array and the array into html forms for submission:

<html>
    <head>
	<style>
#text_disabled{
border:0;
font-size:10pt;
color:black;
}
</style>
    <title>Question and Answer Read From Text File</title>
    </head>
    <body>
        <form action="processor.php" method="POST" align="center">
        <b><u> QUESTIONS AND ANSWERS QUIZ</u></b> <br /><br />
<?php
$openFile = fopen("questionandanswers.txt", "r") or exit ("unable to open the text       file");
$string = fread($openFile, filesize("questionandanswers.txt"));

//Regex to get from the first number to the string's end, so we ignore the Number Question... bit;
preg_match('/\d.*/', $string, $match);

//We get all the strings starting with a number until it finds another number (which will be the beginning of another question;
preg_match_all('/\d\D*/', $match[0], $results);

$qas = array(); // We prepare an array with all the questions/answers
foreach($results[0] as $result){
    //Separating the answer from the string with all the answers.
    list($question, $all_answers) = explode(':', $result);

    //Separating the different answers
    $answers_array = explode('.', $all_answers);

    //Stuffing the question and the array with all the answers into the previously prepared array;
    $qas[] = array('question' => $question, 'answers' => $answers_array);
}

//Looping through the array and outputting all the info into a form;
foreach($qas as $k=>$v)
{
echo "<input id='text_disabled' style='width:40%' type='text' value='".$v['question']."' name='questions[]' disabled>";
echo "<select name='selected_answers[]>";
foreach($v['answers'] as $answer){
echo "<option value='".$answer."'>".$answer."</option>";
}
echo "</select>";
echo "<br/><br/>";
}
?>
<input type="submit" value="Submit Answers">&nbsp <input type="reset" value ="Clear Answers">
</form>
    </body>
</html>


What I have problems with is how to handle the processor.php page that is suppose to add one for every correct answer and a zero for every wrong answer and at the end displays the questions both missed and correct answers in their order and the final score.

Is This A Good Question/Topic? 0
  • +

Replies To: array submission to Php server side script with forms

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,508
  • Joined: 12-December 12

Re: array submission to Php server side script with forms

Posted 23 April 2013 - 11:43 AM

Your text file is not the easiest to work with, as you have discovered; using regex and searching for a number is not the best way to work, and will break down fairly easily.

Assuming you want to continue with a text file, and not a database, I might do something like this:

Quote

ANDYQ1 The text of the question here. It can be one long line.
ANDYA1 Answers,Comma,Separated,Here,Another One
etc..

That is, using code numbers for the questions. Then you can read the file line by line, examining the first 6 or so characters, and using split() to break up the comma separated values.

Then, in a separate file:
ANDYQ1:A,ANDYQ2:B,ANDYQ3:C

In this way it won't matter what order the questions appear in. You can also insert ANDYQ1, etc., as ids for the selects, and A,B,C as the options values. You don't need to store the precise text of the answer, just which option's value is correct.

Of course, you might decide to keep the question, the options, and the answers in a single file.

This post has been edited by andrewsw: 23 April 2013 - 11:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,508
  • Joined: 12-December 12

Re: array submission to Php server side script with forms

Posted 23 April 2013 - 11:54 AM

From the docs:

Quote

split() is deprecated as of PHP 5.3.0. preg_split() is the suggested alternative to this function. If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.

Was This Post Helpful? 0
  • +
  • -

#4 ferdinandmucos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-April 13

Re: array submission to Php server side script with forms

Posted 23 April 2013 - 12:13 PM

I'm not well conversant with either, using your formula would leave me even much confused.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#5 ferdinandmucos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-April 13

Re: array submission to Php server side script with forms

Posted 23 April 2013 - 12:48 PM

I'm not conversant with much of php function. But it seems you are introducing a new concept that might make it easier to conceptualize, your guidance through the same would be highly appreciated to understand it better. thank you.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,508
  • Joined: 12-December 12

Re: array submission to Php server side script with forms

Posted 23 April 2013 - 01:05 PM

View Postferdinandmucos, on 23 April 2013 - 07:48 PM, said:

I'm not conversant with much of php function. But it seems you are introducing a new concept that might make it easier to conceptualize, your guidance through the same would be highly appreciated to understand it better. thank you.

If that is your code in the first post then you already make use of most of the features that my approach would require: opening a file, looping, using explode(), creating arrays, etc.. All I'm suggesting is, storing the information in the file(s) in a slightly different way.

If, however, you are still not comfortable with the features that you are using then you'll need to continue studying them. There are also tutorials here @DIC that may be helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1