Archives For MySQL

I suspect people commonly underestimate what’s required of a good Web site search engine. Some developers probably think that you just create a search box and then use the supplied terms in a database query to get the results. But there are actually three aspects to a search engine:

  • The index of the content to be searched
  • The act of actually performing the search
  • The reporting of the search results

Many people, I believe, only really think about these last two, but it’s really the index that’s key to the success of any search engine, just like a good index at the back of a book makes it possible for a reader to quickly find what they’re looking for.

As far as search engines go, the gold standard is Apache Lucene. Lucene has been a reliable and popular search engine of choice for years now. Although Lucene is written in Java, the Zend_Search_Lucene module, part of the Zend Framework, is a great PHP port of the software. [intlink id=”790″ type=”post”]In a previous post[/intlink], I explained how to integrate Zend_Search_Lucene into a Yii-based Web application. The focus in that post is really on getting the two different frameworks to work together. This is easily accomplished for two reasons:

  1. Yii supports third-party tools nicely
  2. The Zend Framework can be used piecemeal

So that previous post on Yii and Zend_Search_Lucene walks you through the Yii Controller and View files you’d create to perform a search and report upon the results, something that Zend_Search_Lucene does easily. Creating the index itself is the actual challenge, then, and one that I don’t feel is adequately documented elsewhere. In this post, I explain how to use Zend_Search_Lucene to create a search index of a site.

Continue Reading…

I’m very pleased to say that I just received my copies of the Effortless E-Commerce with PHP and MySQL book yesterday, which means it should be available in stores any day now. Thanks to everyone for their interest in the book!

In the second demo site for my book “[intlink id=”1578″ type=”page”]Effortless E-Commerce with PHP and MySQL[/intlink]”, customers are able to make purchases without logging in. Although this isn’t a standard approach for many e-commerce sites, it’s actually a better way to go in terms of increasing profits. Simply put, requiring registration will inevitably hurt sales. I know there have been times when I stopped going through with a sale because I didn’t feel the need to register at a site I’m not likely to shop at again. But without a login system, it’s a bit more challenging to let customers view previous orders (e.g., to check the status of an existing order or to review older orders). There are two solutions.

The first solution is to provide a registration option. Then registered users could login and see existing orders but other customers could place orders without having to register. But what if you wanted any customer to be able to view their orders?

Without a registration system, the best way to allow customers to access their orders is by creating a pseudo-login, using information that would be unique to the customer and not publicly known. Logical options include any of the following: the customer’s email address, the order number, the order amount, the order date, the shipping zip code, and so forth. For example, using the email address and order number are the most practical choices. Simply create a form that takes these two pieces of information. Validate that the email address is of the proper email address format (perhaps using the Filter extension) and that the order number is a positive integer. Then, if both pieces of data passed the validation tests, you’d do a SELECT query like

SELECT * FROM customers AS c, orders AS o WHERE c.email=’$email’ AND o.id=$oid

If that query returns one record, the order information can be displayed. Note that a more complicated query is required to fetch the details of the order, similar to the one used to show the items in the customer’s cart or wish list (see the book for details). Also, as the second example in the book relies upon stored procedures, my inclination would be to write a stored procedure that accepts an email address and an order ID and returns either FALSE or the order contents, depending upon the validity of the submitted values.

And that’s all there is to extending this particular example site to add this nice feature.

Validation Scenarios in Yii

October 14, 2010

A long, long time ago, in a land far, far away (as far as I know), Jonah Turnquist wrote an article on validation scenarios in Yii. It’s a topic that I hadn’t paid too much attention to until playing around with the “search” scenario added in more recent versions of Yii (1.1 or thereabouts). If you’re curious about what validation scenarios are, Jonah’s post is a great little introduction.

I’ve spent the past couple of weeks developing a search engine for a Yii-based site (my problems and solutions specific to the search engine will be addressed in a separate post). As you may know, a search engine is really two things:

  1. One tool for creating the site index
  2. Another tool for searching the index and displaying the results

For both, I used the Zend_Search_Lucene module within Yii (or, I think that’s the route I’ll end up using). And although I initially created a Controller method for generating the index (per the instructions I sketched out in this other post), indexing a lot of content through a Web request is less than ideal. This particular site probably only has a hundred or more database records and a couple dozen files (PDFs) to be indexed, but the process still took a minute or two. Even if the index is only created or updated once a day or once a week, that’s still a lot of Web server resources being tied up. A better solution is to use a command-line script to create the index. Since most of the indexing requires a database connection and some Model information, I didn’t want to separate the indexing script from the Yii-based site. Fortunately, Yii supports command-line scripts by creating a “console application”. As with other things in Yii that are less common, the documentation on console applications is meager, but it was enough for me to go on. Here’s what you need to know… Continue Reading…