How to arrange delete process in project - php

I have big project where I need to arrange delete of several things like clients, orders, products etc. But theres a point that I need to make an archive for restoring.
So which solution is best.
I have researched and some my ideas were not successful.
1. First was make all rows with status is deleted. But it makes problems in selections and making program work slow.
2. The idea was making separate tables for deleted items but it made problem in orders point as I couldn't manage relations between not deleted orders and deleted clients and products.
Please if theres some ideas how can it be solved ping me.

Well your first idea is kind of the best here. It should'nt make that big kind of problems in selection as you always got the WHERE parameter aviable (though this will take some time and work). Its most easy to restore all of your data then.
And well it shouldnt really slower your programm as databases are build to handle big numbers of data. You should look to optimize your queries first, thats mostly the problem. But like i said your first idea was a good way you were heading

you can add a field in your table call it "visible" that has two values 0 or 1.
then you can use a query like this:
SELECT * FROM tablename WHERE visible=1 // shows all records that has this value, now if you want a record deleted(hidden actually) go to your table and change the field visible of the record to 0, now it won't show on your page but still exist in your table, to show this record again change visible to 1, hope this helps.

Related

Store row count in database or get dynamically

Say I have a table full of comments, each from different users, and I want to count how many comments each user has. Should I have a separate table with the count, and update that on creation/deletion of comments, or should I query the count every time?
I feel like the latter is better, but I want some more experienced input on the matter. Thanks.
Following the good old YAGNI principle, I would suggest you go with the simplest solution for now, which is just counting the number of comments as needed. This is just pragmatic coding.
If, down the line, you find this is causing even small performance problems, then you should replace it with a cached value using a stored procedure or similar, but chances are it will serve you just fine.
So, I realise this probably isn't the clear answer you want, but: if you're making something small, go with the easy solution (counting); if you're making something bigger, go with the easy solution (counting) then upgrade to the harder solution (storing a value) if you find you need it. If you know what you're making is guaranteed to be big (lucky you!) then fine, go straight for the harder solution.
Note: I've said "harder solution" but as you probably know it's only fractionally harder than the easy solution.
After a commnent is created or deleted you should count right in the hour.
If you count every time you will make some desnecessary querys.
Doing a MYSQL count for each user comments would be the easiest way to go about this as below :
$userid= ? ;
SELECT COUNT(*) FROM comments WHERE userid=userid
Updating the table every-time will result to multiple MYSQL calls each time you need to query the comments.
Alternatively you would create a column for comments count in the user , and add the comments for that specific user each time he/she adds a comment , then just querying the user you will have the comments for that user

How could I organize my database so that I can move items up or down?

I am creating a bit of software where people have birthday lists and they can put items in their list, and it’s viewable by other people. I have a table in my database where everything is organized called birthday lists. It is arranged like this:
An example row would be:
The person field is who owns the item, and all the items for everybody are in the same table. The id, name, link, and description are pretty self explanatory. The number field is how it is shown when you view the list page, so number 10 would appear above 0, and the priority is irrelevant to this question. This is an example of what happens when I view Jimmy’s list in my browser. (don’t worry, I will arrive at my point eventually)
I’m adding a lot of pictures because I never make any sense and this is the best way I can explain things :)
Anyway, here is another picture showing an example database.
If I viewed Russell’s list in the browser, it would look like this:
Notice how the $$$$ item is above the stapler because it has a higher number. That’s what it’s for. If I was logged in as Russell viewing this list, I would see buttons saying Move Up and Move Down that increment and decrement the number property on that item. I shall demonstrate my problem with an example scenario.
Let’s say Jimmy logs on and he has a lot of list items. He moves some up quite a few times, so it gets that the number value on one item is way above that of all the others. He then moves another item up, reloads the page, and thinks it didn’t do anything, because the number value of the second item is still less than that of the first. I hope that makes sense. Another problem is that if the number value on two items is the same, it is a huge issue. The point is, the way I have it isn’t going to work.
Does anyone know of a way I could get this number thing to work properly, and tell me how I could make it so users could move items up and down on their list? (also, if my database is organized badly, I’d love to know too :))
Thanks a bunch in advance!
Ending information, in case you need it:
I am building this with PHP 5 and MySQL
This is on a remote Linux server with GoDaddy
When you move an item up or down by one 'move' swap the 'number' values of the item being moved with the 'number' value of the item it is jumping over.
I would handle it this way:
For moving/sorting your items, you could use jQuery UI sortable for example.
You can get the order by it's method serialize().
Send the serialized data to your PHP script and swap/update the changed positions (number in your table)
Update your dataset in your database.
To SELECT your sorted data use ORDER BY number DESC (more)
Good luck! :-)

Resorting id field in database after deleting data

I have a problem. I created a database (my_database), created a table inside it(members), and created fields (id, name, lastname, address). The id field is set to auto-increment. Now I have 5 rows, which means id has a value of 1, 2, 3, 4 and 5. When I delete id 3, then what's left is '1 2 4 5'. My question is what should I do to resort '1 2 4 5' to '1 2 3 4'? Any help would be much appreciated.
Update: I have to do this resorting because on my site I am displaying all the members using loop and accessing the id number. Or is it even possible to get the id of a data? Example I have data this data: id=2, lastname=clyde, address=switzerland. How can I get the id value just by the lastname value?
You really don't want to do that for a number of reasons.
First off, things might go wrong if a page is open. Let's say a user is on the edit page of the entry with id = 4 when you delete the entry with id = 3 and update the ids as you suggest. Now, when the editing person hits submit, the id will be taken from his page and he will be updating the new entry 4 instead of the old entry 4. Now this is a blunt and simple example, but beyond those there are lots more things that can go wrong at this level.
Secondly, there is a lot of work involved when programming this. First, you need to update your columns. Then, you also need to make sure MySQL knows what number to give the next entry. And then all of that has to work in an environment that makes sure we don't get quite as many problems with the point mentioned above (which is quite a hard thing). This is a lot of work compared to the alternative of not doing anything.
The third problem is that there is a huge performance overhead. Imagine having a database with thousands upon thousands of entries, and then removing an entry with a low id. Suddenly, all entries with higher ids have to be updated. This might well mean that your site becomes unresponsive because it is doing this task and can't handle too much else at the same time (in fact, in order to make sure that we don't get problems like in the first point, we have to make sure that we don't do anything else at the same time (or make sure we work on different copies of the data or something) because we could end up with a result that comes from during this whole update process.
My suggestion would be in line with what others are saying: just leave it as it is and do not worry about this. auto_increment is meant for just one purpose: giving each value a unique identifier easily. Use this identifier to identify and refer to the same entry only. Perhaps one could also make a case about sorting on these identifiers, but no further than to have a certain order (and even then people will disagree with this use of it).
Instead of trying to update the ids, we should look for another place to solve this problem. If the problem is purely how you feel about it, that's easy. You may not feel good about it, but you just need to convince yourself that updating all those ids is not the solution you are looking for. If you use the numbers elsewhere, the problem can be a little more complex to solve. However, there is always the possibility of using PHP to generate numbers for each entry, which most definitely is the logical place to do so if the numbers are used in the generating of your html content. If you provide more details about where you use the sequential numbers, a look could be taken at how to solve it in that case.
You could do an UPDATE query but mostly developers just leave the id's as is. You can do the numbering in the PHP if it's important to you.
You can either manually update id value by the following statement :
update members set id = (id-1) where id > 3;
or my advice would be to leave as it is. Its not causing any ambuiguity.

Facebook-like Notification System

I wanted to create a notification system like the facebook one and thinking of the structure to realize this system.
I've two tables: notification (id, uid, query, date) and notification_unread (id, nid, uid, date).
I tought to use this the following way:
If I make a comment somewhere, I'll add: my uid, $_SERVER['QUERY_STRING'], NOW() to notification.
With notification_unread I wanted to insert data of posts that hasn't been read yet. In this case nid should refer to the notification id. And only if there may be another new post, data will be inserted in this table. Everytime something has been seen by the user, I'll delete the specific entry from this table.
Well, however I couldn't really figure out, if this approach (including DB Design) of me is somewhere somehow "the wrong" or "too complicated" way
and I couldn't figure out to realize this, because I can't think of a logical way WHEN and HOW to insert data to notification_unread. For instance I don't want to notify myself that I made a new post, after I made it. But I guess I'd still have to insert data to the table?
So, the thing is that I'm trying to insert as much data as needed and realize this most effecient.
I hope you could follow me and would really appreciate any suggestions!
At first glance, it may seem very simple & clean solution but its a downfall afterwards.
As podiluska pointed, its better to have a check field.
You can easily display unread one, but what will happen once a user has check it? You will have to move it in read table which will increase your disk I/O which is the slowest process in any system. You can simply query off last 5 notifications or something.
You may want to create an archive table to move old notifications to archive table. Here important thing will be to maintain that notification table since all the updates will be here. The bigger it becomes, more the performance will suffer. Its a long time effect but you might want to consider the solution now rather than later.

How to manage working/open project records in mysql with more than one user?

I have a few ideas, but thought Im likely touching on a common topic (though I didnt find it)...
Weekly we have about 500 records a single user has been processing, we use pagination set to single record, when the record is updated the page reloads the next record. Works simple and easy. -Codeigniter, PHP.
Ahh, but now I want multiple users to work on the same group of records without grabbing the same record, until they are exhausted, the records not the users.
Is there a general convention for doing this?
off the cuff, I'd likely change the status of the working record to pending as its being loaded. Am I missing something? Is there a better/cleaner way?
I don't really see any other solution than setting some sort of "lock" on the record. You might also consider locking say 10 records ahead for each user and in that way create a sub-queue for them. Though in this case you should probably also have a timestamp on the lock and a cron-job to unlock them after a certain period of time (so that someone doesn't go on vacation with 10 records no one else can work on)

Categories