Jump to content
Larry Ullman's Book Forums

Recommended Posts

I thought Lou meant the site in the link belonged to him* I just now visited it and it seems like it was just something he* was referencing. My bad.

 

*Him, right? I don't see a gender listed in Lou's profile, so I assumed based on the name. (I knew a guy named Lou once.)

 

- - - -

 

I am also wondering something. Since the title is only in the URL for the benefit of search engines, does it even have to be unique?

 

No I haven't completed any site yet. I've been reading PHP, MySQL, HTML5 books for 3 years and haven't come close to completing a real site yet. I have every one of Larry's books except this new Advanced one. I can code, but I lack a plan for putting it all together. Basically, it's a database problem.

Link to comment
Share on other sites

I'm going to look at the E-Commerce book, I have that one as well. Never got to the second project in that book, so if the mod_rewrites section is there, that's why I missed it.

Link to comment
Share on other sites

I think I'm confused because the rewrite rule part always has blog.php?t=title&a=article_id. Is that the part that's going to be the new URL? I don't see specifically where the new URL is going to be the title of the blog post. I see you're using the regex's parenthesis and assigning them $1 and $2 but I don't see where this comes into play to assign the new URL.

Link to comment
Share on other sites

Guest Deleted

Yup, that's how my site works. The directory structure you see in the URL is a lie xD

 

Well, I assume it's mod_rewrite. I use codeigniter and I don't actually see how it does it.

 

And you should make a site, even if it's not very good, because the experience will help your comprehension tremendously.

Link to comment
Share on other sites

I am building a dating site. Figured I'd start simple, but the few things I don't understand have stopped me from completing it thus far.

 

Clean URLs for the blog section (mod rewrite)

 

Database structure (user table, profile table, etc). I find it really hard to figure out what's going into the database and specific tables.

 

I know how to make a user's image gallery, but how do you let them define a main photo? do you put that main photo in their profile table, or just in the photos table? Can you define a main photo by say, a 0 or 1 in the photos table, or always keep their main photo in their profile table?

 

If you store sessions in a database using procedural mysql (Larry writes the six functions in the advanced book), can you connect to the database for other tasks using PDO or object oriented?

 

I can get distances between users from my zip code database, but how would I implement users from other countries? i see on other dating sites they let users sign up from different countries and territories. do you have to determine distances for those users as well, and if so, how? Or should I just keep the site USA only?

 

See I have these weird questions one needs to actually complete a site but is rarely covered in any book. :)

Link to comment
Share on other sites

I don't see how this RewriteRule works. I'd like the title of the blog post to be part of the URL of the blog post, I don't want the URL to be article_id=2. It looks like these rules make sure the URL is in the format I don't want. Can you explain each line?

I think I'm confused because the rewrite rule part always has blog.php?t=title&a=article_id. Is that the part that's going to be the new URL? I don't see specifically where the new URL is going to be the title of the blog post. I see you're using the regex's parenthesis and assigning them $1 and $2 but I don't see where this comes into play to assign the new URL.

The whole point of mod_rewrite is that you show users one URL, but use a completely different one to process everything. In the URL you show your user, you want to insert the blog title. That's the usual pattern.

However, in the URL you convert to (which users will never see or know about), you want to do something like "article_id=2".

 

Database structure (user table, profile table, etc). I find it really hard to figure out what's going into the database and specific tables.

This I can't help you with without more information. This sort of question probably belongs in a new topic though.

 

I know how to make a user's image gallery, but how do you let them define a main photo? do you put that main photo in their profile table, or just in the photos table? Can you define a main photo by say, a 0 or 1 in the photos table, or always keep their main photo in their profile table?

There are any number of solutions, but adding a flag column to the DB table would probably be the easiest. Like you said, mark a user's main photo with a 1, and all other photos with a 0.

 

If you store sessions in a database using procedural mysql (Larry writes the six functions in the advanced book), can you connect to the database for other tasks using PDO or object oriented?

Yes. How you connect to the DB has no effect on the data in the DB. You can connect any number of ways or any combination of ways.

 

I can get distances between users from my zip code database, but how would I implement users from other countries? i see on other dating sites they let users sign up from different countries and territories. do you have to determine distances for those users as well, and if so, how? Or should I just keep the site USA only?

Good question. I'm thinking that you can use the Google Maps API to calculate distances, but I really don't know.

 

And whether to use PDO, MySQLIi, it's crazy. All those options and you basically just insert, update, delete and select information.

Yep. I prefer to keep things simple, but you're right in that there are a number of ways to do the same things.

Link to comment
Share on other sites

Still confused but getting there.

 

Say you have your details.php page and it's showing article 2. details.php?article_id=2

 

You do your rewrite rule to change the url to articles/my-article-title, taking the info from details.php?article_id=2.

 

I still don't get how you get the title of the article into that new URL. Where does it come from? It seems to be going in the wrong order. How does your regular expression pull the title out if the link is article_id=2?

Link to comment
Share on other sites

Okay I went back to read some. Stupid me. I'm thinking that the links have to be details.php?article_id=2 instead of just inserting the title of the post into the link like Hartley showed me. I don't know why I thought the first way was the only way. Probably because in one of the books I'm reading they do it that way, and instead of trying to solve the issue in a different way I stayed rigid in the original link format.

 

Another question though. If I change the links to the blog posts to the article id/name of blog post format, what happens to details.php that originally handled whatever post I pulled up, let it be article_id 1, 2, 3, 4, 5, or 100? How would I get to the details.php page if the link is already the name of the article?

Link to comment
Share on other sites

The title of the article is in the DB.

If your DB is properly designed, you should only need the article ID to get everything else you need about a particular blog post (including the title). As such, you put the article ID somewhere in the pretty URL with the title of the article, and you then write a regex that takes that article ID from the pretty URL and uses it to produce a meaningful URL that your script can handle.

 

I've already stated this a few times (so it might be useful for you to go back and read this entire thread from the beginning), but the following type of pretty URL is worthless to you:

www.domain-name.com/blog-title-here/

 

You instead need to add the article ID somewhere in the pretty URL. For example:

www.domain-name.com/2/blog-title-here/

 

By doing that, your pretty URL is just as readable to people, and you can use mod_rewrite to get that article ID ("2") and use it to get the right info from the DB. For example, if you have the following mod_rewrite RewriteRule in an .htaccess file in your web root:

RewriteRule ^(\d+) details.php?article_id=$1

Then the following pretty URL will be converted to the following actual URL, which can be processed like any other GET request to the details.php script:

www.domain-name.com/2/blog-title-here/

www.domain-name.com/details.php?article_id=2

 

Does that make sense?

Link to comment
Share on other sites

Reading your stuff Hartley, makes me realize this has too many layers of abtraction for me to ever completely understand.To really make a complete and useful website you've got to be a master, and all of these master details aren't covered in any one book (or ten books) and only a small percentage could ever understand it anyway.

Link to comment
Share on other sites

Let me try to write this in some kind of psuedo code.

 

1 On my pages link the individual blog posts with their title, like articles/row(from database)/title-of-post (from the database row), not just details.php?article_id

 

2 Create a rewrite rule that matches the link (articles/whatever ever comes after it), stores the row and title in a variable like $1 and $2, then run details.php?article_id=$1. I guess I only need the article id then.

 

3 The details.php script executes like before, but the user sees the pretty URL, not the mess with the query string

Link to comment
Share on other sites

Yes, your last post was right on the money. That's exactly what most people usually do. For things like articles and blog posts, it's also popular to combine the date of the article (instead of the ID) with the title. Either way is fine.

 

Also, don't get so down about becoming (or not becoming) a web development master. This stuff takes time. If it were easy, everyone would do it.

You are right that no book can cover it all. What a good book does do though is give you enough info and direction to allow you to find the rest of the info you need on your own.

Larry's book combined with random Stack Overflow posts is enough to do most anything.

 

Anyway, it sounds like you're starting to get it. I recommend playing around with it to ensure that you got it.

Please report back any success you find with mod_rewrite, and feel free to ask any other questions you have.

Link to comment
Share on other sites

The site I referred to takes the following approach:

 

1. blog.php - handles displaying the blog.  If the user clicks on the blog nav item, the user is shown excerpts from the last 5 posts each with a 'read more' link to the full article. This link links to blog.php with the title and id of the blog post e.g. blog/blog-post-title/7. This is the clean url. The mod_rewrite rule

RewriteRule ^blog/([A-Za-z0-9\+\-]+)/([0-9]+)$ blog.php?t=$1&id=$2

says match anything that starts with 'blog' followed by a slash followed by any number of letters, numbers and dashes followed by another slash, followed by numbers and translate it to blog.php?t=$1&id=$2 where $1 is a variable and is equal to the first block and $2 is a variable equal to the second block e.g. blog.php?t=blog-post-title&id=7. In blog.php the title and id are accessed via the $_GET global. I've passed the title in the url as it is being used as the <title> tag in the HTML page in step 2.

 

2. if the user clicks on the 'read more' link, he is taken to a page that displays the one post in full. The t variable in the url is used as the title of the html page and as the heading.

 


And whether to use PDO, MySQLIi, it's crazy. All those options and you basically just insert, update, delete and select information.

PHP is an evolving language so often a new function or class will be an improvement on an earlier function. For example the mysqli functions are an improved extension of the mysql functions. It's all a learning process and you will continue to pick up new ways of doing things. You will be surprised how quickly you will get to the point of being able to create the site you want as along as you keep at it. Like HartleySan said, feel free to ask any questions.

Link to comment
Share on other sites

Guest Deleted

Speaking of PHP mastery taking time, I've been at it for many years and I'm nowhere close to a master. I still sleep at night. Don't sweat it, Lou. Do the best you can and learn at a pace that's not going to burn you out. It might help if you picked some small aspect of your site and focused on that for the time being. When you got it working, you'd have something to feel good about.

Link to comment
Share on other sites

Mod Rewrite is pretty great. You can change all your .php pages, like register.php, to just register. I'm going to mess around with it tonight and get a feel for how it works.

Link to comment
Share on other sites

Guest Deleted

It is pretty cool, isn't it?

 

And yes, by all means, play around! You'll be amazed by how much it helps your comprehension. :)

Link to comment
Share on other sites

 Share

×
×
  • Create New...