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

  • Home
  • SEARCH
  • 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 9112713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:48:03+00:00 2026-06-17T03:48:03+00:00

I have a phpfiddle here: http://phpfiddle.org/main/code/get-rps The problem I am getting is that in

  • 0

I have a phpfiddle here: http://phpfiddle.org/main/code/get-rps

The problem I am getting is that in the Total Marks column, it is only display the total marks for the last question in all of the rows for all questions, rather than looping through all the total marks and displaying the correct total marks for each question. What am I doing wrong in the code?

Below is code:

<?php
$incorrect_ans = array(
                   array('A','C','D'),
                   array('B','C','D'),
                   array('A','B','D'),
                   array('A','B','C'));

$searchQuestionNo = array(
                   1,
                   2,
                   2,
                   3);

$totalMarks = array(
                   3,
                   5,
                   5,
                   2);

$ques_ans = array();    //to store incorrect answers against ques no.

$q_occ_count = array_count_values($searchQuestionNo);
foreach($searchQuestionNo as $key => $questionNo)
{
    if ( ! array_key_exists($questionNo, $ques_ans))
    {
        if($q_occ_count[$questionNo] === 1) //if a ques has only one correct ans
        {
            $ques_ans[$questionNo] = $incorrect_ans[$key];  //store the array of incorrect ans against the ques no as key 
        }
        else //if a ques has more than 1 correct ans
        {
            //find the intersection of incorrect_ans arrays for this ques
            $q_keys = array_keys($searchQuestionNo, $questionNo);
            $q_incorrect_ans = $incorrect_ans[$q_keys[0]];
            foreach($q_keys as $q_key) {
                $q_incorrect_ans = array_values(array_intersect($q_incorrect_ans, $incorrect_ans[$q_key]));
            }       
            $ques_ans[$questionNo] = $q_incorrect_ans;  //store the array of incorrect ans against the ques no as key
        }
    }
}
?>
<table border='1' id='penaltytbl'>
<thead>
<tr>
<th class='questionnoth'>Question No.</th>
<th class='answerth'>Incorrect Answer</th>
<th class='marksth'>Total Marks</th>
</tr>
</thead>
<tbody>
<?php

foreach($ques_ans as $questionNo => $inc_ans)
{
    $q_row_span = count($inc_ans);
    $row_count = 0;
    ?>
    <tr class="questiontd">

        <!-- Question No -->
        <td class="questionnumtd q<?php echo $questionNo?>_qnum" rowspan="<?php echo $q_row_span ?>">
                <?php echo $questionNo?><input type="hidden" name="numQuestion" value="<?php echo $questionNo?>" />
        </td>

        <!-- first incorrect ans -->
        <td class="answertd"><?php echo $inc_ans[$row_count]; ?></td>

        <!-- Marks -->
       <td class="totalmarkstd" rowspan="<?php echo$q_row_span?>"><?php echo$totalMarks[$key]?></td>
    </tr>
    <?php
        //remaining incorrect answers in separate row (if any) follows here
    if($row_count < $q_row_span - 1) 
    {
        for($i=($row_count + 1); $i<$q_row_span; $i++) { ?>     
            <tr><td class="answertd"><?php echo $inc_ans[$i]; ?></td></tr>
    <?php
        }
    }
}
?>
</tbody>
</table>
  • 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-17T03:48:04+00:00Added an answer on June 17, 2026 at 3:48 am

    This line is your problem.

    <?php echo$totalMarks[$key]?>

    $key is set from your loop above, and is never reset in the bottom loop, so it is still has the last value from that loop. I’m assuming it should be <?php echo$totalMarks[$questionNo]?>

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

Sidebar

Related Questions

http://phpfiddle.org/main/code/get-rps Now it is not shown in the above fiddle that it is able
I currently have this piece of code: NSString *strURL = [NSString stringWithFormat:@http://www.sample.com /phpFile.php?firstname=%@,txtfirstName.text]; NSString
Here is what I am trying to accomplish. I have a form that uses
Have a problem that seems easy on paper but i'm having a big problem
Have a look at this code: #include <iostream> using namespace std; int main() {
Have such a problem, hope you'll help me.. Can't find anywhere. Here is the
Have a fun issue with sharepoint calendar view filtering. That code works fine: SPSecurity.RunWithElevatedPrivileges(delegate()
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
Have an issue with marshall and unmarshall readers and writers. So here it is.
My output does not insert the data for Total Marks correctly, it seems like

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.