A text file based database complement.
This class handles standard CSV or TXT text files as they where database
tables. It supports most benefits of both SQL tables and PHP arrays. It
doesn't need a real database management system nor does it require any
knowlege of the SQL language. It hides any filesystem functions so you don't
have to deal with file pointers, field delimiters, escape sequences and so
on. Because it uses the widespreaded standard CSV file format you are able to
create, read and update the tables using any spreadsheet software. It
supports user definied table sort similar to ORDER BY, auto incremented ID
numbers and lots more.
See MyCSV() or dump() for some examples.
Don't hesitate to
report bugs or feature requests.
Variable Summary
data -- Two dimensional associative array containing all the table row data.
delimiter -- Field delimiter.
fields -- Array containing all the table field names.
Constructor Summary
MyCSV -- Reads a CSV file and returns it as a MyCSV object.
Method Summary
add_field -- Adds a new field (column) to the table.
count -- Gets the number of rows in the table.
data -- Gets a table row including their ID number.
data_seek -- Moves the internal row pointer to the specified row number.
delete -- Deletes a table row specified by the id.
drop_field -- Deletes a field/column from the table.
drop_table -- Clears the table.
dump -- Dumps the table to screen.
each -- Gets the current data row and increases the internal pointer.
end -- Sets the internal pointer to the last data row.
exists -- Checks if the CSV file for this table already exists.
export -- Returns a complete CSV dump of the table.
fetch_assoc -- Gets the current data row and increase the internal pointer.
first -- Gets the first ID number from the table.
id_exists -- Checks if the data row specified by the ID exists.
ids -- Gets an array containing all the IDs of the table.
insert -- Inserts a new table row using the next free auto incremented ID number.
insert_id -- Gets the ID generated from the previous insert() call.
is_writeable -- Checks if the CSV file for this table is writeable.
join -- Performs an inner join with another table on a foreign key.
krsort -- Sorts the table rows by ID in reverse order.
ksort -- Sorts the table rows by ID.
last -- Gets the last ID number used in the table.
limit -- Limits the number of rows to be fetched.
max -- Gets the biggest ID number used in the table.
min -- Gets the smallest ID number used in the table.
next -- Gets the next ID number.
num_rows -- Gets the number of rows in the table.
prev -- Gets the previous ID number.
rand -- Picks one or more random ID numbers out of the table.
reset -- Sets the internal pointer to the first data row.
row_exists -- Looks if a data row is already in the table.
seek -- Sets the internal pointer to the data row specified by offset.
sort -- Orders the table rows by one or more columns.
tablename -- Gets the table name without the default ".csv" file extension.
update -- Updates a table row with some new field/value pairs.
write -- Rewrites the CSV table file or creates a new one.
Two dimensional associative array containing all the table row data.
array $data
Field delimiter.
string $delimiter
The field delimiter for separating values in the CSV file. Default is "," (default CSV style). If not, the class tries to use ";" (European/German CSV style), "\t" (tabulator separated values), "\0", "|", "&" (URI encoded/parameter style), ":" (Unix /etc/passwd style) and " " (log file style). Normaly you don't have to touch this variable. Simply choose your delimiter when creating your initial CSV file.
Array containing all the table field names.
string $fields
First have to be "id".
Reads a CSV file and returns it as a MyCSV object.
MyCSV MyCSV ( [ string tablename [, int length]])
Reads a table into a new MyCSV object. The file name may be entered with
or without the .csv
file extension. If the file does not
exist it will be created when calling write(). Set length
to the maximum number of bytes per row you expect (as you did in
fgetcsv()). Default is 10000 bytes per line. Setting this to 1000 may
speed up the method up to 20 times if you'r sure there is no longer line.
For example, create a file called table.csv
with the
following content and call the script below.
id,value 3,Example 4,Another value 7,Blue
<?php require_once("MyCSV.class.php"); $table = new MyCSV("table"); while ($row = $table->each()) { echo $row['id'] . " is " . $row['value'] . "<br>"; } ?>
Adds a new field (column) to the table.
bool add_field ( string field [, string afterField])
Returns false on failure, e.g. if the field already exists.
See also insert(), drop_field().
Gets the number of rows in the table.
int count ( void)
This is an alias for num_rows().
Gets a table row including their ID number.
array data ( mixed id)
Returns false if the row does not exist.
Moves the internal row pointer to the specified row number.
bool data_seek ( int row_number)
This is an alias for seek().
Deletes a table row specified by the id.
void delete ( [ mixed id])
Deletes a table row specified by the id. Deletes all rows if no id is given.
Deletes a field/column from the table.
bool drop_field ( string field)
Clears the table.
void drop_table ( void)
Remove all columns and all fields too.
Dumps the table to screen.
void dump ( void)
Example:
<?php require_once("MyCSV.class.php"); $table = new MyCSV("people"); $table->insert(array('name' => "Adam", 'age' => 23)); $table->insert(array('name' => "Bill", 'age' => 19)); echo "<pre>"; $table->dump(); ?>
See also export().
Gets the current data row and increases the internal pointer.
array each ( void)
See MyCSV() for an example.
Sets the internal pointer to the last data row.
void end ( void)
See also reset().
Checks if the CSV file for this table already exists.
bool exists ( void)
Returns a complete CSV dump of the table.
string export ( void)
Gets the current data row and increase the internal pointer.
array fetch_assoc ( void)
This is an alias for each().
Gets the first ID number from the table.
int first ( void)
This depends on how's the table sorted and isn't identical to min() in any case.
Checks if the data row specified by the ID exists.
bool id_exists ( mixed id)
See also row_exists().
Gets an array containing all the IDs of the table.
array ids ( void)
Inserts a new table row using the next free auto incremented ID number.
void insert ( array data)
Gets the ID generated from the previous insert() call.
int insert_id ( void)
Checks if the CSV file for this table is writeable.
bool is_writeable ( void)
Performs an inner join with another table on a foreign key.
void join ( array rightTable, string foreignKey)
This adds temporary columns to the table.
Sorts the table rows by ID in reverse order.
void krsort ( [ int sort_flags])
This is identical to
sort("id DESC")
but a little bit faster.
Sorts the table rows by ID.
void ksort ( [ int sort_flags])
This is identical to
sort("id")
but a little bit faster.
Gets the last ID number used in the table.
int last ( void)
This depends on how's the table sorted and isn't identical to max() in any case.
Limits the number of rows to be fetched.
void limit ( [ mixed offset [, int rows]])
Use limit(0, 2)
, limit(2)
,
limit("0,2")
or something like that to fetch the first two
rows only when calling each() (or fetch_assoc()). Use
limit(2, 2)
, limit(4, 2)
and so on to fetch the
next two rows. Call limit()
(or limit(0)
or
something like that) to reset the limitation.
Warning! The limitation has no effect on delete(),
update() and so on! Any following method call like sort()
or join() that sets or resets the
internal pointer will change the offset (but not the number of rows) set
by limit().
Gets the biggest ID number used in the table.
int max ( void)
This is often the same as insert_id() which returns the last inserted ID. But unlike that, max() doesn't depend on a previous call of insert().
Gets the smallest ID number used in the table.
int min ( void)
Typically, this is 1.
Gets the next ID number.
int next ( mixed id [, int offset])
Use offset to get another ID near to the row specified by id. Default is 1 (one forward). Returns false if there is no row at this position.
Gets the number of rows in the table.
int num_rows ( void)
See also count().
Gets the previous ID number.
int prev ( mixed id [, int offset])
This is an alias for next() with offset defaults to -1. Returns false if there is no row at this position.
Picks one or more random ID numbers out of the table.
int rand ( [ int num_req])
Sets the internal pointer to the first data row.
void reset ( void)
Looks if a data row is already in the table.
bool row_exists ( array search)
See also id_exists().
Sets the internal pointer to the data row specified by offset.
bool seek ( [ int offset [, int whence]])
Sets the internal pointer to the data row specified by offset.
Whence may be SEEK_SET to set an absolute position (default),
SEEK_CUR for a relative position or SEEK_END for an absolute position
counted from the end of the table. The behaviour of this method is
identical to fseek(). Keep in mind that offset has nothing to do
with the row IDs. Example:
$table = new MyCSV("table"); $table->insert(array('id' => 3)); // Row 0 $table->insert(array('id' => 7)); // Row 1 $table->seek(1); $row = $table->fetch_assoc(); echo $row['id']; // Output: 7
Orders the table rows by one or more columns.
void sort ( mixed sort_flags)
Sorting order flags:
$table->sort("a, b DESC"); $table->sort("a b DESC"); // Same as above $table->sort("a", "b", SORT_DESC); // Same as above $table->sort("a SORT_STRING SORT_NULL b SORT_NULL"); $table->sort("a SORT_NAT, b SORT_NAT, c");
Gets the table name without the default ".csv" file extension.
string tablename ( void)
Updates a table row with some new field/value pairs.
bool update ( array data [, mixed id])
Examples:
$table->update(array(...), 3); $table->update(array('id' => 3, ...)); $table->update(array('id' => 7, ...), 3); // Moves ID 3 to ID 7
Rewrites the CSV table file or creates a new one.
bool write ( [ string tablename [, string delimiter]])
write() closes any file when done.
The files created are binary-safe and compatible with any external spread
sheet software (e.g. Excel) with a few exceptions: