Online dictionary - should I use AJAX for the search results? - php

I'm making an online dictionary. Now I have two options:
1) Use AJAX for retrieving results
2) Just use some regular PHP scripts
If I choose the first option more likely the online dictionary will have one page and it's fast. If I choose the second option I'll have more pages (separate page for each entry with it's own URL), but it's slower. Personally I like the second option, don't really like to much AJAX on pages.
What is your opinion? Cons and pros (for this certain case)?
Thank you.

If you use the second solution (ie, several URLs, one per page/definition), your users will be able to bookmark URLs for each specific page, use those to come back to youor site or link to it, or send them to other people, or whatever they want -- which is good.
If you only have one and only one page for your whole website, people cannot link to specific pages/definitions ; they have no way to come back to one specific word ; and that's bad :-(
Same for search engines, btw (Even if not that important for search results) : if they only see one page, they will not index your content well... and you probably want your site indexed.
So, in your case, I would probably go with several distinct URLs (even the corresponding pages are all generated by the same PHP script, of course).
And same thing for search results : you probably want to give people the ability to link to search results, don't you ?
About the "speed" thing : well, with Ajax, you'll send one request to the server. Without Ajax, you'll still send one request (for a bigger page, I admit), plus the ones for the images, CSS, JS, and all.
You should read a bit about Frontend optimization (see Yahoo's Exceptional Performance pages, for instance) ; it'll help quite a lot, about that ;-)

You could potentially use htaccess (assuming you're on a linux server) to give unique urls to each page which keeping one page for displaying content.
For instance:
http://yourdomain.com/s/sausage would point to http://yourdomain.com/page.php?id=426
Which then would pull from a db and show you the 426th result, which would be for sausage, hurray. You'd probably want to build a htaccess generator for this, or use a system of slugs and use regular expressions
htaccess would look like this:
$http://yourdomain.com/s/([^/]+)^ $http://yourdomain.com/page.php?slug=$1
this will send anything after http://yourdomain.com/s/ to page.php as a slug variable, then reference your database for that slug.
Not sure if that's in any way helpful.

Related

Masking subdirectories and pages for my website

I want to mask some of the subdirectories and pages for my website. For example:
I want to change
www.example.com/blog.php?post=post1
to
www.example.com/blog
or something similar to that. I have seen examples about redirecting and such but that doesn't seem to work for what I want and I would like to stay away from iframes if possible. I just want the address bar to not reflect my internal directory structure and page names. I want it to keep showing /blog while they are moving from post to post. Thanks.
P.S. I am not using wordpress or any other CMS or blogging system.
You can use apache mod_rewrite for that.
Mod Rewrite Generator
And if You don't want to have the same url on blog-post/article change, but to display different content, all I can think of this moment is by using either POST method or browser cookies, but, that would require a lot of page reloads, and it simply is not recommended for wide use. If You are building per_se (one person only) panel or similar, than url doesn't matter that much, but, speaking of blog..
It is quite reasonable to hide .php extension or url query index/key, but not what You would like to accomplish.
Like I said, it is possible, but Luke .. don't do that.
Blog should be bookmarkable on each and every #stop, and that happens just because of unique urls and hash values. Without these two, no hyperlinking possible *(not to mention seo penals and flaws + dozen of other unwanted obstacles, page caching for instance).

JQuery Pagination With Dynamic Loading

I'm currently creating a website for a friend of mine, and he wants a "Blog" type of system on the front page, which is fine by me, i'll be able to do that.
I'm currently using the Pagination Class slightly modified to remove the "All" option (And needless output related to it).
However, I would like to be able to Dynamically load each page instead of redirecting to each page (If a re-write of the Pagination class is required i won't mind doing it, as long as I can get a general concept of how to do it...)
Solutions like JPaginate Won't work for me, because in the end, the page could be loading huge amounts of text (The message body is stored in a LargeText table) and i don't want to have all the articles in the source code (Previous experience such as that has actually taught me loading that much text into a web browser is not the smartest thing)
A few things to note, the connection is MySQLi (The Pagination class only appends the limit code for Sql), I don't know much Javascript, and i still consider myself a noob at php.
You can use something like this:
http://www.9lessons.info/2009/09/pagination-with-jquery-mysql-and-php.html
http://www.9lessons.info/2010/10/pagination-with-jquery-php-ajax-and.html

How to open a post in PHP when clicking it, a good approach

So I have a list of posts and when clicking on it, it should direct me to that particular post. Basically the way I code this is adding the id of the post to the end of the link, like posts/1 which will take me to the post with the id of 1. This is a basic design, because I see on many sites they do not have such urls...do they post the data to the new page? Or what is a better design to do this?
This is exactly how StackOverflow (among others) does this and it is good practice indeed. This is the URL of your post:
http://stackoverflow.com/questions/9257811/how-to-open-a-post-in-php-when-clicking-it-a-good-approach
The "/questions/9257811" segment is the id of the post. The other part is optional and is only used for Search Engine Optimization. Try to load http://stackoverflow.com/questions/9257811/WRONG-DESCRIPTION and see for yourself.
Edit: I couldn't use a wrong description in the above link, StackOverflow was correcting it automatically. It's easy to get the title once you get the id, which is how they autocorrect it. This avoids links like "/posts/34234/offensive-words-here" for example.
What you describe is the best way to send and receive an id in the URL. Most webpages do it this way. You have a link (example: Post #1.).
When applying url-rewrite it simply "prettify" the url by making it look this way http://www.yoursite.com/post/1, but in fact, the url is actully still the same. You can access it with $_GET['id'] was usual.
I beg to differ. Though most sites uses it (and i admit, numericals are more convenient for refer), a friendly approach is, using the title of the post. But you need to do a lot of extra steps to make it error free (such as, no post can have identical titles, using a dash or underscore between the words for flexibility plus filtering the url should be more strict and becomes complicated in this case). If you want to avoid all these hussle, use ids as they are easier to implement.

How to use multiple files in a php webpage

I am creating a webpage with PHP/MySQL, and each page is in its own .php file.
How should I organize the addressbar, when changing the viewing page?
I have thought of two different ways to solve this:
1) www.mywebpage.com/?page=REFERENCES+pageid=GUID_TO_USE_FOR_DB_LOOKUP
2) www.mywebpage.com/references.php?GUID_TO_USE_FOR_DB_LOOKUP
Which should I choose? Are there other ways to do this?
Edit: When the user clicks a link to another page, I want to redirect the user to the corresponding .php file. The question is how to show this in the addressbar in the web browser.
With the GUID_TO_USE_FOR_DB_LOOKUP, I want to pass parameter to the .php file
Another way to do it:
www.mywebpage.com/references/GUID.html
You can use mod_rewrite to use URLs like this and have them mapped to "references.php?GUID=xyz" internally.
However, all solutions are fine and will work the same way (display the page), the advantage of the above is just that the URL is more "readable" as well as better for search engine optimization as the URL-"path" is parsed by search engines (other than using parameters).
Search for "mod_rewrite SEO" to find out more about this topic, e.g.:http://www.htmlist.com/how-to/a-simplemod_rewrite-tutorial/
A couple of questions to ask:
Why do you care about the URL? Should it be human readable? Do you care about SEO? Do you like to use a "framework" with a single point of entry (which could work like your first example)? Do you want to "hide" your actual filenames from your users? etc. etc.

How to organize a PHP blog

So, currently I'm organizing my blog based on filename: To create a post I enter the name of the file. As opposed to storing the post in the database, I store them in PHP files. So each time I create a post, A new row in the table is created with the filename and a unique ID. To reference the post (e.g. for comments) I get the name of the current file, then search the entries table for a matching file name. The post ID of the comment matches the ID of that post.
Obviously this isn't the standard way of organizing a blog, but I do it this way for a few reasons:
Clean URL's (even cleaner than mod_rewrite can provide from what I've read)
I always have a hard copy of the post on my machine
Easier to remember the URL of a specific post (kind of a part of clean URL's)
Now I know that the standard way would be storing each post in the database. I know how to do this, but the clean URL's is the main problem. So now to my questions:
Is there anything WRONG with the way I'm doing it now, or could any problems arise from it in the future?
Can the same level of clean URL's that I can get now be achieved with mod_rewrite? If so, links are appreciated
I will be hosting this on a web host. Do only certain web-hosts provide access to the necessary files for mod_rewrite, or is it generally standard on all web-hosts?
Thanks so much guys!
P.S. To be clear, I don't plan on using a blogging engine.
As cletus said, this is similar to Movable Type. There's nothing inherently wrong with storing your data in files.
One thing that comes to mind is: how much are you storing in the files? Just the post content, or does each PHP file contain a copy of the entire design of the page as opposed to using a base template? How difficult would it be to change the design later on? This may or may not be a problem.
What exactly are you looking for in terms of clean URLs? Rewrite rules are quite powerful and flexible. By using mod_rewrite in conjunction with a main PHP file that answers all requests, you can pretty much have any URL format you want, including user-friendly URLs without obscure ID numbers or even file extensions.
Edit:
Here is how it would work with mod_rewrite and a main PHP file that processes requests:
Web server passes all requests (e.g., /my-post-title) to, say, index.php
index.php parses the request path ("my-post-title")
Look up "my-post-title" in the database's "slug" or "friendly name" (whatever you want to call it) column and locates the appropriate row that way
Retrieve the post from the database
Apply a template to the post data
Return the completed page to the client
This is essentially how systems like Drupal and WordPress work.
Also, regarding how Movable Type works, it's been a while since I've used it so I might be wrong, but I believe it stores all posts in the database. When you hit the publish button, it generates plain HTML files by pulling post data from the database and inserting it into a template. This is incredibly efficient when your site is under heavy load - there are no scripts running when a visitor opens up your website, and the server can keep up with heavy visitation when it only needs to serve up static files.
So obviously you've got a lot of options when figuring out how your solution should work. The one you proposed sounds fine, though you might want to give careful consideration to how you'll maintain a large number of posts in individual files, particularly if you want to change the design of the entire site later on. You might want to consider a templating engine like Smarty, and just store post data (no layout tags) in your individual files, for instance. Or just use some basic include() statements in your post files to suck in headers, footers, nav menus, etc.
What you're describing is kind of like how Movable Type works. The issues you'll need to cover are:
Syndication: RSS/Atom;
Sitemap: for Google;
Commenting; and
Tagging and filtering content.
It's not unreasonable not to use a database. If I were to do that I'd be using a templating engine like Smarty that does a better job of caching the results than PHP will out of the box.

Categories