27.8. How to enter thousands of ephemerids

Figure 27-7. Administration panel: Ephemerids.

Administration panel: Ephemerids.

Problem: You have a file of nice quotes (often called "fortunes") that you would like to see displayed every day in the Ephemerids module (see Section 6.1 for the user view and Section 7.1 for the administrator view of the Ephemerids module) . You don't want to write all those texts by hand, so you are looking for a way to automate the text insertion.

Idea:The right command to insert a text in the Ephemerids table nuke_ephem is:

insert into nuke_ephem values (4,4,11,2002,
"Nothing cures insomnia like the realization that it's time to get up.",");

We will use a script to produce these INSERT commands (see Inserting Ephemerids through script).

Solution: We write a script like:

#! /bin/sh
did=6
while [ $did -le 30 ]; do
  read LINE
  if [ -n "$LINE" ]; then
    echo "insert into nuke_ephem values (",$did,11,2002,\"$LINE\",");"
    did=$((did+1))
  fi
done

Let's call it "ephemerids" - don't forget to make it executable. When executed, the ephemerids script outputs the MySQL INSERT command we saw above, for each fortune in the standard input. Suppose that the "fortunes" are in the file called "myfortune", one fortune per line. Then, if you use the script as follows:

ephemerids < myfortune > ephemerids.sql

it will create the necessary MySQL statements and put them all in the ephemerids.sql file. Once ephemerids.sql is created, you only need to import it in the PHP-Nuke database. For this, you can type

mysql -u dbuname -h dbhost -p dbname < ephemerids.sql

or use the import function of phpMyAdmin (Section 3.3).

Verification: To show the content of nuke_ephem and verify that the fortunes were inserted, execute the following SQL query:

select eid,did,mid,yid from nuke_ephem;

Note that in this version, the above problem can be easily solved with the Random Quotes block (see Section 8.2.8). However, instead of fortunes, you could just as well have the birthdays of hundreds of people, or the dates and descriptions of thousands of historical events, as in a classic ephemerids context.