hi i trying to work on a lunch monthly order form.what i want to do is create a table just like a calendar with 30 cells label 1 to 30 days and in each cell the same food item with checkbox next to it generated from mysql.

the user put their username and checked their desired food in each cell for the 30days and the data is stored in another table in the database

so there will be a table with all food item which will be update and will use these value to generate the checkbox in each date cell on my table

another table to store the food choice of each user

can anyone tell me how i can do this.how do i generate checkbox from the database?

Dani AI

Generated

Good direction in the thread so far β€” asked the right questions, raised the common "implode" approach (avoid it for anything you need to query or update), and @diafol was correct to push a normalized design. Below are practical, modern suggestions to make the calendar/checkbox UX reliable and easy to store.

Keep the UI light: show per-day choices in a popup or via a datepicker rather than rendering 30 full menus at once. Generate checkboxes by querying your foods table for the relevant day (or globally) and output a checkbox whose value is the food id; use a name pattern that groups selections by date, for example food[YYYY-MM-DD][]. Remember unchecked checkboxes are not submitted, so treat missing arrays as "no items selected" server-side. Escape labels with htmlspecialchars before output.

Server-side storage: avoid saving a serialized/imploded string. Create one order record per user+date and separate order_item rows for each selected food. Use transactions and prepared statements (PDO or mysqli) to insert the order and its items atomically, and add a unique constraint on (user_id, for_date) to prevent duplicates. Validate all incoming IDs against the foods table, enforce quantities, and sanitize/verify the date format.

Example flow (conceptual):

  • Fetch menu for the chosen date and render checkboxes named food[YYYY-MM-DD][].
  • On submit, validate user and date, start DB transaction, insert order row, insert orderitem rows for each posted food id, commit.
    Final tips: migrate away from old `mysql
    *calls (use PDO/mysqli), add a CSRF token, index foreign keys for performance, and handle timezones consistently with aDATE/DATETIME` column.

Recommended Answers

All 5 Replies

Even I posted a similar query few days back........ And I'm still stuck with it...

Some people advised me to store the checked value in only one column.... I mean as a string using the implode function.... and while retreiving data use SELECT to recover it and then explode that string and echo it.......

my bigest problem is how should i design my database to hold the name and value of each checkboxitem and echo the name and checkbox into my table.

ok i got this from a search which is supose to generatecheck box from mysql but for some resone im getting an error

<?
include("includes/db.php");

MYSQL_CONNECT(HOST,USER,PASS) OR DIE("Unable to connect to database"); 
@mysql_select_db(DB) or die( "Unable to select database");

//$query=("select * from categories");
$query=("select * from categories order by category, category desc");

$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() ); 

while($row=mysql_fetch_array($result)){ 
$category = @$row["category"];
$cid = @$row["cid"];

echo "<input type=\"checkbox\" name=\"$category\" value=\"$cid\"> $category:";
echo "<br>";
}
?>
Member Avatar for Member #120589

First of all get your db sorted. Will the same foods be available every day or will they rotate? Say bananas every Mon, Tue, Thu and oranges every Wed, Fri?

TABLE: food
food_id (PK, int)
category_id (FK, tinyint) - optional don't know whether you need this
food_label (varchar 30) - checkbox label
price - optional
available (int, 3) - optional - bits of days, e.g. Sun=1,Mon=2,Tue=4,Wed=8 etc, so 11 = Sun,Mon,Wed
(or if req'd you could always set up separate day fields)

OPTIONAL TABLE: category
category_id (PK,tinyint)
category_label (varchar, 20)
category_order (tinyint, 2) - optional - for ordering display purposes only

TABLE: users
user_id (PK, int)
username (etc all the usual user table stuff)

TABLE: orders
order_id (PK, int)
user_id (FK, int)
for_date (int) - unix timestamp OR for_date (date) - unix date
ordered_on (int) or (date)
(you could have optional cost, tax, total etc here if required - or even a dedicated set of tables for pricing)

TABLE: orderitems
order_id (FK,int)
food_id (FK,int)
quantity (tinyint, 2)

OPTIONAL TABLE: closeddates
closed_id (PK, id)
from_date (int or date)
to_date (int or date)

OK, once data is sorted, you can start designing forms around it.
Your calendar idea sounds fine in theory, but it could become a monster if you have 28-31 x whole menus on display at any given time.
An easier solution may be to have a popup menu or better still a div area displaying a form which changes as you select/click a date from say a datepicker or html table calendar.

You could have just a simple vanilla send form to server / follow calendar link to reload page every time OR
you can use js/ajax to easy some of the server load / user wait time. jQuery would be perfect for this.

Anyway, few ideas.

As for creating a checkbox list:

$ordered_array = array(); //just temporary to avoid error on checking
$checks = ''; //assume all orders for the day are in $ordered_array
while($data =  mysql_fetch_assoc($result)){
    $checked = (in_array($data['food_id'],$ordered_array)) ? ' checked="checked"' : '';
    $checks .= "\n<input type=\"checkbox\" id = \"food_{$data['food_id']}\" name=\"food[{$data['food_id']}]\"$checked /> <label for=\"food_{$data['food_id']}\">{$data['food_label']}</label>";
}
//then at the appropriate place:
echo $checks;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.