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 737527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:43:53+00:00 2026-05-14T07:43:53+00:00

I have have an array as follows: $row when I output the entire array

  • 0

I have have an array as follows:

$row when I output the entire array it produces the following:

1. I like crisps (38) 37% 55% 8% 0%

When I echo one part of the array I get the 4 figures I am interested in.

echo "<td class='set1'>". number_format($row[6], 0) ."%</td>";

The above code outputs the following 4 numbers:

37% 55% 8% 0%

What I would like to do is simply ad the first two numbers together (i.e. 37% + 55%) and output the result (92%). Hope that helps?

I should also point out that the array contains much more information than just these four figures.

As requested: output from var_dump[6]

string(7) "36.8421" string(7) "28.9474" string(7) "39.4737" string(7) "23.6842" string(7) "28.9474" string(6) "8.0000" string(7) "23.6842" string(7) "39.4737" string(7) "11.1111" string(7) "13.8889" string(7) "11.1111" string(7) "13.8889" string(7) "17.1429" string(7) "20.0000" string(7) "28.5714" string(7) "25.7143" string(7) "34.2857" string(7) "28.5714" string(7) "28.5714" string(7) "28.5714" string(7) "20.5882" string(7) "20.5882" string(7) "11.7647" string(7) "29.4118" string(7) "17.6471" string(7) "20.5882" string(6) "3.0303" string(6) "2.9412" string(6) "3.0303" string(7) "38.2353" string(7) "12.1212" string(7) "27.2727" string(7) "18.1818" string(7) "33.3333" string(7) "34.7826" string(7) "17.3913" string(7) "30.4348" string(7) "17.3913" string(7) "17.3913" string(7) "13.0435" string(7) "30.4348" string(7) "27.2727"

Is there any way to do this?

Thanks in advance,

Homer.

Entire code – hope this helps:

if ($mysqli->multi_query($query)) {
do {
    /* store first result set */
    if ($result = $mysqli->store_result()) {
        $i = 1;
        while ($row = $result->fetch_row()) {
            if($i==1){
            echo "<tr><td class='qnum'><span class='bold'>". $row[3] .".</span></td><td class='qtext'> ". $row[4] ." (<span class='italics'>". $row[5] ."</span>)</td><td></td>";
            $i = 0;
            }

            echo "<td class='set1'>". number_format($row[6], 0) ."%</td>";
            $var =  $row[6];
        }
        echo "</tr>";
        $result->free();
    }
    /* print divider */
    //if ($mysqli->more_results()) {
      //  printf("-----------------\n");
    //}
} while ($mysqli->next_result());
}

The section where $row[6] appears is where the four firgures 37% 55% 8% 0% appear – I would like to add the first two of these figures together.

If this helps, here’s the result of the SQL query

response    q1  Responses   qnum    Question_Text   Total   percentage
4           4   14          1       I like crisps   38    36.8421
3           3   21          1       I like crisps   38    55.2632
2           2   3           1       I like crisps   38    7.8947
1           NULL    0       1       I like crisps   38    0.0000

The what I’d like to do is ad the 36.8421 and 55.2632 together.

Hope this helps!

  • 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-05-14T07:43:54+00:00Added an answer on May 14, 2026 at 7:43 am

    Based on the assumption that you always want to sum every two rows, you could do it as follows:

    // This for-loop is intended as a replacement for your 
    // innermost while-loop (rows 6-14 in your question).
    // I used a for-construct because we need to count the 
    // cycles, and it will do it for us.
    // The condition for terminating the loop is still the same
    // as in the original while-loop.
    
    ?><tr><?php
    for ($rownum = 1; $row = $result->fetch_row(); $rownum++) {
        if ($rownum == 1) {
            ?>
            <td class='qnum'><span class='bold'><?php echo $row[3] ?></span></td>
            <td class='qtext'><?php echo $row[4] ?> (<span class='italics'><?php echo $row[5] ?></span>)</td><td></td>
            <?php
        }
    
        // Sum and display on even cycles 
        // as <N> modulo 2 == 0 is true only on even numbers
        if ($rownum % 2 == 0) {
            // Display the formatted value,
            // at the same time adding the previous cycles value to this one
            ?><td class='set1'><?php echo number_format($tmp + $row[6], 0) ?>%</td><?php
    
        // Store the value of $row[6] on odd cycles
        } else {
            $tmp = $row[6];
        }
    }
    ?></tr><?php
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.