Question:
how to make codeigniter dynamic url search engine friendly?
Example: 1
My Current URL: After selecting menu called "Articles"
http://localhost/lw_user/home_control/getMenu/52
Expected URL:
http://localhost/articles
Example:2
My Current URL: After selecting sub-menu called "thehindu" under menu "articles"
http://localhost/lw_user/home_control/getPage/6
Expected URL:
http://localhost/articles/thehindu
NOTE: This is dynamic URL and contents are fetching from database
What you are talking about is called as slug.
So, how to use slug?
Will explain with an example:
URL - http://www.example.com/products/apple-iphone-5S-16GB-brand-new/
1) Assuming you are having a product page and ofcourse product page needs some data from URL to understand which product to display.
2) Before we were querying our database using the id we are getting from the URL. But now we'll do the same thing (querying our database) just replacing id with slug and thats it!
3) Hence adding an additional column in your database named slug. Below will be your updated product database structure (just an example).
Columns Values
id (int(11), PK) 1
title (varchar(1000)) Apple iPhone 5S 16GB
slug (varchar(1000)) apple-iphone-5S-16GB-brand-new
price (varchar(15)) 48000
thumbnail (varchar(255)) apple-iphone-5S-16GB-brand-new.jpg
description (text) blah blah
...
...
I've also answered on slug before. Check if it helps.
How to remove params from url codeigniter
Codeigniter - SEO Friendly URL Structure (Slug Implementation)
This has to be done using the routes class.
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
I would suggest you look at the user guide for more info.
https://ellislab.com/codeigniter/user-guide/general/routing.html
Related
I'm trying to receive all portfolio entries from the database. I want to list this and also link the WordPress path to it. This is the SQL query:
SELECT id, post_title, post_name FROM 1bncopo2_posts WHERE post_type='portfolio';
post_title could be the following:
Mercedes-Benz AMG C63
Mercedes-Benz S500
BMW 650i XDrive
and now post_name for the first entry of the above list is mercedes-benz-amg-c63. However, I found that in my database table, multiple portfolio vehicles (even vehicles by BMW, etc.) have post_name set to mercedes-benz-amg-c63. My idea was to generate the URL to the portfolio item using post_name as an appendix to the wordpress path. TO BE CLEAR: This is going to be a seperate application reading from the WordPress installation's database and trying to make up the corresponding links to the portfolio items.
Now that post_name is not UNIQUE or something I could use, I have two questions:
Why would the WP database allow for duplicated post_names?
How can I get the post (portfolio)'s URL, if not by post_name?
It is impossible to repeat the link, if you see the link from the beginning you will find it different.
If this post (mercedes-benz-amg-c63) for custom post type called (portfolio) you see URL like:
www.domain.com/portfolio/mercedes-benz-amg-c63
But if its post type (post) you see URL like:
www.domain.com/mercedes-benz-amg-c63
Then as you see the URL not duplicated
I am writing some little hacks for wordpress by giving myself some used cases. I think that is the best way to learn this
I have created a scenario where I am checking if the page name is 'x' then fetch contents of posts categorized as 'y' in the page x. Works fine as long as I hard code the page names like this
if(is_page('x')){
query_posts('category_name=y');
}
Now I am thinking what if I generalize the x and retrieve all the categories and push them in an array. Iterate through the array and look for match like
if(pageName == postCategoryName){
query_posts('category_name=the correct category');
}
I believe I would basically have to create the post categories with the same name as the page name
Conceptually I am good with this but when it is coming to the syntax I am getting a bit lost. How should I approach this?
This could be achieved by getting the slug of the current page and then finding the same slug in a category.
e.g. if you were on the Geography page
$slug = basename(get_permalink());
$slug would contain geography so you can query that by:
query_posts("category_name={$slug}");
And that should return the posts in the category.
Is there a way to call field rows in a URL without using the column name??
So I currently have a posting site where users can select category or subcategories of choice from drop downs, how it's currently setup my site outputs links to the categories chosen such as..
topics.php?category=Food&sub_cat=Pies
topics.php?sub_cat=Pies
This allows users to go to either one of the links, or both
topics.php?category=Food&sub_cat=Pies
To give more functionality I am looking at adding textboxes instead of drop downs, the problem is users will more than likely enter the data in different boxes than other users, ie.
User 1. catbox: Food subcatbox: Pies
User 2. catbox: Pies subcatbox: Food
So in this case my current URL system won't return accurate results, so my question is would there be a way where "category" or "subcategory" could be replaced and just put the results together without them being listed in 2-5 different fields therefore not returning all the results that = to that value? "food" or "pie" in this example.
topics.php?xxx=Food&xxx=Pies
or
topics.php?xxx=Pies&xxx=Food
Looking at So homepage if you click "php" it will put php in the URL, click mysql and it will put "php+mysql" that sort of thing.
you can use parent child method in your database.your table would be like this
id - parent_id - category_name - depth
when you want to insert a data to your table it's depth will be one plus it's parent depth
when someone post to your page you first take query witch of the inputs has most depth then that will be your subcategory.
Calling field rows via parameters in your URL may be a very bad idea. It's a perfect way to allow a massive SQL injection attack. So, the answer is probably "yes, but HOLY MOLY PLEASE DON'T!"
Now it may be that your code is parsing these out on the back end and protecting them via any of a variety of methods, I can't tell from the amount of code posted.
I am using the CodeIgniter framework and am having trouble figuring out how to get my urls to display the category name but still have my queries reference the category id.
I have a controller set up called jobs. The structure of the url is "example.com/jobs/listings/category_id" example: "example.com/jobs/listings/3" where 3 would be the category id for "software".
I'd rather the url say "example.com/jobs/listings/category_name" example: "example.com/jobs/listings/software".
The problem I'm running into is that the "jobs" table holds stores the category_id that the job belongs to and not the category_name. I have a separate table that holds just the category id's and their names. I'm not sure how to A.) Write my query and B.) Make an SEO friendly url structure.
My current query looks something like "SELECT * FROM jobs WHERE cat_id = 3;
Also, I'm not sure if I should be using mod_rewrite in an .htaccess file or if I should be using the routes.php file in CodeIgniter.
Any help would be greatly appreciated!
-Thanks
Just join to your other table.
SELECT * FROM jobs JOIN categories ON jobs.cat_id = categories.id WHERE categories.name = ?;
Make sure to put a unique key on the category name, if you don't have one already.
mod_rewrite is a poor solution to app routing and wouldn't help much here anyway. Stick to whatever your framework provides.
Have a look here http://codeigniter.com/user_guide/helpers/url_helper.html at URL_title()
Convert you category name using the url_title() function making sure you have no duplicates.
When a url is called Look up your category from your model using a simple where statement
Eg
$this->db->from('categories');
$this->db->where('name', $url);
Etc...
Use this to load your page titles etc then use the categoryid returned to do the look up on your jobs table.
i still , don't understand , how wordpress can understand what is this url refer to :
www.mysite.com/about-me/
they are using no identifier
if they using slug functions so how they can retain story information or in other word , how they change back the slugged title to select from database
It processes the "pretty" URL and queries the database with that data. Of course slugs are checked to be unique on creation. Details are in the function url_to_postid() in the file wp-includes/rewrite.php.
If you want to directly obtain the id from the slug you can query the database:
SELECT ID
FROM wp_posts
WHERE post_name = '$slug'
you might need to check wp_posts which is the default name, but it depends on the installation.
This is just a guess:
My guess is that they store the titles in a database, and make sure every title is unique. That way, they can do a look-up by title and know which item is coupled to that.