Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageHi, This may be of some help: http://www.php.net/manual/en/faq.ht...faq.html.arrays Oli Pugi! wrote: > I designed a book database (mysql) and am now making the user interface fo r > 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 checkboxe s > > > <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 i t > 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 (" . $intBookI D > . ", " . $_REQUEST["1"] . ")"; > } > > execute query and then next checkbox > > if (isset($_REQUEST["2"]) && ($_REQUEST["2"] == 1) > { > $sqlBookGenre = "INSERT INTO link_books_genres VALUES (" . $intBookI D > . ", " . $_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 valu e > == 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 multipl e > selects but I don't have an example for that either. > > Thanx for ur time. > > Pugi! > > Reply to group > >
Post Follow-up to this messageSearch 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.ht...faq.html.arrays > > Oli > > > Pugi! wrote: >
Post Follow-up to this messageSwitch 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
Post Follow-up to this messageSearch 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.ht...faq.html.arrays > > Oli > > > Pugi! wrote: >
Post Follow-up to this message
"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!
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.