Auto increment unique column if value is taken - php

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.

Related

Architectural decision regarding back end

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.

Pass foreign key to another page

I have two pages, one is a new player page which takes information from a user via text box and adds a new entry to the players db and creates a new auto-incremented primary key called playerID. This is working fine.
I then have another page called stat input where the user will enter statistics for an individual year for an existing player. I need a way for this second page to know the playerID of the player whose information is being added. I am not unsure of a good way to do this. Any ideas are appreciated.
Edit: I guess I wasn't very clear. The user may be going directly to the stat input page to add a new entry to an already existing player. The user will not know the playerID and likely only the name of the player.
There are a few ways you could do this. Once you have the player ID, you can store it in a PHP session variable (if you are open to using server-side sessions). You could also pass it in a GET or POST parameter to the second page.
Edit:
As per the additional information you gave, I would go with a dropdown box with each player name. In the box, the "value" attribute of each can be the primary key ID of the player.

Database Concurrency Problems

I'm currently developing a database/website server interface to facilitate inputting data for a data collection project. There are two types of additions being made to the database: A and B here. Some of the tables in the database that handle these entries are as follows:
dcs_projectname_a
dcs_projectname_b
Each of these have tables for all the required input fields in addition to things like creator, timestamp, etc.
The pages on the website facilitate three different options: add, view, and edit. Each page for each type of entry performs the respective function. That is, the add page adds, view page views, etc.
I am just about done; however, there is a major challenge I haven't really confronted yet: concurrency. There will be multiple users adding content to the database at the same time. Each entry is given its own specific id and there CANNOT be any duplicate id's. That is, the first a entry is A000001, the next is A000002, and so on.
On the add and edit pages, there is a disabled field for the user to view the id for other uses when physically documenting entries.
What I need to figure out is how to implement concurrency management so that when users are concurrently adding a's that they will not be under the same id and row.
The add page automatically generates the id to be used by accessing the database's most recent id and adding one.
My thought was to create a new row in the table every time the add page is opened and give it the calculated id. Then, when information is added it performs a modification to that existing row. This way, if another user opens the add page while another entry is currently being added it will be given the future id, not the same one.
With this method I need a way to delete this entry if the user leaves the add page or closes the browser. Then, I also need other users with open add pages to automatically update their id's to one less when the first user (or any other user less than the most id being used) leaves their add page and cancels the addition.
This is kind of a complicated explanation and if you don't understand let me know and I'll try to answer as best as I can. Any help is much appreciated!
Thanks
There's a number of solutions to your problem, but you seem to have made things harder by having your application generate the record IDs for you.
Instead, you could just be using MySQL's AUTO_INCREMENT functionality to automatically generate/increment the record ID for you (upon insert). MySQL will ensure that there are no duplicates, and you can get rid of the extra database call to retrieve the most recent ID.

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.
**

Extending URIs with 2 queries (i.e. 'viewauthorbooks.php?authorid=4' AND 'orderby=returndate") Possible?

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 ;)

Categories