Help Optimizing code


Posted on 16th Feb 2014 07:03 pm by admin

Good Morning,

I wrote a small import function for a website of mine and I know there has to be a better way to handle what I'm doing. I'm hoping someone can take my code and make it run a hair faster as it seems to be pretty slow right now. Think anyone can speed this up? What am I doing wrong? or am I doing it right?

The below function takes a file(csv) reads the content and imports it into the database, how can I optimize this to run faster and cleaner?
Code: function importFile($user_id, $file, $file_id)
{
if(!file_exists($file))
{
return false;
}

$handle = fopen($file, "rb");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);

if(empty($contents))
{
return false;
}

// grab the items
$items = explode("n", $contents);

// if there are items import them.
if(is_array($items))
{
foreach($items as $key=>$val)
{
$save_val = $val;
$val = explode(",", trim($val));
if(!is_array($val) || sizeOf($val)<=1)
{
$val = explode("t", trim($save_val));
if(!is_array($val))
{
return false;
}
}

// part number
// alt part number
// condition
// qty
// description

foreach($val as $inner_key=>$inner_val)
{
$inner_val = str_replace("r", "", $inner_val);
$val[$inner_key] = str_replace(""", "", $val[$inner_key]);
$val[$inner_key] = trim(mysql_real_escape_string($val[$inner_key]));
}

if(!empty($val[0]) && strtolower($val[0]) != "part number")
{

$val[0] = $this->fixPartNumber($val[0]);
$val[1] = $this->fixPartNumber($val[1]);
// check to see if we need to insert or update
$select_sql = "SELECT inventory_id FROM ".TABLE_PREFIX."inventory WHERE inventory_part_number='{$val[0]}' AND user_id={$user_id}";
$result = $this->db->query($select_sql);
//echo mysql_error()."<BR><BR>";
if(!$result)
{
echo "fail";
exit;
}
if(mysql_num_rows($result) == 0)
{
// no record, so insert
$insert_sql = "INSERT INTO ".TABLE_PREFIX."inventory (inventory_part_number, inventory_alt_part_number, inventory_condition_code,
inventory_quantity, inventory_description, last_update, user_id) VALUES (
'{$val[0]}', '{$val[1]}', '{$val[2]}', '{$val[3]}', '{$val[4]}', NOW(), '{$user_id}')";
//echo $insert_sql."<BR><BR>";
$result = $this->db->query($insert_sql);
echo "Inserted: ".$val[0]."<br />";
}
else
{
$update_sql = "UPDATE ".TABLE_PREFIX."inventory SET inventory_part_number='{$val[0]}', inventory_alt_part_number='{$val[1]}',
inventory_condition_code='{$val[2]}', inventory_quantity='{$val[3]}', inventory_description='{$val[4]}', last_update = NOW()
WHERE user_id = {$user_id} AND inventory_part_number = '{$val[0]}'";
//echo $update_sql."<BR><BR>";
$result = $this->db->query($update_sql);
echo "Updated: ".$val[0]."<br />";
}
}
}
$update_sql = "UPDATE ".TABLE_PREFIX."file_upload SET file_flag = 1 WHERE file_id=".$file_id." AND user_id=".$user_id;
$result = $this->db->query($update_sql);
return true;
}
}

No comments posted yet

Your Answer:

Login to answer
248 Like 28 Dislike
Previous forums Next forums
Other forums

Add code to enable passing of checkbox variable on login
Ok, I have a login page, but want to add a checkbox that when checked and successful login is made i

Can you help please? Php file
I have to write this php file to allow users to register their nicknames on nickserv

For an I

Need help with simple code, back and forward buttons.
Basically, I have a set of pages in a folder, which have the title somephrasehere_09.php, somephrase

Create a form of 2 numbers input and find the greatest.
Hi, everybody.

I have a homework in my training of php, which ask you to make a form that ask

need help with date function
i have following form for date

Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01

Alternate messaging
I have 4 strings in MySQL db1

$string1 : Hello
$string2 : Hi
$string3 : Great
$strin

Code Help.. If txt input box empty search X instead..
I am having a problem with my search script. At current it will simply search by a selected date whi

Re-Order by ID Number
I have a cms set up for inputting an image, thumb, title, pdf, and eps. When input the database ass

Session problem?!?
Hello All! I am very new to the php world but I am working on fixing things that a previous programm

question about header() security
is is safe to just use the header() function to redirect someone if they are, say, not logged in? or

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash