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:
Code:
ITEM_NO,GRID_1,GRID_2,GRID_3
and data is stored like this:
Code:
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
etc...
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:
$grid[0]['item']
$grid[0]['grid 1']
$grid[0]['grid 2']
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.
ok,
So I will reference the same text box over and over in the loop; how do I delineate each row/value? \n for enter?
hate to ask so many questions, just can't seem to get my head around this...
Andy
Hi,
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.
Ignore the SESSION bits, that is used to mimic javascript behaviour where it is turned off in a browser.
PHP Code:
function products()
{
echo "<div>\n";
// connect to db
$db = preparedb(ACCOUNTSDB);
if (!$db)
{
$error_msg ="Database error on connection. ".
"Function - products() in page.inc - SqNo:0010";
error_manager(E_USER_ERROR,$error_msg);
die(DIEMESSAGE);
}
// INITIAL DATA VALUES ARE SET
// redisplay of form after error, where javascript disabled will show
// user's typed data.
$products = array();
if ( $_SESSION['cms_name4form'] == 'products' )
{
$products = $_SESSION['cms_array'];
$itemcount = $_SESSION['cms_itemcount'];
}
else
{
// -- 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";
error_manager(E_USER_ERROR,$error_msg);
die(DIEMESSAGE);
}
echo "<table summary='This is a table of all the current products.'>";
echo "<tr>\n";
echo "<th scope='col'></th>".
"<th scope='col'>Type</th>".
"<th scope='col'>Name</th>".
"<th scope='col'>Price</th>".
"<th scope='col'>Description</th>".
"<th scope='col'>Delete</th>";
echo "</tr>\n";
<?php
// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//
// 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
// --
session_start();
require_once ('cms_always.inc');
// -- 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";
error_manager(E_USER_ERROR,$error_msg);
die(DIEMESSAGE);
}
// 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";
error_manager(E_USER_ERROR,$error_msg);
die(DIEMESSAGE);
}
// -- retrieve and validate all data from form
// -- store details in session for on error return
// -- used to redisplay content if javascript disabled
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 ??????????
Ahhhhhh....
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.
Much thanks!
Andy