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;
}
}
PHP onsubmit in the form not going to the fuction.
I have a form through ‘onsubmit’ calling a function validation(). But not going to the function at all. Why?<form action="member.php" method="post"
Trigger tag in aspx is not coming
hi all ,iam new to ajax, iam using file upload inside the update pannel but right now i want to use trigger tag but it is not coming, for that whether i want to install anything like toolkit ,please
I need to increse 6hours more, and i don't know how???
I need to increse 6hours more, and i don't know how???<tr> <th>{ov_server_time}</th> <th
IMAGE - SERVER SIDE scripting help...
How to receive pixel data of an image and buid a bitmap file on the server using the HTTP POST method?please help
PHP url branch??
Functionally, using PHP, I'm doing a test of login input submitted by a user via an html form in a file located at http://..../loginregister.php. If that test is successfull, I want to branch to
PHP code needed to get/post form fields and request mysql query results.
I'm new to PHP I've created an html form to allow the user to request a basic telephone directory listing. They can select their 'listtype' by 'all' or by 'state'. If they choose 'state' then they
Match stored value with the current value in a loop
Hello,I have a MySQL db were I store articles in.I have a form to fill these articles, and in that form I can select images who are stored into a directory. Selecting the images works good but the
What am I missing here? Help!
Hello all!. I can't seem to get this working right. Well - it renders right, but something is going wrong. It's a set of filters for events. The filter marked "type" (category) works
Multipe Dynamic Controls & AutoPostback Issue
I currently have a need to create many dynamic controls (Example Textboxes) that need to do a PostBack upon the data in the control changing (Autopostback = true). The following sample code
Refining of search Criteria
Dear FriendsI need a help, I have a search page which of cars related, which has different search criteria e.g. maker, model, color, displacement etc. when i give any search criteria and click the