Databases
|
|
|
|
<?php
/* loadtodb.php
Class to load n/a separated files into a database
(c) Andreas Bernhardsen
scha_8@yahoo.com
*/
/* How to use it
*
* The first thing you need to do is to load the class with the constructor
*
* $load = new LoadToDB('members', 'myFile.txt', 1 [, ',']);
*
* Where 'members' is the name of the table to input into and 'myFile.txt'
* is the file containing the data to be inputed. third argument (0)
* spesifies if the first line of the file is a heading file.
* if you spesifie this the first line of the file will not be inserted
*
* The last argument is optional, if you dont pass anything it will assume
* that the file is comma (,) separated. you can pass other things like
* "t" (tab)
*
* after you have constructed the class you need to specify the fields you
* want inputed. This you need to pass as an array
*
* example: here is a file
*
* NAME, ADDRESS, PHONE, FAX, CELLPHONE, MEMBER NR
* Andreas Bernhardsen, none, 11111111, 11111112, 90909090, 1
* John Doe, Rd drive 1, 22222222, 22222223, 91919191, 2
* Jane Doe, Rd drive 2, 33333333, 33333334, 92929292, 3
*
* Lets say my members table contain 3 fields:
* name, phone, member_nr
* I dont really need address, fax and cellphone information
* so you say:
*
* $load->SetFields(array(0 => 'name', 2 => 'phone', 5 => 'member_nr'));
*
* Can you see where I'm heading at? The number you put it into secifies how
* commas (,) befor the data.
*
* after you have done that you only need to call:
*
* $sucess = $load->LoadIntoDB();
*
* this will return 1 on success and 0 on failure
* be sure you have connected to the mysql database befor executing this
* last function.
*
* send me e-mail at scha_8@yahoo.com with bugs or sugestions
*
*/
class LoadToDB {
/* Declare class variables */
var $dbname;
var $dbfields;
var $dbfile;
var $field_num;
var $a_keys;
var $separator;
var $head;
/* The constructor */
function LoadToDB($name, $file, $head, $separator = ',')
{
if($head)
$this->head = 1; /* if head is set */
$this->separator = $separator; /* define separator */
$this->dbname = $name; /* wich table do we wanna insert into */
$this->dbfile = file("$file"); /* load the file into an array */
}
function SetFields($fields)
{
$this->dbfields = $fields; /* copy */
$this->field_num = count($this->dbfields); /* count */
$this->a_keys = array_keys($this->dbfields); /* find keys */
}
function LoadIntoDB()
{
$rows = count($this->dbfile);
/* make the sting that is identical to all querys */
$start_query = "INSERT INTO ".$this->dbname." (";
for($i = 0; $i < $this->field_num; $i++)
{
if($i != 0)
$start_query .= ", ";
$start_query .= $this->dbfields[$this->a_keys[$i]];
}
$start_query .= ") VALUES (";
/* Loop through all entrys and insert them */
for($i = $this->head; $i < $rows;$i++)
{
$query = $start_query;
$this->dbfile[$i] = addslashes($this->dbfile[$i]);
$n_row = explode($this->separator, $this->dbfile[$i]);
for($j = 0; $j < $this->field_num; $j++)
{
if($j != 0)
$query .= ", ";
$query .= "'".$n_row[$this->a_keys[$j]]."'";
}
$query .= ")";
mysql_query($query);
if(mysql_affected_rows() < 1)
return 0; // on failure
}
return 1; // on success
}
}
?>
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|