We Have a table of approval like below:
In above table we have ApprovalId and ApprovalLevel like first level approval and second level approval in ApprovalLevel (For Approving Approval by its level).
I am trying to fetch record in query if specific user ApprovalLevel >= 1 then check its previous ApprovalStatus if it Approved then show its record too.
I have tried many things but still not getting specify record.
In Simple term select record with User if only if its ApprovalLevel=1 or its previous ApprovalLevel ApprovalStatus=1.
Please help me on this: [IN this table ApprovalStatus: 0 = Pending and 1:Approved]
I believe this is what you are requesting:
SELECT
ApprovalStatus,
ApprovalLevel,
CreatedOn
FROM
approval
WHERE
ApprovalLevel = 1
OR (ApprovalLevel > 1 AND ApprovalStatus = 1)
;
Related
I am doing a booking cancellation function. I have successfully removed the data in reservation table as shown in the code below. However, I would like to update the status for the timeslots. For the timeslots table, 1 = not available for booking anymore, 0 = available for booking. While the user logs in into the account, they can view the appointment and perform booking cancellation, which is where I am stuck with on updating the timeslots to make it available for booking again.
For the reservations table, its status means visited or not visited, which is not related to this question. However, the reservation table is the table which shows the information to the users to view their appointment history, and here is where the cancellation feature will be provided, using a button.
The problem is that I do not know how to connect between the reservation table and the timeslot table. I need the timeslot table to show the updated status is 0 after cancellation happens for a particular day instead of continue showing 1. Below I will attach the database in SQL and the codes implemented.
Timeslots database:
Reservations database:
public function destroy($id,Request $request){
//delete reservation in records
$reservation = Reservation::find($id);
$reservationDelete = $reservation->delete();
//how to update the timeslots status to become 0 from timeslots database?
return redirect()->route('my.appointment')->with('message', 'Appointment has been cancelled successfully');
}
Maybe you should use MySQL transaction to do this both modification in 1 transaction.
I recommend to use 2 different query in 1 transaction.
Your description about this problem is not fully clear, sorry for misunderstand.
I want to create a form where the logged author can fill a textarea and then select 1 or more system users from a checkbox
So when every user logs in he Will see that record only if he has been selected in the checkbox before.
How can i do it?
Must i save values of the checkbox in a single record and then split to set the correct data set?
And then how can i show the whole records to the current user as i want him to see a list of records where he is the author or where he is among the users allowed to set that very same record
I know this can be a generic request but the matter is i dont understand how i could do it, so the answer i look for is not "give me the code" but"explain me the main steps i must follow", someone can give me some hints?
How about creating a table named "record" with the following structure:
id author_id allowed_user_id text
Now, when the author with id 1 selects two users with ids 2 and 3, you insert two rows into that table, one for each allowed user
id author_id allowed_user_id text
1 1 2 just a test
2 1 3 just a test
To show the current user the records he authored
SELECT * FROM record WHERE author_id = current_user_id
and records he's allowed
SELECT * FROM record WHERE allowed_user_id = current_user_id
I have a database that looks like this:
When user wants to use a promo code, it first checks if used_promo is null, that means the user has not used any promo code before. (User can only use 1 promo code)
I can do something like this
SELECT used_promo FROM users WHERE id=5
then I can check with PHP if used_promo is null and if it is, I can do
UPDATE users SET used_promo=2, balance=balance+100 WHERE id=5
Is it possible that the user can try to use the promo code multiple times at the same time, the SELECT will return that used_promo is null before it is set to 2 and the balance will be added multiple times?
In the scenario you've described it's theoretically possible to use the promo code multiple times. Use a single query, which will return an id if the update is successful or no rows otherwise.
UPDATE users
SET used_promo=2, balance=balance+100
WHERE id=5 AND used_promo is null
RETURNING id;
Create one more relational table "promo_used", where columns would be:
id
user_id
promo_code_id
timestamp
Once user apply promo code, this table will have new entry. This way, if user tries to apply same promo code again, we can check by searching through this table.
Hope this helps.
As understood you are worried that a user may use promo code multiple times before you update used_promo column. I don't know what your business model is. However, it is possible in a scenario like this:
User enters the promo code. You reduce the price of item accordingly, but you can't update the used_promo field since purchase is not complete. Without completing the purchase user enters the promo code for another product and gets a reduction again. And after doing this multiple time times user goes completing purchases. Since you don't check the validity of the code at the final step user can get all the price reductions.
So
Maintain the validity status of promo code
Always check the validity
Use atomic way to update validity status, used_promo and finalize the purchase. (Transactions)
of promo code and used_promo before finalizing purchase
I want to build a system on the website, that allows users to do some things depend on their rating. For example I have rule for rating value X:
1 post in 3 days
10 comments in 1 day
20 votes in 2 days
for rating value Y, rule may be following:
3 post in 1 day
50 comments in 1 day
30 votes in 1 day
Each night I recalculate users' ratings, so I know what each user is able to do.
Possibilities don't sum or reset on each rating's recalculation.
One more important thing is that admin can fill concrete user's possibilities at any time.
What is optimal database (MySQL) structure for desired?
I can count what concrete user has done:
SELECT COUNT(*) FROM posts WHERE UserID=XXX AND DateOfPost >= 'YYY'
SELECT COUNT(*) FROM comments WHERE UserID=XXX AND CommentOfPost >= 'YYY'
But how can I do admin filling possibilities in this case?
I would log the number of actions of each user each day and use that table to compare.
This table would contain the following fields:
date: the day when the action took place
count: the number of actions took that day
userId: who did this action
action: which action post/comment/vote/...
ignore: boolean, if this is set, admin has reset the values
Checking a rule: SELECT SUM(count) FROM log WHERE userId = XXX AND action = YYY AND ignore = 0 AND DATEDIFF(date, NOW()) <= DAYS
Resetting a rule: UPDATE ignore = 1 FROM log WHERE userId = XXX
If his rating changes the result is still valid (you'll just compare with on other total)
When you create a rules table:
action
limits
days
rating_min
rating_max
You can query for permissions like this:
SELECT action, IF(SUM(count) < MIN(limits), 1, 0) as can_do_action FROM log LEFT JOIN rules ON rules.action = log.action WHERE userId = XXX AND rating_min <= RATING AND rating_max >= RATING AND ignore = 0 AND DATEDIFF(date, NOW()) <= days
So you get a table loggin like this:
- comment => 1
- votes => 0
You do have to update this table every action (create a new row if first action of the day or update the count of the row)
The absence of a rule means no actions have been made, so we can ignore it.
If I understand you correctly you have a user that can post 1 blog, and comment 10 times. Now he/she has commented 5 times and posted a blog. You want the admin to click a button, and now the user can again post a blog and comment 10 times?
It might be a bit of a hack, but you could count the actions that are being reset/ignored, and substract that from the current actions?
e.g.: user has 1 blog and 5 comments. Admin presses "reset", and you save those values.
Now as the user posts another blog, and you check if that's allowed, you'll get
SELECT COUNT(*) FROM posts WHERE UserID=XXX AND DateOfPost >= 'YYY'
And you do something like this
SELECT changes FROM adminTable WHERE UserID=XXX AND type = 'post'
And if count - changes is ok, you're set.
What about having, in the user table, three columns called remainingPosts, remainingComments and remainingVotes? You'll decrease the columns when the user has performed a specific action, and in this way the admin can always "refill" those columns, even above the original limit.
===
Another option is to store the expiration of the permissions in those columns, so that you can reset the permissions just putting the expiry for a certain column to the day before. You can then use your queries to get the remaining number of posts/comments/votes for the user.
I suggest separating the two concerns entirely:
The process of enabling features/possibilities to users
The data model of user features
For example, you could have a simple many-to-many table representing user features:
user_features(
user_id
,feature_id
,source (admin|earned)
,primary key(user_id, feature_id)
);
This makes it really easy for an administrator to disable/enable parts or all of the feature set.
Your nightly job would query relevant tables and grant/revoke features by inserting/deleting from this table.
If you go with this approach, you can actually give the features either based on a rating or specific actions.
A rule such as "3 posts in 3 days", can be implemented like this:
when a user posts, check if the previous post was made within 24 hours.
if yes then
increment counter by 1
record current timestamp
if counter = 3 then
grant feature to user
else
reset counter to 1
record current timestamp
You would need two columns (post_count:int, last_post:date) in some table keyed by user_id.
I'm creating a system where users can write their current status and other users can comment.
If anyone has commented on a user's status the user will get notified, but I'm having an issue coding this behavior.
My desired behavior is this: if i have one comment on my status (by one user, of course):
echo "user has commented your status"
If I have two comments in 2 of my statuses (and the both are from same user):
"user has commented your status"
"user has commented your status"
(the text should be printed twice, each for one status)
If I have 3 comments in my 2 statuses (where the 1st status has 1 comment from one user, and the second has 2 by different users)
echo "More than one users has commented your status"
echo "user has commented your status"
So if I have 2 comments in 1 status (where both comments are different user and NOT the same)
echo "More than one users has commented your status"
To summarize: my system should only echo one time for EACH status notification. If its one comment from one user, or more comment in one status, from one user, you will get:
echo "user has commented your status"
but if comments come from different users in one status, you will get:
echo "more than one has commented your status
When a user comments, it also saves in users_msgs a row, with:
uID | nData | icon
uID contains the status creator's id (not the id of the user who commented)
icon contains the commenting user's id.
nData contains the status id.
(please do not comment on the names of the columns)
How can I do this? I don't know where to start.
The only thing that I know should be done is that in the query should be uID = '$USER' (where $USER is the id of the signed-in user). But how do I check if there's more than one user that has commented per status id (nData), and if not show a single message of user has commented on your status.
If something's unclear please comment and I'll update my question.
Just change how you're querying. What you want to find is the number of users that have commented on a particular status and the number of comments. Based on those two pieces of information you can determine what to print.
If you use GROUP BY grouping statements you can aggregate data about each status message. You should group by the id of the status message, this is nData. Then all you need to do is count the number of unique users (that's unique values of icon) and also the raw number of comments, which can be achieved using COUNT(*).
SELECT uID, nData,
COUNT(DISTINCT icon) as num_commenting_users,
COUNT(*) as num_comments
FROM users_msgs WHERE uID=$USER GROUP BY nData
Now num_commenting_users will tell you the number of unique users that have commented on status with id nData, and num_comments will tell you how many comments have been made on status with id nData.
For each row returned by this query, you can then do a simple check, if there are more than 1 unique commenting users, print "more than one has commented your status", else print "user has commented your status":
$query = mysql_query(...);
while($row = $mysql_fetch_assoc($query)){
if($row['num_commenting_users'] > 1){
echo "more than one has commented your status";
}else{
// we know there was at least one comment made, and
// even if many comments were made, they were all made
// by the same user (since there was exactly 1 num_commenting_users)
echo "user has commented your status";
}
}
For each status change of each user you should have "not read comments count" variable. For example, user "USER" has changed his status twice:
user_id status not_read_comments_count
USER BUSY 0
USER AVAIL 0
And people has left some comments on the status changes:
user_id status not_read_comments_count
USER BUSY 2
USER AVAIL 1
When "USER" comes back to site and reads his profile, your program reads "not_read_comments_count" for each status change and yields:
"You have 2 comments on your status"
"You have 1 comment on yout status"
and right after yielding it resets all "not_read_comments_count" to 0:
user_id status not_read_comments_count
USER BUSY 0
USER AVAIL 0