I've built a URL shortener using the Bijective Function algorithm as stated in Stack Overflow. My database table has two columns; ID and URL. For every input URL, the ID is automatically incremented its value is converted to base62. This value forms the slug for the shortened link.
Example: If ID is 42 then its base62 form is g (may differ according to the alphabet set). So the shortened URL is https://example.com/g.
Now I'd like to add custom URL support where the user chooses a custom URL slug. I could create another table to store custom URLs and then check both tables for a matching slug. But this seems rather inefficient.
Can anyone provide an efficient solution?
P.S. I'm using PHP and MySQL.
You could do the reverse mapping of the slug to the corresponding ID it would occupy if it was naturally generated, just keep this in mind:
Before increasing your ID variable you can make sure the entry it would increase to is not already populated by someone who entered a slug which corresponds to that ID, if that's the case just keep incrementing the ID column until you find a new unique value.
Related
I have an API that handles text translation from English to other languages. Translations need to be done once and be saved in the WordPress database for future use.
Example: A single post will be translated into 4 languages which means 4 different versions of a single post.
A user can only access a single version depending on the language he/she has selected.
The current idea
Create a database table (translated_table) where translated posts will be stored.
At this point, I'm stuck on how the posts will be displayed to the user
Question:
Is there a better way to save, map posts and retrieve translated posts such that if you access a Post whose id = 7 in English you can switch the language and still read its translated version stored in translated_table?
I would set up a table to hold your languages (wp_languages), and a cross table (wp_postsXlanguages) with a foreign key (wp_posts_ID) to record the translation locations.
Example:
When you want to retrieve a post in a given language you can use the cross table to locate it. Note that ID's 1 and 4 in the cross table are optional and entirely up to you.
In my back end, I have a table in which I store events (activities).
After some research, I've decided to store the primary key as an auto-incremental primary key because it appears to be significantly faster than storing it as UUID.
Now in my front end, I want to create a route to show the profile of an event.
Instead of having a route like: xyz.com/events/1.
I would prefer to hide the id by showing a unique string in which the event name is shown, but in a way that it forms a unique string that I can query in my database.
I've noticed that quite some websites, (incl. Facebook) are able to do this.
They show eg. firstname.lastname with a number after that.
What is the best way to achieve this?
How do I generate a unique string from the event name that I can query?
In events table i would store additional unique slug (created on event save action).
f.e. "Some great event" has slug "some-great-event".
If the slug is not unique then additional distinction is needed f.e. "some-great-event-2".
You use the slug to prepare a link and as an identifier to query database record.
There are many libs to generate slugs, check github.
I am trying to generate a unique slug for pages based on the title of the post the user enters so, say a title is "Once upon a time" this will translate into Slug->once-upon-a-time, however I want people to be able to have matching titles so if another person comes along with the same title I then want the slug to be Slug->once-upon-a-time2. How can this be done in laravel or just php. I essentially just want to try and save but if it fails then increment and then save again. Thanks for any info
This isn't exactly an answer but why not separate by user and allow them to have identical post names? For example 'www.example.com/users/{user_id}/posts/once-upon-a-time', would avoid this issue completely and allow for checking out individual users writing.
I've built a webpage that, in its products page has this prototype for its url
http://www.example.com/products/show/some-really-nice-product/512
The last bit of the url is the product id, which serves the sole purpose of searching the db for the product.
But, is it possible, using routing, to be able to hide that part and still be possible to search the product by its id?
Like
http://www.example.com/products/show/some-really-nice-product/ ??
Thank you
If you don't want to put a number in your URL, you can't later search by a number/id :)
So you could add a VARCHAR field to the table, say "urlized", and use "some-really-nice-product" as its content for the given ID (you'll need to 'urlize' all your product names and make them unique). Don't forget to index your new field...
Then, you could access the product page using this URL which is very SEO friendly (ends with ".html"):
http://www.example.com/products/show/some-really-nice-product.html
...by adding a route to CodeIgniter, somehow like this:
$route['^products/show/(.*).html'] = 'products/show/$1';
In your controller/model, search the database for the string that is passed to "show" method, instead of the ID
if you hide id you can't retrieve that from the url anymore
if you want to hide that anyway just do:
$route['products/show/(:any)'] = "products/show/$1/$2";
I have a link in my system as displayed above; 'viewauthorbooks.php?authorid=4' which works fine and generates a page displaying the books only associated with the particular author. However I am implementing another feature where the user can sort the columns (return date, book name etc) and I am using the ORDER BY SQL clause. I have this also working as required for other pages, which do not already have another query in the URI. But for this particular page there is already a paramter returned in the URL, and I am having difficulty in extending it.
When the user clicks on the a table column title I'm getting an error, and the original author ID is being lost!!
This is the URI link I am trying to use:
<th>Return Date</th>
This is so that the data can be sorted in order of Return Date. When I run this; the author ID gets lost for some reason, also I want to know if I am using correct layout to have 2 parameters run in the address? Thanks.
Yes, the layout is correct. Of course it's possible to have even more than 2 parameters in the query string
you have to check resulting query string (just take look into your address bar after click), there must be something like viewauthorbooks.php?authorid=22&orderby=returndate. If number is lost, you have to check it's source - $row['authorid'] variable.
you have to use & and sanitize your output, but apart from that it's correct ;)