LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2015, 11:47 AM   #1
esteeven
Senior Member
 
Registered: Oct 2001
Location: Bristol UK
Distribution: Arch Slackware Ubuntu
Posts: 1,082

Rep: Reputation: 52
Thumbs up Replace numbers with a sequence in PHP ising Vim or Geany


Hello
I'm finding it hard to even articulate the problem I have set myself.

I am writing a multiple choice quiz. It contains 100 question. I created a test model of the quiz with 4 questions and it works. My PHP is here:


Code:
 <?php
            
            $answer1 = $_POST['question-1-answers'];
            $answer2 = $_POST['question-2-answers'];
            $answer3 = $_POST['question-3-answers'];
            $answer4 = $_POST['question-4-answers'];
            $answer5 = $_POST['question-5-answers'];
        
            $totalCorrect = 0;
            
            if ($answer1 == "C") { $totalCorrect++; }
            if ($answer2 == "B") { $totalCorrect++; }
            if ($answer3 == "A") { $totalCorrect++; }
            if ($answer4 == "D") { $totalCorrect++; }
            if ($answer5) { $totalCorrect++; }
            
            echo "<div id='results'>$totalCorrect / 4 correct</div>";
            
        ?>
What I want to do is extend what I have up to 100 without having to copy and paste what I have got and then manually replace the numbers up to 100. I realise that I will have to manually insert the correct answer for all 100 questions as there is no pattern to the answers.

I'm really in the dark here. I don't know whether this is something that can be achieved within Vim or whether I need to use a script or another tool.

Thanks in advance for your help.
 
Old 04-07-2015, 12:54 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
An array would be handy:
Code:
$right_answers = array (1=>'C',2=>'B',3=>'A',4=>'D');
foreach ($right_answers as $key => $value) {
    $post_index= 'question-'.$key.'-answers';
    if (isset ($_POST[$post_index])) {
        $answer= $_POST[$post_index];
        ...
    }
}

Last edited by NevemTeve; 04-07-2015 at 12:55 PM.
 
Old 04-08-2015, 02:39 AM   #3
esteeven
Senior Member
 
Registered: Oct 2001
Location: Bristol UK
Distribution: Arch Slackware Ubuntu
Posts: 1,082

Original Poster
Rep: Reputation: 52
Thanks NevemTeve. If I understand correctly (which I probably don't : this is all very new to me ) the code you have given me improves what I have done so far but I am not sure it answers my question. I really want to extend the answers to 100. The actual correct answers - A, B, C and D - do not follow a regular, predictable pattern and I will have to input them manually and individually. There will be more than one multiple choice test (of 100 questions) using the same come to "mark" the test.

The final lines will be:

Code:
 
            $answer99 = $_POST['question-99-answers'];
            $answer100 = $_POST['question-100-answers'];
        
            $totalCorrect = 0;
            
            if ($answer99 == "C") { $totalCorrect++; }
            if ($answer100 == "B") { $totalCorrect++; }
I simply want a way of replicating what I have but increasing the answer numbers up to 100.
 
Old 04-08-2015, 03:36 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Of course, array '$right_answers' has to have 100 elements is the final version. It will be sg like this:

Code:
$right_answers = array (1=>'A','B','C','D','D','A','C','B','B','A', /* first ten */
....
'D','B','C','D','D','A','C','B','B','C'); /* last ten */

$totalCorrect= 0;
foreach ($right_answers as $key => $value) {
    $post_index= 'question-'.$key.'-answers';
    if (isset ($_POST[$post_index])) {
        $answer= $_POST[$post_index];
        if ($answer === $value) ++$totalCorrect;
    }
}
 
1 members found this post helpful.
Old 04-08-2015, 12:35 PM   #5
esteeven
Senior Member
 
Registered: Oct 2001
Location: Bristol UK
Distribution: Arch Slackware Ubuntu
Posts: 1,082

Original Poster
Rep: Reputation: 52
Okay. That makes more sense now. I'll have a look at this later and try to implement it. Many thanks NevemTeve.
 
Old 04-10-2015, 09:03 PM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Ideally, to generalise the solution (to create the PHP) you should store the questions, the answer choices and right solution in a data base and use a loop to create the PHP file.

This way you can key in the100 data sets (consisting of question, answer choices and right answer) off line ie. at your own pace into say a simple text file. Then write a small script (I prefer awk) to generate what ever code you want.

IN MY VIEW THIS WOULD SOLVE YOUR MAJOR PROBLEM OF REPEATEDLY ENTERING, CORRECTING AND VERIFYING THE INTEGRITY OF THE PHP STRUCTURE USING VIM.
You could then mail this PHP file off to others to use.

Another solution once you have the question bank ready would be write a PHP programme that reads the data and renders the page. You obviously know this. Of course this requires access to a personal or test web server that eders PHP onto the browser.

Ok

Last edited by AnanthaP; 04-10-2015 at 09:08 PM.
 
2 members found this post helpful.
Old 04-27-2015, 04:17 PM   #7
esteeven
Senior Member
 
Registered: Oct 2001
Location: Bristol UK
Distribution: Arch Slackware Ubuntu
Posts: 1,082

Original Poster
Rep: Reputation: 52
Thanks NevemTeve - that really did the trick. Everything now works as it should. It's amazing how just seeing the right code at the right time can open up a whole new world. I've been playing with this array since you posted it and I really feel as though I have learned something. Cheers!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] generating series of numbers of sequence vjramana Programming 7 03-19-2011 10:34 AM
[SOLVED] Replace sequential numbers in a file with a different sequence using sed thefiend Linux - Newbie 6 04-12-2010 10:29 PM
next sequence of numbers ????? fachamix General 11 11-30-2009 06:52 AM
[SOLVED] regex with numbers in sequence schneidz Programming 6 04-28-2009 06:52 PM
sequence of numbers, how to extract which numbers are missing jonlake Programming 13 06-26-2006 03:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration