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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:54:17+00:00 2026-05-23T02:54:17+00:00

All right, so I am being passed an array of checked items represented by

  • 0

All right, so I am being passed an array of checked items represented by their IDs through a $_GET request. For example, suppose the following form:

Question 1
[ ] Answer 1 [31]
[ ] Answer 2 [32]
[ ] Answer 3 [33]
[ ] Answer 4 [34]

Let’s assume the user chose Answer 2 and Answer 4, so the array that is passed contains 32 and 34, respectively, symbolizing their IDs in the database. Now, I want to loop through a database query of ALL of the answers for this question and print back checkboxes. However, if an answer is the one they picked, I’d like to have the checkbox already checked, so that, by my example, the output would be:

Question 1
[ ] Answer 1 [31]
[x] Answer 2 [32]
[ ] Answer 3 [33]
[x] Answer 4 [34]

I guess I’m just a bit off on my looping structure… but here is what I started with:

// The array that gets passed
$curr_answer_id = $_GET['answer'];

$length = count($curr_answer_id);

while($row = mysql_fetch_assoc($fetch2)){
    for($j = 0; $j < $length; $j++){
        if($row['answer_id'] == $curr_answer_id[$j]){
            echo "<input type='checkbox' value='$curr_answer_id[$j]' checked>".$row['answer']."</input>";
        } else {
            echo "<input type='checkbox' value='$curr_answer_id[$j]'>".$row['answer']."</input>";
        }
    }
        echo "<br \>";
}

However, this duplicates results for as many answers were checked. Aside from the duplicates, however, it functions fine. As in my previous example, the output is:

Question 1
[ ] Answer 1 [31]
[ ] Answer 1 [31]
[x] Answer 2 [32]
[ ] Answer 2 [32]
[ ] Answer 3 [33]
[ ] Answer 3 [33]
[ ] Answer 4 [34]
[x] Answer 4 [34]

How can I fix this using what I’ve got? If anyone can also lend me to a more efficient way to cycle through all of the data that would be great as well.

Thanks.

  • 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-23T02:54:18+00:00Added an answer on May 23, 2026 at 2:54 am

    Maybe that is the right solution for you?

    $curr_answer_id = $_GET['answer'];
    
    while($row = mysql_fetch_assoc($fetch2))
    {
        echo "<input type='checkbox' value='".$row['answer_id']."' ".(in_array($row['answer_id'], $curr_answer_id) ? " checked" : "").">".$row['answer']."</input>";
        echo "<br \>";
    }
    

    Not tested.

    Update: replaced input value with $row['answer_id'] instead of $curr_answer_id[$j]

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

Sidebar

Related Questions

In Enterprise Manager you could script all SPs in a database through the right
All the data is being passed as I have sufficiently tested over and over
The idea is to move all of the right elements into the left and
The WebBrowser control has a property called IsWebBrowserContextMenuEnabled that disables all ability to right-click
All members are camel case, right? Why True/False but not true/false, which is more
Right now, I keep all of my projects on my laptop. I'm thinking that
The Form variable in my form is being passed/retrieved via Session. I have a
Right now, I use a combination of Python and R for all of my
I have a form being validated in the following manner: //Clear all variables $formCheck
I am trying to load a html page through UIWebview.I need to disable all

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.