开发者

PHP replace page parameters

开发者 https://www.devze.com 2023-04-04 03:26 出处:网络
I am very new to PHP and MySQL and I have some questions. First, I have a menu like this: home.php, about.php, etc. When someone access home.php?page=1&opt=2&etc, I want the URL to appear do

I am very new to PHP and MySQL and I have some questions.

First, I have a menu like this: home.php, about.php, etc. When someone access home.php?page=1&opt=2&etc, I want the URL to appear domain.com/home/1/2/etc . Is there any way to achieve this?

Second, it is a good practice to save the pages content in a database and then display it from that database when the page is requested? I mean if the link is home.php?page=1&opt=2&etc, I have to do a MySQL database interrogation with those parameters, and if it is found I have to display t开发者_开发问答he field (which contains html/php code). It is correct? Or what approach should I have?

Thanks!


First, I have a menu like this: home.php, about.php, etc. When someone access home.php?page=1&opt=2&etc, I want the URL to appear domain.com/home/1/2/etc . Is there any way to achieve this?

As Col. Shrapnel said, it's the other way around, but you should do something else. This article may help you. http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/

Or, assuming you are using Apache, you could do something like this:

In your .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?args=$1 [QSA,L]
</IfModule>

This will return the requested resource if it exists or transform your URL: www.domain.com/1/2 into www.domain.com/index.php?args=/1/2

Somewhere in your PHP: You will have to parse the one parameter: $_GET["args"] Which from the example it would contain "/1/2"

Note that all requests are going to enter through index.php.

I would suggest you to design your URL's carefully and write a parser as general as possible. For instance: Will /x/y always mean: page=x opt=y or sometimes it will mean for example invoice=x line=y? Maybe you could consider to add some more info to the request: www.domain.com/pages/1/2 Would clearly refer to pages.

Of course that, if you only will dispatch pages, the first option will work ok.


EDIT.
Well, based on your comments, here is another answer.

Do not mess with urls. Just forget it for a while. After all it's just a decoration. It will confuse you and waste too much time, while it's way less important than your main goal - site itself. Go for some tutorial (I believe Sitepoint.com have one) and learn how to build dymamic PHP+mysql driven site. It is not the topic that can be answered in a 2-paragraph answer. In fact, people go for classes for about a year to get the basics. You're asking too much, as though web-development is like a silly game. It is not.

When someone access home.php?page=1&opt=2&etc, I want the URL to appear domain.com/home/1/2/etc

There is no sense in that.
The only thing is sensible is opposite one: someone access domain.com/home/1/2/etc but real address processed is home.php?page=1&opt=2&etc

I have to do a MySQL database interrogation with those parameters, and if it is found I have to display the field

Yes.
You're on the right track. That's exactly how every site in the world works.

(which contains html/php code). It is correct?

Yes, but with one correction: without PHP. HTML only

0

精彩评论

暂无评论...
验证码 换一张
取 消