after multiple delete maintain order column - php

in my project i have to maintain a order of every record and if i delete in between i have to maintain order in a form that order don't have any gape like after 3 next is 4 only... not 5 or 6......my display like.
if i delete multiple record that have Rule ID are 1,3 and 4...then my rule ID 0 remain 0, 2 becomes 1, and 5 becomes 3...like that i want. i am deleting record base on id.
id rileid
1 0
3 1
5 2
6 3
11 4
14 5
how to do that? please help me.

Related

PHP + MySQL - ORDER BY LIKES

So I am using mysql to store likes on Minecraft Server Pages as a way for them to advertise. However, I am storing these likes in a separate table:
id user_id server_id
1 1 1
2 1 24
4 2 22
5 2 22
6 2 1
7 2 4
8 2 5
9 2 6
10 2 17
11 2 18
12 2 21
13 2 24
Insert code:
INSERT INTO likes (user_id, server_id) VALUES ('".$user_id."', '".$server_id."');
But I am currently sorting them by date added (recent first) but I would like to know how to sort them by likes.
You should put a counter column on your servers table (lets say: likes_counter) and then, every time you add a like to the likes table, you increment this counter. Put a index on that column tooo!
INSERT INTO likes (user_id, server_id) VALUES ('".$user_id."', '".$server_id."');
UPDATE servers_table SET likes_counter = likes_counter+1 WHERE server_id = '".$server_id."';
Now, when you select the servers, you only need to do a ordered by likes select:
SELECT * FROM servers_table ORDER BY likes_counter DESC;
Here i'm assuming your table of servers is named servers_table, replace it with the name you gave to the table.

Laravel get last row for each items

I use Laravel Eloquent and I have this table:
id client_id project_id status_id
1 1 1 1
2 2 1 1
3 1 1 2
4 2 1 2
I want take the last row which insert for each client. How can i do?
Since Laravel offers method 'first' to take the very first matching record, how about first you order by descending and take the first row?
$affectedRow = TableName::where(condition)->orderBy('id', 'desc')->first();

Prevent duplicate row values

I've done some digging and I can't find an effective way to prevent duplicate entries based on my needs. I need columns 2 (proj_id) and column 4 (dept_id) never to be the same, as each dept would only work on a project once. So, rows 1 and 4, 6 and 7, and 14 and 15 shouldn't be allowed. I'll keep digging as well.
summary_id proj_id hours_id dept_id date_entered
1 8 3 6 9/9/2012
2 2 2 6 9/9/2012
3 1 6 19 9/9/2012
4 8 3 6 9/9/2012
5 2 5 17 9/9/2012
6 7 2 5 9/9/2012
7 7 2 5 9/9/2012
8 2 5 17 9/9/2012
9 7 4 17 10/10/2012
10 3 6 1 10/10/2012
11 5 1 15 10/10/2012
12 4 4 3 10/10/2012
13 3 5 1 10/10/2012
14 8 2 13 10/10/2012
15 8 2 13 10/10/2012
Before applying unique combine key to your table, you have to remove duplicate records first then apply the following sql command:
ALTER TABLE your_table_name ADD UNIQUE (proj_id, dept_id);
Define an unique key on both columns
ALTER TABLE `your_table` ADD UNIQUE (`proj_id`, `dept_id`);
Looks like you are new to php and mysql. So here's the easiest way to do it.
Log on to PHPMyAdmin
Select your DB and your table.
View the Structure of it (clicking the button on top of the screen).
Check the two fields (proj_id and dept_id) using the check box on the left.
At the bottom of the table you should find the the words "With selected" and in front of it some actions. Select the option to make "Primary".
Of course, if you have duplicate entries first delete them.

SQL - Filtering search results

OK – need some help here – might have bitten off more than I can chew here – but I’m looking to write a SQL query that always returns a minimum of 10 results. If the first condition is applied and the result set exceeds 10 results then move on to the next condition etc..
Example
Find all the small red plastic toy cars that cost less than £5.00.
I always want to show a minimum of 10 items on the screen. Want to search on “small” then on “red” and then on “< £5.00”. If the query returns more than 10 items then continue to filter on as many tags as possible (i.e plastic, toy and car) – the more matches it can make the higher it should be ranked (i.e if a products has all 3 tags associated with it – it will be at the top of the list, if a product only matches 1 tag – then this will be lower down the list.
Price Table
ID Price
1 £1.50
2 £2.50
3 £6.00
.
Colour Table
ID Colour
1 Red
2 Blue
3 Yellow
.
Size Table
ID Shape
1 Small
2 Medium
3 Large
.
Products Table
ID Description price_id colour_id size_id
1 Item 1 1 2 2
2 Item 2 2 2 1
3 Item 3 1 1 1
4 Item 4 3 2 3
5 Item 5 3 1 2
6 Item 6 1 1 2
7 Item 7 1 1 3
.
Tags Table
ID Description
1 Shiny
2 Plastic
3 Wood
4 Toy
5 Disney
6 Animal
7 Car
.
Items_Tags Table
ID tag_id product_id
1 1 1
2 4 1
3 7 1
4 2 2
5 3 3
6 4 3
7 7 6
8 7 7
9 7 2
Quite a long example - but I hope you get the point. I have wondered whether there would be any benefit to putting all the filters within the tags table - i.e the price, colour and size and then would only have to search against the tags table.
Any ideas anyone?
Thanks
It depends on your usage: filtering integers is faster than string (by the way, are they indexed?) but JOIN is also time consuming. So if the code outside the sql works with the integers, keep them (and don't join if not required to show the text)

How to deal with a large mySQL tree

I am working on a project which user registration multiples like this.
1st Month -> 1 user
2nd Month -> 4 users comes under the above user
3rd Month -> 16 users (i.e, 4 users comes under each 4 users above )
4th Month -> 64 users (i.e, 4 users comes under each 16 users above )
eg:
1
2 | 2
8 | 8
32| 32
and continues...
Please give me an advice, how to store this in database.
Thanks in advance.
Assuming user_id is your primary key, create a parent column that contains the user_id of the parent user. For example:
user_id parent
1 NULL
2 1
3 1
4 1
5 1
6 2
...
You will also want to create an index on the parent column so that you can quickly do a reverse lookup (i.e. find all children of a given user).

Categories