![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| ||||
| TUTORIAL: Efficient Storage of Option Choices Facilitates Searching & Retrieval AN ERROR HAS BEEN FOUND IN THE METHODOLOGY DESCRIBED HEREIN. PLEASE SEE THE LAST FEW POSTS IN THIS THREAD BEFORE PROCEEDING. Let me begin by offering a warning. This is a technical article detailing quite a few concepts, including some which are mathematical. If that's not your thing, then this may not be the article for you. If it is, well, then have I got a treat for you! Also, you're definitely going to want to add this thread to your notify list as I'm breaking it up into multiple parts and will post one part each day. Programming is very much about giving system users choices and then changing behavior to reflect the users' choice(s). Searching for houses meeting a certain criteria, loading programs at system startup, and finding long-lost loved ones are just some examples where programs must change behavior based upon choices available to system users. In this article I will discuss an efficient algorithm (or methodology, if you prefer) for storing user choices which facilitates any subsequent decision-making processes from the perspective of PHP 5 OOP and MySQL. Let's begin by taking a look at an example usage. We will then work through this example, expanding the code, until we get a fully-functional example. Amazon books has quite a few subjects and the subject list doesn't change that often, so we'll use their subject list (reference: Amazon.com: Subjects: Books: Nonfiction, Professional & Technical, Literature & Fiction, Science & More ) as our working example. We'll also work under the assumption that categories have only one attribute, name. The next step would be to model our database and create a PHP script to handle the database connection. I'm not going to discuss most of the database model, just the part which is relevant to what we need: saving and searching through subjects as quickly as possible. That means the only real question which must be answered before we create the database is, "What type of field should subject be?" Well, we really only have three options to check out given it will be a single database field: char/varchar, enum, or integer.
Now, each of these fields has certain characteristics, so let's discuss each of them.
Last edited by TeraTask : 07-27-2007 at 03:01 PM. |
| The Following 3 Users Say Thank You to TeraTask For This Useful Post: | ||
| |
| ||||
| Part 2 The only thing we've been told so far is "1 subject requires 1 bit". The reason that requirement was made is because each subject will be assigned to one bit of an integer. To do that, we can adjust our PHP script (and add in some DB validation): PHP Code: PHP Code: Now, that we have each subject assigned to the bits 0-34 and created a database table which can hold the sum of any combination of those subjects, we're essentially done with setting up the framework and can begin looking at how we can use this structure. Tomorrow we'll get into using this list to actually get something done! In the meantime, please feel free to post any questions on what's above. |
| The Following 2 Users Say Thank You to TeraTask For This Useful Post: | ||
admin (07-20-2007), HelloWorld (07-19-2007) | ||
| ||||
| Thanx TeraTask, I think this would help a lot to answer Google second's interview question... Google Interview Question |
| ||||
| I guess I'm one of the person who can't help that much lol.. However, here's what I found weird: I see that they're all multiplication of 2. BUT, the last two are: Code: [-2147483648] => Sports
[0] => Women's Fiction |
| The Following User Says Thank You to HelloWorld For This Useful Post: | ||
TeraTask (07-27-2007) | ||
| ||||
| lol. You identified the problem. That's happening b/c the number of bits necessary to store the number I've assigned to those 2 slots exceeds what PHP allows by default. I have to study their documentation to see if there's a way around that. If there is, I'll share it. If there isn't, I'll kill this thread with a big warning. I had used this method on some guy's site a couple of years back - I bet he's really screwed since he ticked me off and I stopped working with him. lol. EDIT: Just found Quote:
Last edited by Lee : 07-27-2007 at 04:06 PM. Reason: 2 posts in 3mins, just edit your last :) |
| ||||
| Quote:
|
| ||||
| Our posts crossed, HelloWorld. PHP does have types, you just don't have to explicitly declare them. Type-casting works, however. For example, PHP Code: |
| The Following User Says Thank You to TeraTask For This Useful Post: | ||
HelloWorld (07-27-2007) | ||