Hello,
I’m working on an item database system. I have a form which returns a number of values, and then send them to this script for processing:
[code=php]$postnames = array_keys($_POST);
$p = 0;
foreach($_POST as $value) {
$p++;
$postvalues[$p] = $value;
}
$postnames = array_slice($postnames, 0, count($postnames) – 6);
$price = array_slice($postvalues, -6, 6);
for($p = 0; $p <= 5; $p++) { if(empty($price[$p])) $price[$p] = 0; }
$buyprice = implode(‘|’, array_slice($price, 0, 3));
$sellprice = implode(‘|’, array_slice($price, 3, 3));
$postvalues = array_slice($postvalues, 0, count($postvalues) – 6);
$buildnames = $postnames[0];
$buildvalues = ‘”‘ . $postvalues[0] . ‘”‘;
for($p = 1; $p <= count($postnames) – 7; $p++) {
if(!empty($postvalues[$p])) {
$buildnames .= ‘, ‘ . $postnames[$p];
$buildvalues .= ‘, “‘ . $postvalues[$p] . ‘”‘;
}
}
$buildnames .= ‘, item_buy, item_sell’;
$buildvalues .= ‘, “‘ . $buyprice . ‘”, “‘ . $sellprice . ‘”‘;
$sql = @mysql_query(‘INSERT INTO items(‘ . $buildnames . ‘) VALUES(‘ . $buildvalues . ‘);’);
if(!$sql) die(“<br><br>INTERNAL ERROR”);
The script works, but as I’m fairly new to PHP I’m terrible at optimizing code. So, if anyone could help me optimize this chunk, I’d appreciate it.
Thanks