Need help with PHP-MYSQL-Forms and checkboxes (lots of them)

This is a discussion on Need help with PHP-MYSQL-Forms and checkboxes (lots of them) within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Search for variable variables on www.php.net ie <input type = checkbox name=mycheckbox$i> Oli Filth wrote: &...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-18-2004
peter
 
Posts: n/a
Default Re: Need help with PHP-MYSQL-Forms and checkboxes (lots of them)

Search for variable variables on www.php.net

ie
<input type = checkbox name=mycheckbox$i>

Oli Filth wrote:
> Hi,
>
> This may be of some help:
>
> http://www.php.net/manual/en/faq.htm...aq.html.arrays
>
> Oli
>
>
> Pugi! wrote:
>
>> I designed a book database (mysql) and am now making the user
>> interface for it (php).
>> The main table is books and contains quite a lot of information
>> (title, subtitle, isbn, pages, size, ...) but for example not the
>> author, because a book can have more then one author. So I also have a
>> table authors and a table link_books_authors.
>> The same with genres (humour, action, fantasy, history, spy, ....); a
>> book can belong to more then one genre.
>> The list of genres is represented in the input form by a list of
>> checkboxes
>>
>>
>> <table>
>> <tr>
>> <td><input type="checkbox" name="5" value="1"> Action</td>
>> <td><input type="checkbox" name="4" value="1"> Biology</td>
>> <td><input type="checkbox" name="8" value="1"> Chemistry</td>
>> <td><input type="checkbox" name="3" value="1"> Fantasy</td>
>> </tr>
>> <tr>
>> <td><input type="checkbox" name="15" value="1"> Horror</td>
>> <td><input type="checkbox" name="7" value="1"> Science</td>
>> <td><input type="checkbox" name="2" value="1"> Science-Fiction</td>
>> <td><input type="checkbox" name="9" value="1"> Tao</td>
>> </tr>
>> <tr>
>> ...
>> </tr>
>> </table>
>>
>>
>> The list is automatically created, alphabeticaly, with the contents of
>> the table genres and the "name" is the id of the genre in the
>> genres-table.
>> So far so good. Now I must proces the information of the form and
>> insert it in the database. If I hardcode the processing of each genre,
>> still no problem :
>>
>> if (isset($_REQUEST["1"]) && ($_REQUEST["1"] == 1)
>> {
>> $sqlBookGenre = "INSERT INTO link_books_genres VALUES (" .
>> $intBookID . ", " . $_REQUEST["1"] . ")";
>> }
>>
>> execute query and then next checkbox
>>
>> if (isset($_REQUEST["2"]) && ($_REQUEST["2"] == 1)
>> {
>> $sqlBookGenre = "INSERT INTO link_books_genres VALUES (" .
>> $intBookID . ", " . $_REQUEST["2"] . ")";
>> }
>>
>> For the moment I have already 15 genres, but I will create more, so I
>> have to put the above coding in a loop. So again I read the list of
>> genres (at least the id's) and store them in an array $arrGenres.
>> With a while-loop I want to check if a checkbox is checked (isset and
>> value == 1) or not.
>> But how do I do that. How do a obtain the value of $_REQUEST["1"],
>> $_REQUEST["2"], ... when it is in a loop ?
>> $_REQUEST[$arrGenres["id"]] and $_REQUEST["$arrGenres["id"]"] don't
>> work. I also tried to put the values of the checkboxes on the screen
>> with echo and printf, but didn't succeed in it.
>> I suppose an alternative would be a select or listbox and allowing
>> multiple selects but I don't have an example for that either.
>>
>> Thanx for ur time.
>>
>> Pugi!
>>
>> Reply to group
>>

Reply With Quote
  #2 (permalink)  
Old 12-19-2004
Pugi!
 
Posts: n/a
Default Need help with PHP-MYSQL-Forms and checkboxes (lots of them)

I designed a book database (mysql) and am now making the user interface for
it (php).
The main table is books and contains quite a lot of information (title,
subtitle, isbn, pages, size, ...) but for example not the author, because a
book can have more then one author. So I also have a table authors and a
table link_books_authors.
The same with genres (humour, action, fantasy, history, spy, ....); a book
can belong to more then one genre.
The list of genres is represented in the input form by a list of checkboxes


<table>
<tr>
<td><input type="checkbox" name="5" value="1"> Action</td>
<td><input type="checkbox" name="4" value="1"> Biology</td>
<td><input type="checkbox" name="8" value="1"> Chemistry</td>
<td><input type="checkbox" name="3" value="1"> Fantasy</td>
</tr>
<tr>
<td><input type="checkbox" name="15" value="1"> Horror</td>
<td><input type="checkbox" name="7" value="1"> Science</td>
<td><input type="checkbox" name="2" value="1"> Science-Fiction</td>
<td><input type="checkbox" name="9" value="1"> Tao</td>
</tr>
<tr>
...
</tr>
</table>


The list is automatically created, alphabeticaly, with the contents of the
table genres and the "name" is the id of the genre in the genres-table.
So far so good. Now I must proces the information of the form and insert it
in the database. If I hardcode the processing of each genre, still no
problem :

if (isset($_REQUEST["1"]) && ($_REQUEST["1"] == 1)
{
$sqlBookGenre = "INSERT INTO link_books_genres VALUES (" . $intBookID
.. ", " . $_REQUEST["1"] . ")";
}

execute query and then next checkbox

if (isset($_REQUEST["2"]) && ($_REQUEST["2"] == 1)
{
$sqlBookGenre = "INSERT INTO link_books_genres VALUES (" . $intBookID
.. ", " . $_REQUEST["2"] . ")";
}

For the moment I have already 15 genres, but I will create more, so I have
to put the above coding in a loop. So again I read the list of genres (at
least the id's) and store them in an array $arrGenres.
With a while-loop I want to check if a checkbox is checked (isset and value
== 1) or not.
But how do I do that. How do a obtain the value of $_REQUEST["1"],
$_REQUEST["2"], ... when it is in a loop ?
$_REQUEST[$arrGenres["id"]] and $_REQUEST["$arrGenres["id"]"] don't work. I
also tried to put the values of the checkboxes on the screen with echo and
printf, but didn't succeed in it.
I suppose an alternative would be a select or listbox and allowing multiple
selects but I don't have an example for that either.

Thanx for ur time.

Pugi!

Reply to group


Reply With Quote
  #3 (permalink)  
Old 12-19-2004
Oli Filth
 
Posts: n/a
Default Re: Need help with PHP-MYSQL-Forms and checkboxes (lots of them)

Hi,

This may be of some help:

http://www.php.net/manual/en/faq.htm...aq.html.arrays

Oli


Pugi! wrote:
> I designed a book database (mysql) and am now making the user interface for
> it (php).
> The main table is books and contains quite a lot of information (title,
> subtitle, isbn, pages, size, ...) but for example not the author, because a
> book can have more then one author. So I also have a table authors and a
> table link_books_authors.
> The same with genres (humour, action, fantasy, history, spy, ....); a book
> can belong to more then one genre.
> The list of genres is represented in the input form by a list of checkboxes
>
>
> <table>
> <tr>
> <td><input type="checkbox" name="5" value="1"> Action</td>
> <td><input type="checkbox" name="4" value="1"> Biology</td>
> <td><input type="checkbox" name="8" value="1"> Chemistry</td>
> <td><input type="checkbox" name="3" value="1"> Fantasy</td>
> </tr>
> <tr>
> <td><input type="checkbox" name="15" value="1"> Horror</td>
> <td><input type="checkbox" name="7" value="1"> Science</td>
> <td><input type="checkbox" name="2" value="1"> Science-Fiction</td>
> <td><input type="checkbox" name="9" value="1"> Tao</td>
> </tr>
> <tr>
> ...
> </tr>
> </table>
>
>
> The list is automatically created, alphabeticaly, with the contents of the
> table genres and the "name" is the id of the genre in the genres-table.
> So far so good. Now I must proces the information of the form and insert it
> in the database. If I hardcode the processing of each genre, still no
> problem :
>
> if (isset($_REQUEST["1"]) && ($_REQUEST["1"] == 1)
> {
> $sqlBookGenre = "INSERT INTO link_books_genres VALUES (" . $intBookID
> . ", " . $_REQUEST["1"] . ")";
> }
>
> execute query and then next checkbox
>
> if (isset($_REQUEST["2"]) && ($_REQUEST["2"] == 1)
> {
> $sqlBookGenre = "INSERT INTO link_books_genres VALUES (" . $intBookID
> . ", " . $_REQUEST["2"] . ")";
> }
>
> For the moment I have already 15 genres, but I will create more, so I have
> to put the above coding in a loop. So again I read the list of genres (at
> least the id's) and store them in an array $arrGenres.
> With a while-loop I want to check if a checkbox is checked (isset and value
> == 1) or not.
> But how do I do that. How do a obtain the value of $_REQUEST["1"],
> $_REQUEST["2"], ... when it is in a loop ?
> $_REQUEST[$arrGenres["id"]] and $_REQUEST["$arrGenres["id"]"] don't work. I
> also tried to put the values of the checkboxes on the screen with echo and
> printf, but didn't succeed in it.
> I suppose an alternative would be a select or listbox and allowing multiple
> selects but I don't have an example for that either.
>
> Thanx for ur time.
>
> Pugi!
>
> Reply to group
>
>

Reply With Quote
  #4 (permalink)  
Old 12-20-2004
Hilarion
 
Posts: n/a
Default Re: Need help with PHP-MYSQL-Forms and checkboxes (lots of them)

Switch from:

<input type="checkbox" name="5" value="1">

to:

<input type="checkbox" name="genre[5]" value="1">

or

<input type="checkbox" name="genre[]" value="5">


Then you'll get an array $_REQUEST['genre'][...]:
- indexed by genre ID (first case),
- containing genre IDs (second case).


so you can use (code for second case):

foreach( $_REQUEST['genre'] as $genreID )
{
$sqlBookGenre =
'INSERT INTO link_books_genres '.
'VALUES (' . intval( $intBookID ) . ', ' . intval( $genreID ) . ')';
do_something_with_sql_query( $sqlBookGenre );
}


Hilarion


Reply With Quote
  #5 (permalink)  
Old 12-21-2004
Pugi!
 
Posts: n/a
Default Re: Need help with PHP-MYSQL-Forms and checkboxes (lots of them)


"Hilarion" <hilarion@SPAM.op.SMIECI.pl> schreef in bericht
news:cq6djc$qha$1@news.onet.pl...
> Switch from:
>
> <input type="checkbox" name="5" value="1">
>
> to:
>
> <input type="checkbox" name="genre[5]" value="1">
>
> or
>
> <input type="checkbox" name="genre[]" value="5">
>
>
> Then you'll get an array $_REQUEST['genre'][...]:
> - indexed by genre ID (first case),
> - containing genre IDs (second case).
>
>
> so you can use (code for second case):
>
> foreach( $_REQUEST['genre'] as $genreID )
> {
> $sqlBookGenre =
> 'INSERT INTO link_books_genres '.
> 'VALUES (' . intval( $intBookID ) . ', ' . intval( $genreID ) . ')';
> do_something_with_sql_query( $sqlBookGenre );
> }
>
>
> Hilarion
>


Thank u. Works very well.

Pugi!


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:11 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0