Saving Wordpress translation in database - php

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.

Related

Codeigniter doing translations from the database

I have been looking around to see if I can learn how to do translations that come from a database.
My aim is to have an admin do the translations for each label in an admin console and when a user selects a language, the site will reload and it will load the translated options from the db instead of having multiple view files in different languages.
So far what I have is adding an extra segment in my url to accommodate different languages ex. www.testwebsite.com/Application/en-US/content
I plan on reading that segment, storing it into a cookie or session and when a new page loads, it retrieves that option from the cookie or session and always loads the right language.
As for the db structure, so far I have
id | label | language | translation
The main issue now is how do I load this properly? My experience so far has taught me that when I want to load a bunch of items from the database, it will come in an array form and in order to access it, I must add a for loop/ foreach loop.
Is there a way to replace the labels without them being in a loop? Or is there a better way to do what I am trying to achieve?

Adding custom URL support for my URL shortener

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.

Auto increment unique column if value is taken

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.

identifying dynamic pages (created by PHP from database)

In case this question has been asked before just refer me to the answer and I shall be greatful.. it's a very common problem however I have not been able to find solution form google
I am developing a website in PHP using codeigniter. I have articles /Posts. These posts are located in a database and when ever a user demands to see an Article, all its relevant data is pulled from database using a unique "Content_id" and a page is presented to user dynamically.
However once the data is dispatched to the view and an html page is created from that page I have no way of finding which "Content_id" was used in generating that page. Now in case user comments on that page I dont know which "content_id" to save in the comments table along with this comment so that next time when this article is displayed then all the relevant comments can also be displayed.
some of the solutions that I thought are as follows
Can I use session to store this data? (However I am not using sessions for those users who are not logged in ... So can I use sessions for all users )
Will using a form on the page with hidden fields be a viable option...
How do Wordpress or other CMS are handle this problem?
To achieve your goal, your table needs four columns:
Unique row number
Content
Post number
Post date (timestamp)
The unique row number is the primary key. Auto_increment it.
The content is the posted message.
The post number is the row number of the post that starts the conversation. Make it the same as the row number in the conversation's first post. It is the row number of the post being responded to. All responses to the conversation will have the same post number.
The post date is a timestamp. It permits you to organize the order of the posts in a particularized conversation.
Each conversation, or sub_conversation, follows this routine.
Usually, the post id is passed as a GET variable anyway. If not, a hidden input is fine.
I am Very sory to Know that no one answerd my question except one persone
Any ways here is the solution that I have figured out in 1 sentence **
The pages are not required to be numbered But the content needs to be numbered... thus each content (Article, post etc) has a content id and that id needs to be considered.
**

Adding new Tables in an Open Source Script (Beginner Question)

I'm still a new php beginner, in fact I just started learning couple a days ago.
I love to learn and implement at the same time on real scripts because that's motivate me to continue learning.
Yesterday I put my hands on an open source script (File Uploading Script)
And the script is pretty simple, and I want to create categories in the script
Example: When someone would like to upload he can choose the categorie of the file so it can be added there. I should create the values of the categorie (either in MYSQL or from Control panel)
What I want: I want you guys to tell me what exactly I should be doing. for example U should create something and something to do this. and that's all.(no accurate steps)
I know how to deal with mysqls and with php but I would like to get hints of what every programmer will should do that is all.
[DB Part]
You'll need to create a table contains 2 or 3 fields:
catID field (would be a primary key & autoincrement)
catName field (contains tha category name)
you can add a parentID (to have the catID of the parent category if there is any), imho, no real need for it as a beginner
you will need to link this table with files table taking catID as the foriegn key
[Form Part]
You can either list the categories in the uploading form, to be in a combo box, dropdown list, or even a check box, then insert it in the DB within the form,
or you can let the you user chose the category first before you show him the uploading form, then in your script you register the the catID in a session, & later when you upload the file you use that catID value
see?

Categories