"Gilles Ganault" <no****@nospam.comwrote in message
news:q5********************************@4ax.com...
Hello
Out of curiosity, is there a smarter, easier way to read data sent by
a form, and save them into a database? I have about 20 fields, and
it'd be easier if I could just use a loop to go through an array and
generate the SQL query in a couple of lines:
here's one way...certainly not the only way. however, this demonstrates what
you ask...sorry for the text-wrapping:
<?
$date = strtotime('now');
$date = date('m', $date) . '/01/' . date('Y', $date);
$date = $_REQUEST['date'] ? $_REQUEST['date'] : $date;
$date = strtotime($date);
$dealer = 'someFacility';
// define updateable fields and default their values
$financials = array(
'Center' =$dealer
,
'Name' =$dealerName
,
'Month' =date('m', $date)
,
'Year' =date('Y', $date)
,
'pbsMaterialInventory' =0 ,
'bcsAdvertising' =0
);
// used to create dynamic db update statements
function createUpdateStatement(&$value, $key)
{
$value = $key . " = '" . $value . "'";
return $value;
}
// used to format field values
function formatValue(&$value, $key = '', $request = array())
{
if (!is_array($request)){ $request = array($request); }
if (in_array($key, array('Center', 'Name', 'Month', 'Year'))){ return; }
if ($request){ $value = $request[$key]; }
$value = number_format(getInteger($value));
return $value;
}
// whatever $value is, format it as an integer regarless of locale...
// inval may truncate commas...we're just forcing correct truncation
function getInteger(&$value, $key = '', $request = array())
{
if (!is_array($request)){ $request = array($request); }
if (in_array($key, array('Center', 'Name', 'Month', 'Year'))){ return; }
if ($request){ $value = $request[$key]; }
$locale = localeconv();
$decimal = $locale['decimal_point'] . $locale['mon_decimal_point'];
$negative = $locale['negative_sign'];
$value = intval(preg_replace('/[^0-9' . $decimal . $negative . ']/',
'', $value));
return $value;
}
// update $financials with values provided by user submit
array_walk($financials, 'getInteger', $_REQUEST);
// sledge-hammer db update
$sql = "
DELETE
FROM financials
WHERE Center = '" . $dealer . "'
AND STR_TO_DATE(CONCAT(Year, '-', Month, '-01'), '%Y-%m-%d')
=
STR_TO_DATE('" . date('Y-m-d', $date) . "', '%Y-%m-%d')
";
db::execute($sql);
$sql = "
INSERT INTO
financials
(
" . implode(",\r\n ", array_keys($financials)) . "
)
VALUES
(
'" . implode("',\r\n '", $financials) . "'
)
";
db::execute($sql);
// more gentle update where we have determined a record exists alread
array_walk($financials, 'createUpdateStatement');
$sql = "
UPDATE financials
SET
" . implode(",\r\n ", $supplement) . "
WHERE Center = '" . $dealer . "'
";
db::execute($sql);
// now, verify in debug that data made it into the db
// having used either sledge-hammer or gentle upate
$sql = "
SELECT " . implode(",\r\n ",
array_keys($financials)) . "
FROM financials
WHERE Center = '" . $dealer .
"'
AND STR_TO_DATE(CONCAT(Year, '-', Month, '-01'),
'%Y-%m-%d') =
STR_TO_DATE('" . date('Y-m-d', $date) . "',
'%Y-%m-%d')
";
$records = db::execute($sql);
$financials = $records[0];
array_walk($financials, 'formatValue');
echo '<pre style="font:10px;">' . print_r($financials, true) . '</pre>';
?>