Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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



Report this thread to moderator Post Follow-up to this message
Old Post
Pugi!
12-19-04 08:56 PM


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

Report this thread to moderator Post Follow-up to this message
Old Post
Oli Filth
12-20-04 01:55 AM


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.ht...faq.html.arrays
>
> Oli
>
>
> Pugi! wrote:
> 

Report this thread to moderator Post Follow-up to this message
Old Post
peter
12-20-04 01:55 AM


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



Report this thread to moderator Post Follow-up to this message
Old Post
Hilarion
12-20-04 01:57 PM


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.ht...faq.html.arrays
>
> Oli
>
>
> Pugi! wrote:
> 

Report this thread to moderator Post Follow-up to this message
Old Post
peter
12-21-04 08:57 PM


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!



Report this thread to moderator Post Follow-up to this message
Old Post
Pugi!
12-21-04 08:57 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:59 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.