Syntax for array?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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
Question two, how to restrict the select statemet only to items in
that array.

Any help greatly appreciated

Garry Jones
Previosly I had something similar but I knew the checkbox values to
check for. Then I laboriously used "post" and checked each value in
turn and selected one by one, but now I dont preknow the checkbox
values to check for as they are dynamicaly created each time a user
loads the form.

Re: Syntax for array?

El 24/09/2010 5:46, GarryJones escribió/wrote:
Quoted text here. Click to load it

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

- 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;

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor satinado:

Site Timeline