<< back | print
.CSV based 'database'
This example is using a comma seperated value (short csv) file and two .php files. the empty php file takes the input/content of the text file (.csv) and give us the output with a html-table.
Very similar to txt-database
How does it works?
The txt file (.csv) has a content in following format:
Title , year, genre , director
Black Men Can Swim,2007,Drama,Parthiban Shanmugam
Sasirekha Parinayam,1936,Romance,Chitrapu Narasimha Rao
Krab i Joanna,1981,Drama,Zbigniew Kuzminski
Goryeo jang,1963,Drama,Ki-young Kim
Pyjamas Preferred,1932,Comedy,Val Valentine
Brown Paper Bag,2003,Drama,Michael Baig-Clifford
Angel Blade,2002,Thriller,Masami Obari
The .php pages are looking for a specific string and lists the line(s) which matches the
searchingword.
Example:
The data.php is looking for everything and therefore the searchword is empty $suchwort=" ";
data.php:
$file = "data.csv"; // database file
$suchwort = " "; // searchnig word
$fp = fopen($file,'r+'); // if error, test it without '+'
while(!feof($fp))
{
$zeile = fgets($fp,1024);
if(strstr($zeile, $suchwort)) // looking for $suchwort and show the whole line
echo "$zeile
";
}
the data.php contains a search-input-field as well.
it sends the searchword "$word" to the data-search-output.php:
form action='data-search-out.php' method='post'
Namn/Titel: input type='text' name='word' value='Namn/Titel' INPUT TYPE='submit' name='submit' VALUE='LOOK NOW'
/form
the next file gives us only the data your are lookng for:
data-search-output.php:
$file = "data.csv"; // database file
$suchwort = $word;
// ALTERNATIVE if your server security is to high:
//$suchwort = $_REQUEST["word"]; // searchnig word
$fp = fopen($file,'r+'); // if error, test it without '+'
while(!feof($fp))
{
$zeile = fgets($fp,1024);
if(strstr($zeile, $suchwort)) // looking for $suchwort and show the whole line
echo "$zeile
";
}
Check it out:
data.csv [Database file]
data.php [page loads the DB file]
download the example package:
csv-database.zip [3 files - 4 KB]
Note: the comments of the .php files is in German.