FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Syntax for array?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Syntax for array? [message #169726 is a reply to message #169723] Fri, 24 September 2010 07:59 Go to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma:
Senior Member
El 24/09/2010 5:46, GarryJones escribió/wrote:
> I am trying to use an array but cant get the syntax right.
>
> I have a list of items all with an id.
>
> In a form the user clicks a check box for the items he wants to see.
>
> After the user has chosen his items I need to place the checkbox
> values that are clicked into an array. I am unsure how to do this. All
> the checkbox values have the same name but unique values.
>
> Then I need to use a select statement to access the items that are in
> the array.
>
> SELECT * FROM mylist WHERE "syntax for itemid is in the array" ORDER
> BY itemid
>
> So simply put,
> Question one, how to place chosen unknown checkbox values into an
> array
> Question two, how to restrict the select statemet only to items in
> that array.
>
> Any help greatly appreciated

You've made a question where you happily mix PHP, HTML, SQL and
JavaScript. I think the main problem is that you don't have a clear view
of the different languages involved. There isn't a single technology
called "web". Before you get proficient in the tools you are using you
need to identify them :)

To sum up:

- The ID attribute is completely irrelevant for PHP. It's never sent to
the server when you submit the form. Forget about it.

- HTML forms allow to define many fields with the same name. You can use
this to emulate arrays. However, PHP has a known limitation: it won't
retrieve all the values when field names are the same. Happily, there's
a workaround: name the fields with traling square brackets, e.g.

<input type="checkbox" name="preferences[]" value="a">
<input type="checkbox" name="preferences[]" value="b">
<input type="checkbox" name="preferences[]" value="c">

... or

<input type="checkbox" name="preferences[a]" value="yes">
<input type="checkbox" name="preferences[b]" value="yes">
<input type="checkbox" name="preferences[c]" value="yes">

This way, PHP will automagically put them into an array called
$_POST['preferences']

- No SQL dialect I'm aware of supports the concept of "array". The
closest you have is the IN() clause:

SELECT * FROM mylist
WHERE itemid IN ('a', 'b', 'c')
ORDER BY itemid

However, a SQL query is just a string. That means that you can use any
of the PHP string functions to build it. For instance, you could do this:

$items = array();
foreach($_GET['preferences'] as $item_id){
// Assuming MySQL:
$items[] = "'" . mysql_real_escape_string($item_id) . "'";
}
$sql = 'SELECT * FROM mylist
WHERE itemid IN (' . implode(', ', $items) . ')
ORDER BY itemid';

The SQL server will receive the query contained in the $sql variable.
You can see it if you do this:

echo $sql;



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
[Message index]
 
Read Message
Read Message
Previous Topic: I need Help
Next Topic: Tokan error & 401 fatal error while running google analytics API code on dedicated server
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 22:45:54 GMT 2024

Total time taken to generate the page: 0.04601 seconds