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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:05:39+00:00 2026-05-25T00:05:39+00:00

Okay, so I have a table, with 20 fields named q1, q2, q3 all

  • 0

Okay, so I have a table, with 20 fields named q1, q2, q3 all the way to q20

These fields contain answers, which is a number out of 10.

I want to basically choose 10 questions at random, and do some maths and the php to display it on page.

Here is what I have so far:

$selector = "10218";
/* Work out how many rows to divide by */
$nr = mysql_query("SELECT * FROM `reviews` WHERE `selector` = '" . $selector . "'");
$nr = mysql_num_rows($nr);
/* Get the total sum of answers for question 1  */
$q1 = mysql_query("SELECT sum(`q1`) AS `sum` FROM `reviews` WHERE `selector` = '" . $selector. "'");
$q1 = mysql_fetch_array($q1);
$q1 = $q1['sum'] / $nr;
echo "q1 avg is " . round($q1);

Basically, we want to do this for 10 questions – at random.

We don’t want to have to write this code out 20 times, as it is quite strenuous and we want to do some sort of “array” to contain the questions in, and then a for each loop to loop 10 times with a random question?

We want to end up with:

q1 avg is 8
q2 avg is 4
q9 avg is 7
q4 avg is 8
q14 avg is 4
q18 avg is 6
q12 avg is 3
q7 avg is 10
q20 avg is 8
q13 avg is 9

How can we go about doing this? If you didn’t understand let me know and I’ll try rephrase it.

  • 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-25T00:05:40+00:00Added an answer on May 25, 2026 at 12:05 am

    How about this:

    // ...
    $query = 'SELECT';
    for ( $i = 1; $i <= 20; $i ++ ) {
        $query .= ' SUM(`q' . $i . '`) AS `sum' . $i . '`';
    }
    $query .= 'FROM `reviews` WHERE `selector` = "' . $selector . '"';
    
    $q1 = mysql_query( $query );
    $q1 = mysql_fetch_array( $q1 );
    foreach ( array_rand( $q1, 10 ) as $col => $sum ) {
        echo $col . ' is ' . $sum . '<br />';
    }
    

    By the way, the sum is not the average 🙂

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

Sidebar

Related Questions

Okay, I have these table and I also have another table right here: So,idAlbum
Okay, here's the situation: We have a table of about 50 columns (created by
Okay I have this RewriteRule which is supposed to redirect any request for the
Okay I have a series of objects based on a base class which are
okay i have found the way to run a video in a image.... the
Okay so I have the following Code which appends a string to another in
Okay, I'll simplify this post. I have a table tasks containing, for example, a
Okay I have table: ID Name Description Picture 1 Alex Alex desc.. 2 2
Okay, say i have a table with the field code. The query select code
I have an article controller, which manages all article relations like authors, translations, related

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.