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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:04:13+00:00 2026-06-05T00:04:13+00:00

I want to create simple poll system. I have two question and two answers.

  • 0

I want to create simple poll system. I have two question and two answers. First Question has two answer but second question hasn’t any answer. In a sense I want to get:

First question?
Answer1
Answer2

Second question?

But I’m getting

First question?
Answer1
Answer2

Second question?
Answer1 
Answer2

Second question need not to have Answer1 and Answer2(same with First question answers but I have two answer in mysql).

And my foreach loops.. How need i to change my loops?

foreach($questions->getQuestion($_GET["category"]) as $data) // Questions
{
    echo $data["question"] . "<br/>";
    foreach($questions->getAnswer($data["id"]) as $answers) // Answers
    {
        echo $answers["answer"] . "<br/>"; // This needn't print data to below of Second Question
    }
}

Functions:

public function getQuestion( $cat )
{
    if(empty($cat))
    {
        header("location:index.php");
    }

    $this->sql = "SELECT * FROM questions WHERE category='" . $cat . "'";
    $data = parent::getData();
    return $data;
}

public function getAnswer( $question )
{
    $this->sql = "SELECT * FROM answers WHERE questions='" . $question .  "'";
    $data = parent::getData();
    return $data;
}

Datas:

  Array
(
    [0] => Array
        (
            [id] => 1
            [soru] => First Question
            [kategori] => 1
        )

    [1] => Array
        (
            [id] => 2
            [soru] => Second Question
            [kategori] => 1
        )

)
Array
(
    [0] => Array
        (
            [id] => 1
            [soru] => First Question
            [kategori] => 1
        )

    [1] => Array
        (
            [id] => 2
            [soru] => Second Question
            [kategori] => 1
        )

    [2] => Array
        (
            [id] => 1
            [cevap] => Answer1
            [sorular] => 1
        )

    [3] => Array
        (
            [id] => 2
            [cevap] => Answer2
            [sorular] => 1
        )

    [4] => Array
        (
            [id] => 1
            [cevap] => Answer1
            [sorular] => 1
        )

    [5] => Array
        (
            [id] => 2
            [cevap] => Answer2
            [sorular] => 1
        )

)

note: these datas are only example. These are data that in mysql.

  • 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-05T00:04:16+00:00Added an answer on June 5, 2026 at 12:04 am

    The easiest way i can think of would be to have two queries:

    1. requesting list of question form database ordered by question_id
    2. requesting lost of all answers for all the question from list 1. ordered by question_id

    Then on the php side of things, you match the answers to each question. It will be a single loop, since both question and answer are already ordered and there is no need to “step back”.

    What you will end up will be a structure like this:

    list = [
        {
           question: "Lorem ipsum",
           answers: [
              "answer number one",
              "answer number two"
           ]
        },
        {
           question: "Cogito ergo sum",
           answers: [
              "answer number one",
              "answer number two"
              "answer number three",
              "answer number four"
           ]
        }
    ]
    

    You should avoid querying DB for answers to each question separately. It’s kinda wasteful.

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

Sidebar

Related Questions

I want to create simple poll system. I have two question and two answers.
I want to create simple login system using the SQLLIte I have created the
I want to create a simple user registration form with First / Last name,
I want to create a simple COM component in VC++ 2008. I have created
I want to create a simple message of Your comment has been posted! using
I want to create simple form using CForm (yii framework) but 'elements' => array('username'=>array('type'=>'text'))
I want to create simple application like android image gallery but I don't want
I want to create simple dummy tag which can work with Struts2. I have
So... I want to create simple HTTP Chunked transfer encoding prototype. I have messages
I want to create simple utility class that has no visual elements and create

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.