Firefox 3

pjjTextBase for beginners

This is introductory tutorial, covering the very basics of pjjTextBase's usage. Some basic knowledge of PHP, (x)HTML and CSS will be assumed, though.

Including pjjTextBase library

To make use of pjjTextBase, it must be include()ed in your script (let's call it test.php):

include 'pjjtextbase.php';

If test.php is placed in eg. /test directory, and pjjtextbase.php is placed under /scripts, you should use:

include $_SERVER['DOCUMENT_ROOT'] . '/scripts/pjjtextbase.php';

When pjjtextbase.php has been included, you may use its functions.

If pjjtextbase.php was included successfully, you'll see no confirmation. If pjjtextbase.php wasn't found, a warning will be issued. You won't see it, however, unless you switch on error messages' display, which is highly recommended during development. The best place to switch error messages' display is your local php.ini. If you're unsure how to do it, you may put error_reporting(E_ALL); command into your script. Please remember that in this case it will work only in the script where you put it.

Connecting to a database

To do anything with a database, like selecting all or some records (which is probably the most frequent use of any database), you must first connect to it, ie. read it into server's memory. Let's use sample data file, which I attached to pjjTextBase download package, namely protagonist.csv. Let's also assume it's been placed in /data directory.

$protagonists = ptb_connect('protagonist.csv');

You might get a message like this:

File error (read/write or filesystem) called from
   ptb_connect
in file
   test/test.php
on line no.
   132: $protagonists = ptb_connect('protagonist.csv');
message:
   file test/protagonist.csv does not exist!

You must therefore modify your code, so data file can be seen by the script:

$protagonists = ptb_connect('protagonist.csv', '/data');

Now the $protagonists variable should contain data read from protagonist.csv data file.

How to confirm that the connection was successful?

How could you find out if $protagonists really holds all records, since again no confirmation has been displayed?–The easiest way is to display the number of records in $protagonists array:

echo ptb_count($protagonists);

You should see this:

6

...since there are six records in protagonist.csv data file. If you get 0, you will know that something went wrong (but then again, with error messages turned on, you would already have known it.)

How does my data look like now?

It would certainly help a great deal to work with your database, if you could see how your data look like. Here's how you can do it:

echo '<pre>'; print_r($protagonists); echo '</pre>';

Commands given above will produce:

Array ( [0] => Array ( [id] => 1 [protagonist] => Hercule Poirot ) [1] => Array ( [id] => 2 [protagonist] => Miss Marple ) [2] => Array ( [id] => 3 [protagonist] => Tommy and Tuppence ) [3] => Array ( [id] => 4 [protagonist] => Superintendent Battle ) [4] => Array ( [id] => 5 [protagonist] => Quin and Satterthwaite ) [5] => Array ( [id] => 6 [protagonist] => Parker Pyne ) )

Having visualized the data structure, picking a certain record should be a piece of cake now.

In order to display an array, you should use print_r() command and not echo, for echo would simply display a single word 'Array' instead of the array itself.

Displaying a record

Now you should be ready to display a chosen record. The scheme is simple:

variable name
eg. $protagonists
record index
eg. 0
field name
eg. 'protagonist'

Let's suppose you want to show the 4th record. Since records' index starts from 0, it will be:

echo $protagonists[3]['protagonist'];

It will produce:

Superintendent Battle

To style the output with CSS, you could simply include necessary tags and classes in your code, eg.:

echo '<p class="name">', $protagonists[3]['protagonist'], '</p>';

If something is still unclear, or if you want to see more things covered in this tutorial, why don't you e-mail me at ptb (at) pjj dot pl?

Go back to tutorials' main page.