complex PHP and im stuck now - expert help needed.

hi all. im currently a student doing a project for my course. ive got to the stage where im very stuck.
im working in PHP with MYSQL database

ive got lots of tables but for this the important ones are items and bookings
items = list of items a customer can book (ITEMS table with a itemID as p key) (and other things)
bookings that got a booking id and a “selecteditems” field which has items in it. (booking id prim key) (and other things)

heres what i want to do…

1… make a booking
booking.php page a loop to show all the possible items.
A check box for each of the items in the items table in mysql
The value of the check box should be the itemID
And for each of the items a customer decides to have s/he will tick all those boxes
ie list all of the items how ever many there are in the table…
[]1 []2 []3 []4 []5 []6

2. store the booking as a string
The value from all the ticked boxes (ie itemID) i want to turn in to a string using implode to have comma between them so i can store the string in in the bookings table as selecteditems … ie if the user ticks 1 3 5 it would store string in booking table selecteditems field as 1,3,5

the last part of what i want to do is read from the bookings table to view or edit
3… on a viewbookings page it will look at the bookings table and look at the item table and display on the page all of the items in the items table as check boxes again similar to what it did in the first place to make a booking…
but this time it should tick the check boxes if it was ticked in the first instance on the bookings page… just so customer can view what they booked
ie list all of the items
[]1 []2 []3 []4 []5 []6
show the ones that were booked
[/]1 []2 [/]3 []4 [/]5 []6

ive got to the point where i can to the first 2 but don’t know about number 3. i don’t think the way i am doing the first 2 is any good either,

if there’s anyone who knows the theory behind what im trying to do please help.
there’s more to it then just what ive put on, just but im just trying to get the basics working first.


You can get the available items with a simple query, and then run through the result set:

$result = mysql_query("SELECT * FROM items"); while ($itemdata = mysql_fetch_assoc($result)) { echo "<tr><td>$itemID</td> <td>$itemname</td> <td><input type='checkbox' name='item$itemID'></td></tr>"; }

When storing the items per booking, comma-separated wouldn’t be my first choice. Instead, try normalizing: you use a separate table ‘itemsinbooking’ for example, which links the itemIDs to the bookingIDs. You can select a booking and view the items belonging to that booking through this table. This will make it easier for you when you, at a later point in time, for example, would like to also store the number of items per booking.

[code]$result = mysql_query("SELECT * FROM booking WHERE bookingID = ".$yourBookingID);
$bookingData = mysql_fetch_assoc($result);
// echo $bookingData;

$result = mysql_query("SELECT itemname FROM items, itemsinbooking WHERE items.itemID == itemsinbooking.bookingID = ".$yourBookingID);
while ($itemdata = mysql_fetch_assoc($result)) {
echo "


This will allow you to only show the items that were previously selected when the booking was made. In order to show all items, and only the selected items as selected, you’re going to have to run the first query I gave you and make a comparison with the last query.

cheers for your help. ive sent a pm to you aswell in thanks. this seems to do most of the trick. a few changes i needed to put in myself but it looks as if its going to help me alot.


Sponsor our Newsletter | Privacy Policy | Terms of Service