/    Sign up×
Community /Pin to ProfileBookmark

checkboxes repeating problem

Hi everyone,

I am looping through a recordset ($result) containing different languages which I convert to values of html checkboxes, I want to compare the recordset values to array values ($arr_languages_spoken) and check the checkboxes that match. I think I am close, the code I have checks the right boxes but duplicates every value from the recordset so I have 2 of each checkbox, any help would be appreciated, thanks.

MY CODE

while($row = mysql_fetch_array($result))
{
foreach ($arr_languages_spoken as $value)
{
if ($value == $row[‘language’])
{
echo “<input type=checkbox name=languages_spoken_1 value=”.$row[‘language’].” CHECKED/>”.$row[‘language’].” “;
}
else if ($value != $row[‘language’])
{
echo “<input type=checkbox name=languages_spoken_1 value=”.$row[‘language’].” />”.$row[‘language’].” “;
}
}
}

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@tirnaJan 28.2011 — try this

[code=php]
while ($row = mysql_fetch_array($result)) {
foreach ($arr_languages_spoken as $value) {
$checkedStatus = ($value == $row['language'])? "checked='checked'" : '';
echo "<input type='checkbox' name='languages_spoken_1[]' value=" . $row['language'] . " ".$checkedStatus." />" . $row['language'] . " ";
}
}
[/code]
Copy linkTweet thisAlerts:
@andrew_mauthorJan 30.2011 — H tirna,

thanks for the reply. It is almost working, i dont think i mentioned in my first thread that some people will speak multiple languages. With the code you wrote if a person speaks x languages the checkboxes are repeated x amount of times.

I am going to try and work it out later today. I will let you know how it goes, thanks again for your help.
×

Success!

Help @andrew_m spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...