Create a view from slug blog title in plain php - php

I've seen a lot of material on how to generate a unique slug title and then store it in the database.
I want to make a blog with urls like blog/my-first-post and then get the content from the db and make the view.
Let's say I will have a very basic table with columns (id, title, body, slug) and I already have the generated slug in the database for each post.
How can I make a simple router in PHP that gets the url blog/my-first-post get the slug and search for it in the database and then get the contents of that post from the db so I assign the blog contents into an array and then make the view for that url?
All this without using a PHP framework if possible.

You could route everything to an index.php page with htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Using this htaccess file, the URLs would look like www.website.com/blog/slug-name
The slug-name part of the URL can be accessed with $_GET['url']
Then just get the relevant data from your database that matches the slug:
SELECT title, body FROM blogs WHERE slug = $_GET['url']
Obviously don't use that SQL, it's just an example. Make sure you use prepared statements

Related

Imitate the page creation/url rewriting effect of this strawpoll.me

I am looking to do something along the lines of what is shown here:
http://strawpoll.me
When you create a poll on there it gives you a unique url to which you and anyone else can access it by visiting that url.
Now obviously they can't be cluttering up their server with thousands of individual files containing the polls and I'm assuming they have one basic template to which they load the poll information from a database.
I want to make a form and when the user submits it they get their own unique link like how it is on the strawpoll website.
As is it is a simple dynamic page creation requiring only few files...
The poll creation page which is static
The processing.php file which collects the data and inserts into the db
The dynamic poll rendering page polls.php which renders the poll based on an auto-incremented id assigned to the poll.
The polls-results.php which returns the results.
http://thepollsite.com/12345
.htaccess is used to identify which poll to render as well as which results to render.
# Turns on Mod-Rewrite Engine
# ----------------------------------------------------------------
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# Poll Pages
# ----------------------------------------------------------------
RewriteRule ^([^/]+)/?$ polls.php?id=$1 [QSA,L]
RewriteRule ^([^/]+)/?$ polls-results.php?id=$1 [QSA,L]
On both polls.php and polls-results.php you get the id with: $poll_id = $_REQUEST['id']; and process from there.
The embed code is rendered in a simple dialog pop which provides the iframe code as shown.
Though there are frills you can add... its is really a simple process.
On the .htaccess side, you'll want something like:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
With that in place, all requests for files/directories that don't already exist on the server will run index.php instead of showing a 404 page. Then, to get the requested path, use $_SERVER['PATH_INFO'] in your index.php.
This is the general method of using "clean" URLs with PHP. It's probably already been said in another SO answer, but it's hard to find so I wrote it again.
Related:
Why use a single index.php page for entire site? and How to do URL re-writing in PHP?

How can I make pretty URL link with wordpress?

I've got some questions about making pretty URL link.
On my website home page, I have a drop down list of text (city and state) which is going to be my parameter. I'm sending the value through GET METHOD to a WordPress template page which system will display contents based on passed value. The display content part is working successfully, however, I have some question about link.
Right now, the link of the page is showing
www.mydomain.com/list/?state=NY&city=Newyork or
www.mydomain.com/list/?state=IL&city=Chicago
I prefer the link to be the following pretty format, ...
www.mydomain/list/NY/Newyork
www.mydomain/list/IL/Chicago
I have researched on many sites and found recommendation on using htaccess. I'm using the following code but still link doesn't get changed to the pretty format.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([0-9]+)/?$ ?district=$1
RewriteRule ([0-9]+)/([^/]+)/?$ ?district=$1&templename=$2 [L]
</IfModule>
# END WordPress
What you're looking for should be done using wordpress' add_rewrite_rule function. You can read more about it here
Here's a sample that may be what you're looking for:
<?php
add_rewrite_rule('list/([0-9|a-z|A-Z]*)?/?([0-9|a-z|A-Z]*)?/?([0-9|a-z|A-Z]*)?/?([0-9|a-z|A-Z]*)?','index.php?$matches[1]=$matches[2]&$matches[3]=$matches[4]','top');
?>
To explain things in detail:
First, we declare a regular expression to match any get parameters after the "list" slug. (i.e: www.mydomain.com/list/)
Secondly, we declare how wordpress should interpret it. The file will always be index.php for wordpress. The parameters will line up like www.mydomain.com/list/state/IL/city/Chicago which will translate to www.mydomain.com/list/?state=IL&city=Chicago in your code for you to use your $_GET parameters
top tells Wordpress that these rules have precedence over wordpress' own rules.
You may need to call flush_rewrite_rules( false ) or $wp_rewrite->flush_rules() to flush the rules and have your changes come into effect

search engine friendly blog urls

Hey i am in the process of making a blog for my site, i have decided to make my own rather than use a 3rd party, however i cannot figure out how to make the url's seo friendly atm they are like this /blogpost.php?id=15 however i want it to be /the-title is there any way to do this without htaccess? atm i am using this to make the urls'
echo '<a href="blogpost.php?id='.$row['id'].'">';
Then i am using _GET to get the id and then get the information from the database, i could replace id with name but that gives me /blogpost.php?title=The-Title
Thanks for the help.
In your htaccess you put something like this (if using apache)
RewriteEngine On
#ignores real files
RewriteCond %{REQUEST_FILENAME} !-f
#ignores real directorys
RewriteCond %{REQUEST_FILENAME} !-d
#ignores symlinks
RewriteCond %{REQUEST_FILENAME} !-l
#forwards the request onto index.php with the orignal querystring still intact
RewriteRule (.+) index.php?url=$1 [QSA]
Then any thing after the / will be forwared to the $_GET['url'] in index.php.
Then in index.php you can have a bootstrap class that will explode this by /.
Then you can load the appropriate class based on the first element in this array.
For example if the url was /articles
it would load the articles controller based on the /articles
Or if the url was /articles/about-something
The articles controller will load the article by fetching an article from the database with the about-something slug.

PHP+.htaccess: How to create URL like with ID+name of an article?

I am thinking how to create the routing system like this:
http://website.com/something/12345-my-new-car
which says that in URL is placed the ID and slug name of an article.
My current setup is the following (http://website.com/something/12345):
RewriteRule ^something/(.*)$ index\.php?id=something&something_id=$1 [L,QSA]
Now I would like to add the slug name behind the article ID.
First idea - manually build it - something like:
link
And then to get the current URL and with explode() parse the url (/) and then again explode() for parsing - in the 12345-my-new-car => which return me 12345. And now I would just pick out the respective article.
But I don't really like this way, it's a bit clumsy.
Is there any better way to achieve this goal?
This my my .htaccess file :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z-_0-9_/]+)/?$ index.php?args=$1 [L]
In my code I use this method :
$agri = explode("/",$_GET['args']);
And I have table with my argumants. Now I can make with this all.

Output Webpage (Variable URL) with Database values, using PHP Template

I did some searching around, but I'm not very experienced in PHP, so I don't know the correct Terms/Words I am looking for.
However, I do have a good idea of what I want to do.
I have a website with many images, each image has a database entry (MySQL).
I would like each image to have its own landing page, based on its database values. (Title, Category, Tags, etc...)
I don't want to make a separate .php file on my server for each image. (Eg: pinkflowers.php, redroses.php, etc...)
There should be 1 PHP template file, that outputs the webpage for ALL images, based on the URL (variable) that the user visits.
So if someone visits "mysite.com/pinkflowers.php", the page should be output with the variables of pinkflowers from my database.
However, the file pinkflowers.php doesn't actually exist, only template.php exists, which would be the "blueprint" for all the images in my database.
I would like the .php to be removed from the URL in the browser.
"mysite.com/pinkflowers.php" => "mysite.com/pinkflowers"
I already have code that does this with my existing pages (below); I'm not sure if it will also work with these "imaginary" pages.
(.htaccess)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
If the user tries to visit a page that has no related database entry (eg: "mysite.com/430jfif0ij"), they should be re-directed to the homepage.
I'm not expecting anyone to give me ALL the answers, but please at least guide me in the right direction to begin making such a template.
Thanks!
I was able to solve my own question eventually, in the mean time that no Answers were given.
I followed this Tutorial here:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
My .htaccess contained:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^category1/(.*)$ ./template.php?image=$1
RewriteRule ^category2/(.*)$ ./template.php?image=$1
RewriteRule ^category3/(.*)$ ./template.php?image=$1
etc...
category# - being the "folders" that my images are categorized by.
I did this on the template.php to extract the Variable from the URL:
$result = mysql_query('SELECT * FROM image_list WHERE image_name="' . mysql_real_escape_string($_GET['image']) . '"');
To redirect the visitor to the Homepage if they enter an invalid URL/ID, I used this:
if (mysql_num_rows($result)==0) {
echo "<script>window.location = 'http://mysite.com/'</script>";
}
So the combination of all these methods achieved exactly what I was looking for.
Let your .htaccess be something like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ template.php/$1
Then (on template.php) use $_SERVER["path_info"] to determine the path the user has entered (i.e. /pinkflowers) and query the database accordingly.
The reason I like $_SERVER["path_info"] is because it doesn't use (or pollute) the GET variable space.

Categories