I must need more rest....
How about this one: I have a MYSQL table (ITEM_GRIDS) that holds grid info for items such as size, colors, etc...
The table has 4 basic columns:
and data is stored like this:
item grid_1 grid_2 grid_3
123 small black short sleeve
123 small black long sleeve
123 small white short sleeve
123 small white long sleeve
123 medium black short sleeve
123 medium black long sleeve
Ok, for the PHP part:
I would like to create a page to insert/edit/delete grid info for an item. Since each grid_x column will have multiple entries for 1 item, how can I create a page to show all of them, edit say 1 of them, and then save the changes?
Since we won't know how many entries, we really can't do one text field per column per entry. I was thinking of having a text field for each grid column, having one entry per row (or some other delimitation) and then looping through them to build the grid for insert/update. any ideas???
What you do is create an array like:
Now access your database and store the data in the array.
Next read the array to build your form (note each field will have a name such as 'item$x' where $x is the iterative value from say, a for loop which you would be using to access the array.
Now your function which will be in the form action field will then read all the fields putting them into an array, you are now going in the opposite direction so you want $POST_[$field] where $field will be something like $field = 'item'. $c where $c is again the iterative value.
I have included a function below, which does exactly what you need and i hope will be a good example for you to use.
Summary, this function displays in a table format all the products in a database so the user can amend, add or delete.
The table contains extra blank fields which are used for insertion.
// connect to db
$db = preparedb(ACCOUNTSDB);
$error_msg ="Database error on connection. ".
"Function - products() in page.inc - SqNo:0010";
// INITIAL DATA VALUES ARE SET
// user's typed data.
$products = array();
if ( $_SESSION['cms_name4form'] == 'products' )
$products = $_SESSION['cms_array'];
$itemcount = $_SESSION['cms_itemcount'];
// -- retrieve and store any aliases for this account
$tbl = TBLPROD;
$where = "";
$order = "name";
$result = selectrow ($db,$tbl,"*",$where,$order);
if (!$result || get_rowcnt($result) === false)
$error_msg ="Failure on selectrow of $tbl. ".
"Function - products() in page.inc - SqNo:0030";
echo "<table summary='This is a table of all the current products.'>";
echo "<th scope='col'></th>".
// Function : cms_products.php
// Version : 1.00
// Last Update :
// Latest CCNo :
// Copyright (c) Y2Pods Solutions. All rights reserved.
// -- PRODUCTS MANAGEMENT
// -- This function will add, amend or delete
// -- Anti - hack measures
// First, make sure the form was posted from a browser.
// For basic web-forms, we don't care about anything
// other than requests from a browser:
if (!isset($_SERVER['HTTP_USER_AGENT']) )
$error_msg = "Hack Warning! Form content not from a browser. ".
"Function - cms_products.php - SqNo:0010";
// Make sure the form was indeed POST'ed:
// (requires your html form to use: action="post")
if (!$_SERVER['REQUEST_METHOD'] == "POST")
$error_msg = "Hack Warning! Form method not POST. ".
"Function - cms_products.php - SqNo:0020";
// -- retrieve and validate all data from form
// -- store details in session for on error return
for ($i = 0; $i < $_POST['itemcount']+1; $i++ )
if ( !($products[$i]['name'] == INPDEFAULT || strlen($products[$i]['name']) == 0 )
|| $products[$i]['rec_idx'] != 0 )
// Validation of content
if (!isvalid_inputfield($products[$i]['desc'],'text',255) )
$err_msg = "<p>The description input ".
"at line $i is too long (max. 255) or contains unprintable characters.</p>";
exit(ERRORBEGIN. $err_msg. GOBACK);
// add validation to ensure that domain and sitemap is added for type website ??????????
Like manna from the gods!
I completely had no thoughts at ALL about table-set, and this just got the lightbulbs glowing! I will work with this for a bit and report back my progress.