Halloween CSS tricks

Here is a treat for you:

Happy
Halloween

 

Here’s how it is done.

First, create a div of equal width and height.

<div
style="width: 200px; height: 200px;" >
</div>

Then add rounded corners of half the width:

<div
style="width: 200px; height: 200px;
-moz-border-radius: 100px; 
-webkit-border-radius: 100px; 
border-radius: 100px; " >
</div>

Now you have a round div. The property -moz-border-radius is for Firefox, -webkit-border-radius is for Chrome and Safari, and border-radius is the actual CSS3 property. IE does not support this property, so if you have been wondering what the treat was, look at this post in Firefox, Chrome, or Safari.

Next, supply a solid black border of 1px.

<div
style="width: 200px; height: 200px;
-moz-border-radius: 100px; 
-webkit-border-radius: 100px; 
border-radius: 100px; 
border: 1px solid black;" >
</div>

You now have a round div with a border. How about the color? That’s a radial gradient.

<div style="width: 200px; height: 200px; 
-moz-border-radius: 100px; 
-webkit-border-radius: 100px; 
border-radius: 100px; 
border: 1px solid black;
background-image: -ms-radial-gradient(center, circle contain, 
#FECCB1 0%, #EA5507 100%); 
background-image: -moz-radial-gradient(center, circle contain, 
#FECCB1 0%, #EA5507 100%); 
background-image: -o-radial-gradient(center, circle contain, 
#FECCB1 0%, #EA5507 100%); 
background-image: -webkit-gradient(radial, center center, 
0, center center, 143, color-stop(0, #FECCB1), 
color-stop(1, #EA5507)); 
background-image: -webkit-radial-gradient(center, circle contain, 
#FECCB1 0%, #EA5507 100%); 
background-image: radial-gradient(center, 
circle contain, #FECCB1 0%, #EA5507 100%);">

All those different radial-gradient variations support different browsers. Won’t it be nice when everyone supports CSS3?

As for the text, that is done with the google font ‘Mountains of Christmas’. I added the line:

@import 
url(http://fonts.googleapis.com/css?family=Mountains+of+Christmas);

to my child theme’s style sheet, so I can now use the font. The text is in it’s own div, like so:

<div style="font-family: 'Mountains of Christmas', cursive; 
font-size: 3em; text-align: center; padding-top: 20px;">
Happy <br />
Halloween</div>

And this div is nestled inside the previous div.

Here is an image for those of you who might be stuck using a non-compliant browser.

image of css example for non-compliant browsers

And one further note. If you want to use this trick within a wordpress blog post, DO NOT use the Visual editor. It will lose half the css. Stick with the HTML editor—it is much better.

Converting static pages to dynamic pages using PHP and MySQL, Part 4

The challenge:
Take Adam Walker’s Carrajina dictionary (http://carrajina.conlang.org/dicthome.html) and stick it in a database so that Adam can add an entry whenever he wants without re-writing an html page and possibly messing up his html. One stricture is that Adam does not want to learn too much new stuff – no SQL, no PHP, just HTML. And if he can enter SAMPA and have it turn into IPA, that’s a plus!

Step 1 (data prep) is here, Step 2 (create database and load data) is here, and Step 3 (generating listings) is here.

Here is the html for adding a new English entry.

<h2>Add New Entry</h2>

<form action="edit.php" method="post">
<p>
Word  
<input id="term" type="text" value="" name="term" />
  Sampa or IPA
<input id="pron" class="ipa" type="text" value="" name="pron" />
<span class="replace">click for ipa</span>
</p>

<div class="animate">
<p class="toggleable">New Definition +</p>
<div class="newitem" style="display: none;">
<p>
Def #
<input class="num" type="text" name="def[1][defno]" />
  POS
<input class="dp" type="text" name="def[1][pos]" />
  Definition Text
<textarea name="def[1][deftext]" cols="48" rows="3"></textarea>
</p>
</div>
</div>

<div class="animate">
<p class="toggleable">New Subentry +</p>
<div class="newitem" style="display: none;">
<p>
Subentry
<input type="text" name="subentry[1][sterm]" />
  Sampa or IPA
<input class="ipa" type="text" name="subentry[1][spron]">
<span class="replace">click for ipa</span>
</p>
<p>
Def #
<input class="num" type="text" name="subentry[1][sdef][1][defno]">
  POS
<input class="dp" type="text" name="subentry[1][sdef][1][pos]">
  Definition Text
<textarea name="subentry[1][sdef][1][deftext]"
     cols="48" rows="3"></textarea>
</p>
</div>
</div>

<p>
<input type="hidden" value="add" name="task">
<input type="hidden" value="eng" name="lang">
<input type="hidden" value="" name="entryID">
<input type="submit" value="Submit!">
</p>

</form>

This uses some Javascript (or rather jQuery), stored at the bottom of the page:

<script type="text/javascript">
//hides and shows new item sections
$(document).ready(function () {
	$('.newitem').hide();

	$('.animate').children('.toggleable').click(function () {
		$(this).parent().children('.newitem').toggle();
	});
});

//click for ipa
$('.replace').click(function () {
	var inputtext = $(this).prev('.ipa');
	var str = inputtext.val();
	str = str.replace(/a/g, 'ä')
				 .replace(/E/g,'ɛ')
				 .replace(/I/g, 'ɪ')
				 .replace(/J/g, 'ɲ')
				 .replace(/N/g, 'ŋ')
				 .replace(/O/g, 'ɔ')
				 .replace(/U/g, 'ʊ')
				 .replace(/S/g, 'ʃ')
				 .replace(/T/g, 'θ')
				 .replace(/U/g, 'ʊ')
				 .replace(/V/g, 'ʌ')
				 .replace(/W/g, 'ʍ')
				 .replace(/Z/g, 'ʒ')
				 .replace(/3/g, 'ɜ')
				 .replace(/4/g, 'ɾ')
				 .replace(/@/g, 'ə')
				 .replace(/&/g, 'æ')
				 .replace(/,/g, 'ˌ')
				 .replace(/\?/g, 'ʔ');
	var language = "<?php echo $lang; ?>";
	if (language == "eng") {
		str = str
				 .replace(/m=/g, 'm̩')
				 .replace(/n=/g, 'n̩')
				 .replace(/l=/g, 'l̩')
				 .replace(/r/g, 'ɹ');
	}
	if (language == "car") {
		str = str.replace(/d/g, 'd̪')
				 .replace(/d̪ʒ/g, 'd͡ʒ')
				 .replace(/t/g, 't̪')
				 .replace(/t̪ʃ/g, 't͡ʃ')
			     .replace(/n/g, 'n̪');
	}
	$(this).prev('.ipa').val(str);
});
</script>

The click for ipa code is simply a series of global replace functions.

Processing this form works like so:

public function addEntry($mysqli, $entry) {
   $hentryID = null;
   $term = $this->process($entry['term']);
   $pron = $this->process($entry['pron']);

   $query = "INSERT INTO english_entry (english_term,
      english_pronunciation, head_entryID) VALUES (?,?,?)";

   $stmt = $mysqli->stmt_init();
   if ($stmt = $mysqli->prepare($query)) {
      $stmt->bind_param("ssi", $term, $pron, $hentryID);
      $stmt->execute();
      $stmt->close();
   }

   $entryid = $mysqli->insert_id;
   $this->addDefs($mysqli, $entry['def'][1], $entryid);

   if (isset($entry['subentry'])) {
      $this->addSubentry($mysqli, $entry['subentry'][1], $entryid);
      $subid = $mysqli->insert_id;
      $this->addDefs($mysqli, $entry['subentry'][1]['sdef'][1],
         $subid);
   }

   return $entryid;
}

This function calls the process() and addDefs() functions and sometimes the addSubentry() function. process() is part of the parent class, PageController.

class PageController {

private $fake;
private $real;

function __construct() {
   $this->fake = array('[i]', '[/i]', '[b]', '[/b]',
      '[sup]', '[/sup]');
   $this->real = array('<i>', '</i>', '<b>', '</b>',
      '<sup>', '</sup>');
}

public function makeReal($str) {
   $str = str_replace($this->fake, $this->real, $str);
   return $str;
}

public function makeFake($str) {
   $str = str_replace($this->real, $this->fake, $str);
   return $str;
}

public function sanitize($str) {
   $str = trim($str);
   $str = strip_tags($str);
   $str = htmlspecialchars($str, ENT_QUOTES, "UTF-8");
   return $str;
}

public function process($str) {
   $str = $this->makeFake($str);
   $str = $this->sanitize($str);
   $str = $this->makeReal($str);
   return $str;
}

}

So process() changes any allowed html tags into [tag] and [/tag], then trims the string, and strips all quotes and special characters, and then changes [tag] and [/tag] into <tag> and </tag>.

Here is addDefs():

private function addDefs($mysqli, $def, $id) {
   $defno = $this->sanitize($def['defno']);
   $pos = $this->process($def['pos']);
   $deftext = $this->process($def['deftext']);

   $query = "INSERT INTO english_definition (english_entryID,
      definition_number, part_of_speech, definition)
      VALUES (?,?,?,?)";

   $stmt = $mysqli->stmt_init();
   if ($stmt = $mysqli->prepare($query)) {
      $stmt->bind_param("iiss", $id, $defno, $pos, $deftext);
      $stmt->execute();
      $stmt->close();
   }
   $defid = $mysqli->insert_id;

   return $defid;
}

Note that we don’t actually use the returned value, but it is useful to have for debugging purposes. Likewise with addSubentry().

private function addSubentry($mysqli, $sub, $id) {
   $term = $this->process($sub['sterm']);
   $pron = $this->process($sub['spron']);

   $query = "INSERT INTO english_entry (english_term,
      english_pronunciation, head_entryID)
      VALUES (?,?,?)";

   $stmt = $mysqli->stmt_init();
   if ($stmt = $mysqli->prepare($query)) {
      $stmt->bind_param("ssi", $term, $pron, $id);
      $stmt->execute();
      $stmt->close();
   }
   $subid = $mysqli->insert_id;

   return $subid;
}

Adding an entry to the Carrajina side is similar. The html form is different, of course, and the code is somewhat different, too. Here is addEntry():

public function addEntry($mysqli, $entry) {
   $term = $this->process($entry['term']);
   $pron = $this->process($entry['pron']);
   $etym = $this->process($entry['etymology']);
   $csort = $this->getSort($term);

   $query = "INSERT INTO carrajina_entry (carrajina_sort,
      carrajina_term, carrajina_pronunciation, etymology)
      VALUES (?,?,?,?)";

   $stmt = $mysqli->stmt_init();
   if ($stmt = $mysqli->prepare($query)) {
      $stmt->bind_param("ssss", $csort, $term, $pron, $etym);
      $stmt->execute();
      $stmt->close();
   }
   $entryid = $mysqli->insert_id;

   //add defs
   $this->addDefs($mysqli, $entry['def'][1], $entryid);

   //add idioms, if
   if (isset($entry['idioms'])) {
      $this->addIdioms($mysqli, $entry['idioms'][1], $entryid);
   }

   //add note, if
   if (isset($entry['note'])) {
      $this->addNote($mysqli, $entry['note'], $entryid);
   }

   return $entryid;
}

carrajina_sort is a column added to the carrajina_entry table to enable a custom sort value. getSort() works like so:

private function getSort($value) {
   $chararray = array('<sup>', '</sup>', 'ch', 'dj', 'rr', 'ñ',
      'Ch', 'Dj', 'Rr', 'Ñ', 'ó',
      'a', 'b', 'c', 'd', 'e', 'f',
      'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
      'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
      'A', 'B', 'C', 'D', 'E', 'F',
      'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
      'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
      '-', "'", ' ', '...');
   $numberarray = array('000', '', '13', '15', '31', '26',
      '13', '15', '31', '26', '27',
      '10', '11', '12', '14', '16', '17',
      '18', '19', '20', '21', '22', '23', '24', '25', '27', '28',
      '29', '30', '32', '33', '34', '35', '36', '37', '38', '39',
      '10', '11', '12', '14', '16', '17',
      '18', '19', '20', '21', '22', '23', '24', '25', '27', '28',
      '29', '30', '32', '33', '34', '35', '36', '37', '38', '39',
      '', '', '00', '00');
   $value = str_replace($chararray, $numberarray, $value);
   return $value;
}

This converts a Carrajina word into a string of numbers that correspond to the Carrajina sort order.

Carrajina’s addDefs():

private function addDefs($mysqli, $def, $id) {
   $defno = $this->sanitize($def['defno']);
   $pos = $this->process($def['pos']);
   $deftext = $this->process($def['deftext']);

   $query = "INSERT INTO carrajina_definition (carrajina_entryID,
      definition_number, part_of_speech, definition)
      VALUES (?,?,?,?)";

   $stmt = $mysqli->stmt_init();
   if ($stmt = $mysqli->prepare($query)) {
      $stmt->bind_param("iiss", $id, $defno, $pos, $deftext);
      $stmt->execute();
      $stmt->close();
   }
}

addIdioms():

private function addIdioms($mysqli, $idiom, $id) {
   $iterm = $this->process($idiom['idiom']);
   $idef = $this->process($idiom['idiomdef']);
   $csort = $this->getSort($iterm);

   $query = "INSERT INTO carrajina_idiom (carrajina_entryID,
      carrajina_sort, carrajina_idiom, carrajina_idiom_definition)
      VALUES (?,?,?,?)";

   $stmt = $mysqli->stmt_init();
   if ($stmt = $mysqli->prepare($query)) {
      $stmt->bind_param("iiss", $id, $csort, $iterm, $idef);
      $stmt->execute();
      $stmt->close();
   }
}

addNote():

private function addNote($mysqli, $entry, $id) {
   $note = $this->process($entry['note']);
   $query = "INSERT INTO carrajina_note (note, carrajina_entryID)
      VALUES (?,?)";

   $stmt = $mysqli->stmt_init();
   if ($stmt = $mysqli->prepare($query)) {
      $stmt->bind_param("si", $note, $id);
      $stmt->execute();
      $stmt->close();
   }
}

And that’s enough for today. I’ll do updating an entry in Part 5.

Character sets, collations, and sort orders, oh my!

Let’s start with some definitions. Sort order is easy enough – that’s the order things are supposed to go in. In English, that happens to be:

a b c d e f g h i j k l m n o p q r s t u v w x y z

In Carrajina (to choose an example at random) alphabetical order is:

a b c ch d dj e f g h i j l m n ñ o p r rr s t u v x z

So that takes care of sort order. Character sets are also easy. These are the set of characters for use. So ascii is and latin1 is and utf-8 is another set.

Collations are the rules used in a database to determine which letters are equivalent and what order to sort them in. So, in a sense, a collation is a combination of a character set and a sort order, plus rules about which characters are equivalent. In case-insensitive collations, capital A and lower-case a are equivalent. That’s useful, because otherwise all the words with that start with a capital letter would be sorted separately.

In MySQL, there are numerous collations that have the name pattern charset_collation_ci/cs. Examples are utf8_general_ci, latin1_general_ci, latin1_swedish_ci (which is the MySQL default), and so forth. The _ci stands for case-insensitive. _cs is case-sensitive, and then there is _bin as in ascii_bin and utf8_bin. _bin stands for “binary” and means that the collation is sorted in unicode code point order.

In MySQL, within _ci all accented letters are considered the equivalent of unaccented letters, so ‘à’, ‘á’, ‘å’, ‘ä’, and ‘ā’ are all considered to be the equivalent of ‘a’ when comparing and sorting. This means that as far as MySQL is concerned ä=a is a true statement. _bin collations do not do this. One can force MySQL to recognize accented characters as different (so that ‘n’ and ‘ñ’ are not considered to be the same letter) by using the keyword BINARY before the field name.

So which collation you use will determine how your data is compared and sorted.

There are ways to create new collations and to change sort order and equivalencies in existing collations. These require access to the server files, though, which makes them useless to those of us who host our sites on 3rd-party servers.

So here is my hack for creating a new sortorder for any character set. I am not saying this is the only way to do this (it’s not!) or even that it’s the best way to do this.

Let’s create a Carrajina sort order (while still using utf-general_ci collation). I do this by adding a field to the database called sort or something similar. In the case of Carrajina, I added a field called carrajina_sort to the carrajina_entry table and make it a VARCHAR datatype. I then wrote a function to parse each word and add a two digit code for each letter. Despite the fact that I am using numbers to sort, I am storing it as a string.I am also adding k, q, w, and y in case Carrajina ever adopts foreign words with those letters

a, A 10
b, B 11
c, C 12
ch, Ch 13
d, D 14
dj, Dj 15
e, E 16
f, F 17
g, G 18
h, H 19
i, I 20
j, J 21
k, K 22
l, L 23
m, M 24
n, N 25
ñ, Ñ 26
o, O, ó 27
p, P 28
q, Q 29
r, R 30
rr 31
s, S 32
t, T 33
u, U 34
v, V 35
w, W 36
x, X 37
y, Y 38
z, Z 39
[space], … 00
<sup> 000
-, ‘, </sup>

I’ve also set all spaces and ellipses to 00, the opening <sup> tag to 000 (and then the number after the tag will stay as is), and the closing tag, hyphens, and apostrophes are all set to empty. I then use the str_replace() function to substitute one for the other. Like so:

function getSort($value) {
    $chararray = array('<sup>', '</sup>', 'ch', 'dj', 'rr', 'ñ',
       'Ch', 'Dj', 'Rr', 'Ñ', 'ó',
       'a', 'b', 'c', 'd', 'e', 'f',
       'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
       'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
       'A', 'B', 'C', 'D', 'E', 'F',
       'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
       'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
       '-', "'", ' ', '...');
    $numberarray = array('000', '', '13', '15', '31', '26',
       '13', '15', '31', '26', '27',
       '10', '11', '12', '14', '16', '17',
       '18', '19', '20', '21', '22', '23', '24', '25', '27', '28',
       '29', '30', '32', '33', '34', '35', '36', '37', '38', '39',
       '10', '11', '12', '14', '16', '17',
       '18', '19', '20', '21', '22', '23', '24', '25', '27', '28',
       '29', '30', '32', '33', '34', '35', '36', '37', '38', '39',
       '', '', '00', '00');
    $value = str_replace($chararray, $numberarray, $value);
    return $value;
}

I then call each carrajina word, parse it, and update the row with the sort value.

$query = "SELECT carrajina_entryID, carrajina_term
    FROM carrajina_entry";

$stmt = $mysqli->stmt_init();
if ($stmt = $mysqli->prepare($query)) {
    $stmt->execute();
    $stmt->bind_result($id, $term);
    while ($stmt->fetch()) {
        //put each word into the array with the id as index value
        $wordarray[$id] = $term;
    }
    $stmt->close();
}

foreach ($wordarray as $id => $word) {
    $wordsort = getSort($word);
    $sortarray[$id] = $wordsort;
}

$query = "UPDATE carrajina_entry, SET carrajina_sort = ?
    WHERE carrajina_entryID = ?";

foreach ($sortarray as $id => $wordsort) {
    $stmt = $mysqli->stmt_init();
    if ($stmt = $mysqli->prepare($query)) {
        $stmt->bind_param("si", $wordsort, $id);
        $stmt->execute();
        $stmt->close();
    }
}

All done. Each entry now has a corresponding sort string in the carrajina_sort field.

As I said earlier, this is not the only way, or even the “best” way to do this. Those of you who have solved this problem in other ways, please leave a comment with your favorite solution.

 

Converting static pages to dynamic pages using PHP and MySQL, Part 3

The challenge:
Take Adam Walker’s Carrajina dictionary (http://carrajina.conlang.org/dicthome.html) and stick it in a database so that Adam can add an entry whenever he wants without re-writing an html page and possibly messing up his html. One stricture is that Adam does not want to learn too much new stuff – no SQL, no PHP, just HTML. And if he can enter SAMPA and have it turn into IPA, that’s a plus!

Step 1 (data prep) is here, and Step 3 (generating listings) is here.

Step 2 involves importing data into a database. That’s what this blog post covers. I skipped it earlier because it is more complicated than step 2.

Part 4, when I get to it, will discuss the forms for adding new entries to the database and editing existing entries. I might do a Part 5 to discuss CSS and other miscellaneous details.

So, at the end of step 1, we had a data model and two XML files – one with all the English-Carrajina entries and one with all the Carrajina-English entries.

First, we turn our data model into the sql statements required to create the database.

CREATE SCHEMA IF NOT EXISTS adamsdb;

Then we tell the server to use that database for now.

USE adamsdb;

Creating the tables:

CREATE TABLE `carrajina_entry` (
 `carrajina_entryID` int(11) NOT NULL AUTO_INCREMENT,
 `carrajina_term` varchar(255) NOT NULL,
 `carrajina_pronunciation` varchar(255) DEFAULT NULL,
 `etymology` text,
 PRIMARY KEY (`carrajina_entryID`),
 UNIQUE KEY `uq_carrajina_term` (`carrajina_term`)
) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci ;

CREATE TABLE `carrajina_definition` (
 `carrajina_definitionID` int(11) NOT NULL AUTO_INCREMENT,
 `carrajina_entryID` int(11) NOT NULL,
 `definition_number` int(2) NOT NULL DEFAULT '1',
 `part_of_speech` varchar(45) DEFAULT NULL,
 `definition` text,
 PRIMARY KEY (`carrajina_definitionID`)
) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci ;

CREATE TABLE `carrajina_idiom` (
 `carrajina_idiomID` int(11) NOT NULL AUTO_INCREMENT,
 `carrajina_idiom` varchar(255) NOT NULL,
 `carrajina_idiom_definition` text,
 `carrajina_entryID` int(11) NOT NULL,
 PRIMARY KEY (`carrajina_idiomID`)
) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

CREATE TABLE `carrajina_note` (
 `carrajina_noteID` int(11) NOT NULL AUTO_INCREMENT,
 `note` text NOT NULL,
 `carrajina_entryID` int(11) NOT NULL,
 PRIMARY KEY (`carrajina_noteID`)
) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

CREATE TABLE `english_entry` (
 `english_entryID` int(11) NOT NULL AUTO_INCREMENT,
 `english_term` varchar(255) NOT NULL,
 `english_pronunciation` varchar(255) DEFAULT NULL,
 `head_entryID` int(11) DEFAULT NULL,
 PRIMARY KEY (`english_entryID`),
 UNIQUE KEY `uq_english_term` (`english_term`)
 ) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

CREATE TABLE `english_definition` (
 `english_definitionID` int(11) NOT NULL AUTO_INCREMENT,
 `english_entryID` int(11) NOT NULL,
 `definition_number` int(2) NOT NULL DEFAULT '1',
 `part_of_speech` varchar(45) DEFAULT NULL,
 `definition` text,
 PRIMARY KEY (`english_definitionID`)
) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

Then we add the constraints. We could do this earlier, but it is easier to do it afterwards. That is to say, since we are asking MySQL to enforce these foreign key constraints (using innodb tables), it can’t do that until the tables are created. Thus, we create the tables first and then add the foreign key constraints.

ALTER TABLE `carrajina_definition`
  ADD CONSTRAINT `fk_cdefentry` FOREIGN KEY (`carrajina_entryID`)
  REFERENCES `carrajina_entry` (`carrajina_entryID`)
  ON DELETE CASCADE;

ALTER TABLE `english_definition`
  ADD CONSTRAINT `fk_edefentry` FOREIGN KEY (`english_entryID`)
  REFERENCES `english_entry` (`english_entryID`)
  ON DELETE CASCADE;

ALTER TABLE `english_entry`
  ADD CONSTRAINT `self_key` FOREIGN KEY (`head_entryID`)
  REFERENCES `english_entry` (`english_entryID`)
  ON DELETE SET NULL;

ALTER TABLE `carrajina_idiom`
  ADD CONSTRAINT `fk_idiomentry` FOREIGN KEY (`carrajina_entryID`)
  REFERENCES `carrajina_entry` (`carrajina_entryID`)
  ON DELETE CASCADE;

ALTER TABLE `carrajina_note`
  ADD CONSTRAINT `fk_noteentry` FOREIGN KEY (`carrajina_entryID`)
  REFERENCES `carrajina_entry` (`carrajina_entryID`)
  ON DELETE CASCADE;

Now we have an empty dictionary waiting for data. Time for those XML files. I wrote a PHP script to load the XML into the database. This involves a connection to the database, using mysqli which I am not going to detail here.

To process the xml, first we load the file:

if (file_exists('cartheng.xml')) {
    $xml = simplexml_load_file('cartheng.xml');
} else {
    exit('Failed to open cartheng.xml.');
}

simplexml_load_file is part of the SimpleXML extension and it provides an easy way to deal with XML. The simplexml extension comes with PHP 5.1.2 and later and is enabled by default. simplexml_load_file(filename) loads a file into a PHP object. This makes each node in the database an object as well. The XML file consists of a number of “entry” nodes and each can be referred to by $xml->entry[index] where index is the number of the entry, starting with zero and going on to whatever. “entry” subnodes can likewise be called with $xml->entry[index]->subnode_name.

First thing, we get a count of the number of entries:

$total = count($xml);

which turns out to be 2352. For each of those 2352 entries, we want to get the subnodes and enter them into the database:

for ($i=0; $i<$total; $i++) {
//carrajina, ipa, definitions+, etymology?, note?, idiom*
//start by naming our variables...
    $carrajina = null;
    $ipa = null;
    $etymology = null;
    $note = null;
    $subentry = null;

    $carrajina = $xml->entry[$i]->carrajina[0];
    $ipa = $xml->entry[$i]->ipa[0];
    $etymology = $xml->entry[$i]->etymology[0];

    //first insert statement
    $query1 = "INSERT INTO `carrajina_entry` (`carrajina_term`,
    `carrajina_pronunciation`, `etymology`) VALUES (?, ?, ?)";

    if ($stmt1 = $mysqli->prepare($query1)) {
        $stmt1->bind_param("sss", $carrajina, $ipa, $etymology);
        $stmt1->execute();
        $id = $mysqli->insert_id;
        $stmt1->close();
    }

Note that $mysqli->insert_id. That’s how we get the auto-incremented value that is the carrajina_entryID value for the subsequent inserts.

    $defs = $xml->entry[$i]->definitions->definition;
    //defs has subnodes

    $q = count($defs);
    for ($p = 0; $p<$q; $p++) {
        $num = $defs[$p]->num[0];
        $pos = $defs[$p]->pos[0];
        $deftext = $defs[$p]->deftext[0];

        //second insert statement
        $query2 =  "INSERT INTO `carrajina_definition`
        (`carrajina_entryID`, `definition_number`,
        `part_of_speech`, `definition`) VALUES (?, ?, ?, ?)";

        if ($stmt2 = $mysqli->prepare($query2)) {
            $stmt2->bind_param("iiss", $id, $num, $pos, $deftext);
            $stmt2->execute();
            $stmt2->close();
        }
    }//end for

    $note = $xml->entry[$i]->note[0];

    //don't run this if there is no note node
    if ($note) {
        //third insert statement
        $query3 = "INSERT INTO `carrajina_note`(`note`,
        `carrajina_entryID`) VALUES (?, ?)";

        if ($stmt3 = $mysqli->prepare($query3)) {
            $stmt3->bind_param("si", $note, $id);
            $stmt3->execute();
            $stmt3->close();
        }
    }//end if ($note)

    $idioms = $xml->entry[$i]->subentry;
    //idioms is also an array

    if ($idioms) {
        $k = count($idioms);
        for ($j = 0; $j<$k; $j++) {
            $idiom = $idioms[$j]->idiom[0];
            //because the file has idioms in upper-case...
            $idiom = strtolower($idiom);
            $idiomdef = $idioms[$j]->{"idiom-definition"}[0];
            //fourth insert statement
            $query4 =  "INSERT INTO `carrajina_idiom`
            (`carrajina_idiom`, `carrajina_idiom_definition`,
            `carrajina_entryID`) VALUES (?, ?, ?)";

            if ($stmt4 = $mysqli->prepare($query4)) {
                $stmt4->bind_param("ssi", $idiom, $idiomdef, $id);
                $stmt4->execute();
                $stmt4->close();
            }
        }//end for
    }//end if ($idioms)
}//end original for loop

The English-Carrajina side used more or less the same code with node names and variable names changed and of course different insert statements.

OK, maybe that wasn’t so complicated.