Paginating results

February 23rd, 2010 § 0

Well, I succeeded in paginating the results of the main search query (index.php). I’ve set it up to display four records per page (because the record display is fairly large), with links to the other pages of results. I had to switch the search to GET for it to work. I’m not sure that’s completely necessary, but I wasn’t able to get POST variables to carry over. I haven’t positioned the links properly, but that’s inconsequential, really.

I would explain how I did it, with code examples, but I don’t entirely understand it, yet. So that will have to wait for another time.

Project Description

February 17th, 2010 § 0

Since almost all of the most interesting technical problems have been solved, at least as far as I can tell, I’m going to write a paper about the site. Even if it doesn’t go anywhere but my own harddrive, I think it will be good to lay out what went into making the site.

The breakdown will be:

  • Intro
  • Problems facing non-specialists
    • Characters
    • Romanization
    • Using language resources (dictionaries)
    • Computer input
  • the plan of the site as it addresses each of the above
  • Difficulties encountered; solutions
  • Assessment of the site

emacs and WordPress

February 16th, 2010 § 0

This is just a test post from emacs, using weblogger.el. I can’t really believe it, but it’s started to drive me crazy that I have to exit emacs to post to my blog. How crazy is that?

Easy search

February 16th, 2010 § 0

On my way to work today I figured out how to make the search work the way I want.  It really is true that learning this stuff is additive.  Every step of the way, I get another tool that allows me to do what was impossible (or at least frustrating) before.

What I’ve wanted from the start is a single search box that will allow users to enter any number of terms and find those records that have all of those terms.  Without that, I’m not sure how useful the site would be to people who don’t know any Chinese.  That’s why I had tried to use full-text searching (which I will now disable); but that wasn’t suitable to the kind of search necessary here.

The answer to this was really no more complicated than using parentheses.  Here’s my main search as it stands now:

$search = $_POST['search'];
$terms = explode(” “, $search);
foreach ($terms as $value) {
$q .= “(simp LIKE ‘%$value%’ OR trad LIKE ‘%$value%’ OR pinyin LIKE ‘%$value%’ OR wadegiles LIKE ‘%$value%’ OR english LIKE ‘%$value%’) AND “;
}
$q = substr($q,0, (strlen($q-4));
$query = “SELECT simp, trad, pinyin, wadegiles, english FROM main WHERE $q”;

As you can see, this builds on almost everything I’ve learned so far; the only new thing being a little bit of SQL syntax.  In plain language, here’s what the code above does:

First, it gets the user-submitted string, which it then separates into multiple terms using the whitespace as the delimiter.  These terms are put into an array called $terms.  The reason to put them in an array is to be able to handle them one by one, instead of as a single string.  So the array is handled using foreach with the instruction to assign each term in turn to $value, and then to plug $value into the SQL fragment.  At each go-round, the SQL fragment is appended to the contents of $q.  Even though “AND” is supposed to have precedence over OR (which should mean that the parentheses are not necessary), without them the query doesn’t work properly.

Once the foreach loop is complete, it’s necessary to strip off the final “AND” to make a valid query, which is constructed by adding the contents of $q to the first part of the SQL query, all of which is then put into $query.  From there, it’s run as usual.

I think this really makes the site more usable.

Random result

February 15th, 2010 § 0

My trouble with the random record search was really silly.  I set up a while loop to ensure that the random number either returned a valid key, or the whole thing would start over again.  However, I also had this little bit undermining it all:

$rows = mysql_num_rows ($result) or die(mysql_error());

So, basically, I was telling it on the one hand to make sure the random number was valid, but then on the other telling it to give up if the number didn’t work the first time.  Nice.  Here’s what I should have been doing:

$q = “SELECT * FROM main”;
$r = mysql_query ($q);
$num = mysql_num_rows($r);

while (empty($rows)) {
$n = rand(1, $num);
$k = “SELECT * FROM main WHERE main.key=’$n’”;
$result = mysql_query($k);
$rows = mysql_num_rows ($result);
}

Initially, I guess I’ll turn this into a “Surprise Me!” link, but then I want to use it to set up a daily RSS feed of random terms from the db.