Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8531677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:32:32+00:00 2026-06-11T09:32:32+00:00

$answers = array(a, b, c, a, a, a); $S1Q1 = $_POST[‘S1Q1’]; $S1Q2 = $_POST[‘S1Q2’];

  • 0
  $answers = array("a", "b", "c", "a", "a", "a");
  $S1Q1 = $_POST['S1Q1'];
  $S1Q2 = $_POST['S1Q2'];

  $S2Q1 = $_POST['S2Q1'];
  $S2Q2 = $_POST['S2Q2'];
  $S2Q3 = $_POST['S2Q3'];
  $S2Q4 = $_POST['S2Q4'];

  $totalValue .= "About OM | Question 01: " . $S1Q1 . "    " . ($S1Q1 == $correct[0]) ? 'Correct' : 'Wrong' . "\n";
  $totalValue .= "        | Question 02: " . $S1Q2 . "    " . ($S1Q2 == $correct[1]) ? 'Correct' : 'Wrong'  . "\n\n";

    $totalValue .= "About EHS         | Question 01: " . $S2Q1 . "    " . ($S2Q1 == $answers[2]) ? 'Correct' : 'Wrong' . "\n";
    $totalValue .= "                   | Question 02: " . $S2Q2 . "    " . ($S2Q2 == $answers[3]) ? 'Correct' : 'Wrong' . "\n";
    $totalValue .= "                   | Question 03: " . $S2Q3 . "    " . ($S2Q2 == $answers[4]) ? 'Correct' : 'Wrong' . "\n";
    $totalValue .= "                   | Question 04: " . $S2Q4 . "    " . ($S2Q2 == $answers[5]) ? 'Correct' : 'Wrong' . "\n\n";

I have the above php script, what method or function can I use to calculate how many Correct and how many Wrong the user has in a loop and then calculate percentage?

Example:
Correct: 4
Wrong: 2
Total: 6
Percentage Correct: ((4/6) * 100) =
66.666666666% => round it to 66%

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:32:33+00:00Added an answer on June 11, 2026 at 9:32 am

    Here is a quick and dirty way to accomplish it without changing what you already have:

    $correct = substr_count($totalValue, "Correct\n");
    $wrong = substr_count($totalValue, "Wrong\n");
    $total = $correct + $wrong;
    $percentage = floor($correct / $total * 100);
    

    That said, I think this could be done much better. I would pass the variables in as an array. Your post string would look something like this:

    answers[s1][q1]=a&answers[s1][q2]=b&answers[s2][q1]=c&answers[s2][q2]=a
    

    This would be received by PHP like this:

    answers => Array(
        's1' => Array(
            'q1' => 'a',
            'q2' => 'b'
        ),
        's2' => Array(
            'q1' => 'c',
            'q2' => 'a'
        )
    )
    

    Take a look at this example. It looks a bit longer and perhaps a bit more complicated, but it is much more flexible and expandable. If you need to add any questions, answers, etc…all you have to do is add the corresponding array entry. In the long run, I think you will be much happier with that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yes, I've read this question & answers: Passing an array by reference in C?
There have been other question/answers on negative array in C in the forum, but
I have a question with multiple answers. I want an array that has two
Possible Duplicate: Different answers from strlen and sizeof for Pointer & Array based init
I have a question with an array of answers. Each answer has a boolean
Im using the answer to this question to convert an array of XML to
I have read many answers to questions about dynamically resizing NSWindows and nothing has
I have array of answers including marks. Syntax - [ANSWER1|MARK1, ANSWER2|MARK2 ...] answers =
After viewing some answers on stackoverflow, preg_match_all('/<img[^>]+>/i',$html, $result); $img = array(); foreach( $result[0] as
I'm writing a quiz program. I have a 1-Dimensional array of correct answers from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.