/    Sign up×
Community /Pin to ProfileBookmark

Optimization of database script

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”);[/code]

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

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 24.2005 — I feel there's probably a simpler, cleaner way of doing whatever it is you're doing, but it's hard to say without seeing the input form, some understanding of the database table involved, and at least a basic idea of what the overall functionality is supposed to be.

My first suggestion, however, is to add some comments so that when you (or someone else?) comes back to this code in 6 months to make some changes (bug fix, feature enhancement, or whatever) you'll have some idea of what all that array slicing and dicing is for.

My suspicion -- without enough background info to be sure -- is that a little creative use of the convention of using array names within the HTML form could save some of the backflips you're currently performing in the code. As a generic example:
[code=html]
<form action="something.php" method="post">
<p>
Item 1: <input name="item[]" type="text">
Price: <input name="price[]" type="text">
</p>
<p>
Item 1: <input name="item[]" type="text">
Price: <input name="price[]" type="text">
</p>
<p>
Item 1: <input name="item[]" type="text">
Price: <input name="price[]" type="text">
</p>
<p><input type="submit" name="submit" value="Submit Request"></p>
</form>
[/code]

The form-handler PHP script would then have:
<i>
</i>$_POST['item'][0]
$_POST['item'][1]
$_POST['item'][2]
$_POST['price'][0]
$_POST['price'][1]
$_POST['price'][2]

You could then just walk through these with foreach's, like:
[code=php]
foreach($_POST['item'] as $key => $value)
{
echo "</p>$value costs ${$_POST['price'][$key]}.</p>n";
}
[/code]
×

Success!

Help @Wedvich spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...