/    Sign up×
Community /Pin to ProfileBookmark

How to run multiple SELECT on checkbox array posting

Hi Guys…

Here is my quandry..

I am trying to write a simple language translation web page that displays a dynamic checkbox list of words that can be translated into a different language.

eg.

[B]<input type=’checkbox’ name=’phrase[]’ value=’1′> Hello<br></tr><tr>
<input type=’checkbox’ name=’phrase[]’ value=’2′> Yes<br></tr><tr>[/B]

The user checks the words that they would like translated and this is then passed to a select.php script using post.

in my select.php script I run a little bit of test code to make sure I can see data in my array:

[B]$array=$_POST[‘phrase’];
if ($array) {
foreach ($array as $phrases) {echo ‘you selected ‘,$phrases,'<br />’;}[/B]

And this appears to work fine.

Where I am now stumped is how I include this into a mysql_query so that it carries out and displays a select request for each selection.

I have been looking at foreach loops and implode….but I have now just confused myself 🙁

Any help/advice greatfuly received !!!

Cheers

Heave

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@EasyTechOct 27.2009 — First of all, I would use foreach ($array as $phrase) {}

Secondly, do you want a separate query for each phrase? or a single query that will search do a massive search.

If the latter, look into the IN() command for mysql. Use your loop to create a search string, then query "SELECT * FROM Table WHERE phrase IN($in-string)"
Copy linkTweet thisAlerts:
@heaveauthorOct 28.2009 — Hi Easytech

What I am hoping is that the end result will be a single query that will pull out every word translated into the desired language so I can display this back on a web page.

One thing I didnt mention earlier is that on the initial webpage I also have a drop down list of languages. so, the user selects a single language and then multiple words via the checkboxes.

So, the select statement (in theory) would be something like ?? SELECT ${phrase} from PHRASES where LANGUAGE = ${language}

Thanks again
Copy linkTweet thisAlerts:
@EasyTechOct 28.2009 — I would use either:

[code=php]$phr_str = "";
foreach($array as $phrase){
$phr_str .= '$phrase,' // Note the comma
}
$phr_str = mb_substr($phr_str,0,mb_strlen($phr_str));
// ^^ removes last comma

$query = "SELECT $phr_str FROM Phrases";
[/code]


OR, the same as above except
[code=php]$query = "SELECT * FROM Phrases WHERE Phrase IN($phr_str)";[/code]

Each of these will do different things, but I am not 100% certain what you are looking up.

If you are using UTF-8, then you want the mb string functions.

Hope this helps.

--Dave
×

Success!

Help @heave 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.10,
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,
)...