Me and my friends are choosing a group name, and I'm trying to make this ranking where all of us sort the name order by our preferences.
My program should do a calculation based on the order.
Example, if I sort "JAIME" 1 and "CERSEI" 2, "JAIME" gets 10 points and "CERSEI" 8 points. If my friend sort "JAIME" 1 and "CERSEI" 6, "JAIME gets 10 points and "CERSEI" get's no points.
So, the rank will be:
1- JAIME with 20 points
2- CERSEI with 8 points
I have a simple table in mysql called "NAMES" with these fields
"id"
"name"
"riccardo"
"roberto"
"marika"
(and others friend names)
With a Jquery script, every friend can sort the name list. The order number is automatically entered in the mysql db.
So, for example:
"id" --> 1
"name" --> JAIME
"riccardo" --> 1
"roberto" --> 2
"marika" --> 5
1, 2, 5 are the order list number
Now that I got here, I have no idea how to proceed to obtain a rank based on the orders lists :(
hope my question is understandable
thank you
Firstly I'd consider changing your schema somewhat. Currently adding friends means adding columns (which also means updating your queries). Better to have say a friends table and then a table for friends-names ratings.
However with what you have the calculation for a score given a position is:
10-((score-1)*2)
Given this we can create a query to get scores for names:
SELECT id, name, 10-((riccardo-1)*2) + 10-((roberto-1)*2) as score-- Plus all other names
FROM names
ORDER BY score
Using a more usual schema the query would look something like:
SELECT names.id, names.name, SUM(10-((names_ratings.rating-1)*2)) as score
FROM names
LEFT JOIN names_ratings ON (names.id = names_rating.name_id)
ORDER BY score
The second query means that you can add and remove friends much more easily and this wont change the code or the database schema.
Related
i'm developing an api that'll provide a rating value for each student. It's a two stage rating system Click Here . In STAGE-I UI view, student list appears with student name and rating points. To rate a student, one have click on the rating stars & STAGE-II appears with some rating categories. The rating of each student depends on this part. Each category's a rating value as Sum_of_Rating_value/rating_numbers. After rating all categories (or rating 2/3 of these), the result will be re-rated as Sum_of_ALL_Category/Category_number and be placed in STAGE-I student list UI view. N.B.:I've done all the calculations.
Now when i call the data in QUERY as:
<?php
$connection = mysqli_connect("localhost","root","pass","DB_NAME") or die("Error " . mysqli_error($connection));<br><br>
$stu_id=$_POST['stu_id'];<br><br>
$sql_query = " SELECT *, FORMAT(((SELECT SUM(view_rating.total_points+view_rating2.total_points2+view_rating3.total_points3+view_rating4.total_points4+view_rating5.total_points5) FROM view_rating,view_rating2,view_rating3,view_rating4,view_rating5) / (SELECT SUM(view_rating.rating_number+view_rating2.rating_number2+view_rating3.rating_number3+view_rating4.rating_number4+view_rating5.rating_number5) FROM view_rating,view_rating2,view_rating3,view_rating4,view_rating5) ),1) as average_rating,(select count(review) from review WHERE stu_id=14 )as review_count FROM student_info WHERE stu_id='$stu_id' ";<br><br>
$user_array=array();<br>
$main_array=array(); <br><br>
$result = mysqli_query($connection,$sql_query);<br>
while($row =mysqli_fetch_assoc($result))
{<br>
$user_array['stu_id']=$row['stu_id'];<br>
$user_array['name']=$row['name'];<br>
$user_array['mobile']=$row['mobile'];<br>
$user_array['email']=$row['email'];<br>
$user_array['gender']=$row['gender'];<br>
$user_array['Dept']=$row['Dept'];<br>
$user_array['blood_group']=$row['blood_group'];<br>
$user_array['average_rating']=$row['average_rating'];<br>
$user_array['review_count']=$row['review_count'];<br>
array_push($main_array,$user_array);<br>
}<br><br>
$mainarray=array("STUDENTS"=>$main_array);<br>
$jsonData = json_encode($mainarray, JSON_PRETTY_PRINT);<br>
echo $jsonData;<br>
?>
In JSON array, it doesn't pull average_rating and review_count from DB. It only shows null.But when i define stu_id as 2 or 3 in WHERE clause, it shows the same average_rating and same review not only in the defined stu_id but also in the other ids. But I want exact average_point/review fro each student. I think, the problem lies in the QUERY. So, can anyone help me out???
Your $sql_query is really hard to read; I would break it up into smaller pieces.
Why do you have (select count(review) from review WHERE stu_id=14 )as review_count as part of the query?
What's special about the magic number 14? It's generally a bad idea to have magic numbers in your code. Who is student 14? Why is this number hard-coded instead of using $stu_id or some other variable? This is probably related to why you're not getting a review_count.
If you do get a review_count when you change this to WHERE stu_id=2 or WHERE stu_id=3, it's probably either because review has entries for students 2 and 3 but not for student 14, or because $stu_id was either 2 or 3 when you made the query. I'd guess the latter.
Since your overall query is wrapped in an outermost FROM student_info WHERE stu_id='$stu_id', it should only grab information for the student with id $stu_id. If that id is not equal to 14, there's no information for student 14, so when you try to run the code for the review count on student 14 it doesn't find anything (you're searching in a dataset that was already filtered to only have data for student $stu_id).
I'm not sure about this because the code is very hard to read as it is currently structured, but I'd guess that's a good starting point for troubleshooting.
is it possible to using ajax with PHP/SQL to suggest the next avaliable number for a user based on the current numbers in the list? this is to avoid duplication.
I have a form/table http://i.stack.imgur.com/TYhSG.png and in this table I have for an example departments and a list(the numbers in the list needs to be unqiue). because of the way the system is disigned, users had to manually rearrange the numbers if they wanted to the same number in another department. for an example number "4" can only be used once, this example applies to any other numbers.
The numbers in those list are all avaliable in the database, what this means is that I have a select statement and i echo $list to redender those list you show in the image.
Tp avoid users having to manally tryping those numbers especially if the list is up to 100 or more. I basically want to use ajax to first check if the number in the list is already in used and if yes then suggest a number to them.
Can this be done? If so please show me an example.
Thanks for your time.
p.s.
i missed out an information piece of information they is another column called `values
tablename: check
values type list
cars 1 2
cars 1 4
cars 1 3
bicycle 1 2
bicycle 1 3
bicycle 1 4
the numbers in the list can be used again if the values is different so basically the list needs to be unique to the values is it in.
If the column list needs to be unique and automatically increment, you should be using MySQL's AUTO_INCREMENT
This will automatically populate the column with the next available number on row creation.
I'm trying to insert diferent numebers (like "10 11 12") into a MySQL field (the numbers come from PHP), but when I do the query, the field only gets the first number.
For example, I do:
UPDATE profile SET subcategory = '10 11 12' WHERE userId = 1 LIMIT 1
And the DB just registers '10'.
Any way to do it?
This happen because you're updating a number, probably an integer, so mysql do the job just for the first number.
If you do this:
UPDATE profile SET subcategory = 10 WHERE userId = 1 LIMIT 1
UPDATE profile SET subcategory = 11 WHERE userId = 1 LIMIT 1
UPDATE profile SET subcategory = 12 WHERE userId = 1 LIMIT 1
You'll just update the category with the third value (12).
I suggest you a user belonging to multiple subcategories so you'll have to create another table. Eg: a table called subcategories with at least two fields: userId and subcategoryId. And then you could do something like this:
DELETE FROM subcategories WHERE userId=1
INSERT INTO subcategories (userId, subcategory) VALUES (1,10)
INSERT INTO subcategories (userId, subcategory) VALUES (1,11)
INSERT INTO subcategories (userId, subcategory) VALUES (1,12)
The first line (delete) is used just to update the user's subcategories, first you delete all older subcategories for the user and then you insert the new ones. In PHP you could use a foreach() to automatize the insertion of multiple values.
You could also have a non unique userId in the table profiles with an entry per user subcategory but it will complicate things.
I hope it could help you
From your problem I guess that the type of your subcategory is integer. What happens when you put string? It is converted. The converter convert it to first proper integer which is 10 space after 10 is considered as string.
What can solve your problem?
Chance db structure and depend on relations.
(bad idea) change the type to varchar for example and then insert will be done(DONT DO IT)
Do multiple updates(it really depends on db structure)
This very much depends on the problem you are trying to solve.
If you are just trying to store a small number of numbers then using the php join and split functions to take a list of numbers and convert to and from a string and store that in a VARCHAR.
A better way to solve the problem would be to understand the layout of your data. Try having a table that links profiles to subcategories. Two columns, one for the profile ID and one for the Subcategory ID. You might find having a search for database normalisation informative.
This presentation looks relatively informative: http://www.sqa.org.uk/e-learning/MDBS01CD/page_26.htm
I am running a competition where a user enters a team of 3 golfers chosen form a specific list. These entries are then stored in a table - entries with username,player1,player2,player3.
In a different table in the same database I have players results with each player have a player_name,round1score,round2score,round3score,round4score & totalscore.
I want to create a results set which adds the results of a users 3 players (their team) and sorts them with the lowest score for the combined 3 players.
I am having trouble combining the 2 tables and some people are saying the to merge them using PHP afterwards but not sure the best option.
Also I need to after the final round if there is a tie for 1st place make the team with the lowest round4score the top of the results.
Any help or advice on how to achieve this would be greatly appreciated.
Thanks
I have a table that holds user information. One of the columns holds the position of the user in the game they are in.
When a game is being created, I need to update the positions of the users of each team.
Here is an example:
Game id : 7
Team 1 users : 1,2
Team 2 users : 3,4
team1_position : array(1,2)
team2_position : array(13,14)
What I want to do is update the user table using the array of positions in the SET area.
My goal is to be able to update the users without the need for their id (I have different size game boards, so I have multiple position arrays for each board size)
How can I do something like this:
UPDATE user
SET position='(team1_position)'
WHERE game = '7' AND team = '1'
I feel like it would be a waste of resources to select all the id's of each team and update them separately.
I have a hard time understanding what you are trying to do, better explanation would be nice. From what I understand you are selecting data from tables in order to update other tables. Have you tried using an "UPDATE .. JOIN .." query? This should allow you to update multiple rows from one table based on associative data from another table.
For example
UPDATE user
JOIN game ON
user.id=game.id_user
SET user.position=game.team1_position
Obviously this wont work with your code as I have very little info to go on, but that should give you an idea of what to go with.
Sorry if I'm totally off in understanding your problem, as said it's a bit hard to understand your exact issue based on what you've given us.