Firefox 3

Selecting records

So, when you have your database finally designed and data is inserted into appropriate files, you probably want to select some titles and display them. Fine! But before you can do it, you must connect to your database:

$novels = ptb_connect('novels.csv');

Now $novels contains array with all data.

All right then, let's display all novels, in which the principal character is incomparable detective Hercule Poirot! Here we go:

$hercule = ptb_select($novels, "'@protagonist' == '1'");

BTW, although you could write the above line this way:

$hercule = ptb_select($novels, "'protagonist' == 'Hercule Poirot'");

and in fact it would give exactly the same result, however it would also contradict a good rule of programming. What if you later change name from Hercule Poirot to Hercules Poirot? You will have to change your code instead of data. Bad, bad idea.

One record from $hercule looks like this (to display it, I used print_r() PHP function):

  [8] => Array
            [id] => 11
            [title] => Hercule Poirot's Christmas
            [%aka] => 3,4
            [aka] => Array
                    [0] => A Holiday for Murder
                    [1] => Murder for Christmas
            [@protagonist] => 1
            [year] => 1938
            [protagonist] => Hercule Poirot

You should write a template to display the data, so it looks for instance like in example below:

Hercule Poirot series
Hercule Poirot's Christmas
a.k.a. A Holiday for Murder
a.k.a. Murder for Christmas
year of publishing: 1938

All right, that's it for this step. Just one more ptb_select example, so you see how powerful it is:

$hercule = ptb_select($novels, "'@protagonist' == '1' AND 'year' > 1930", 'title ASC');

This line will:

  1. select all the novels in which Hercules Poirot is the principal character AND which were published after 1930
  2. sort them alphabetically, according to their title

Finally, to find all records, where Poirot's surname is a part of the main title, you'd need:

$result = ptb_select($novels, "eregi('Poirot', 'title')");