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.
Related
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.
I apologize for this beginner question but unfortunately it is my level.
I have a fairly simple web page for my work, it is a index.php page that when opened goes out to a DB and retrieves the contents of a certain column. It then places the results in a drop down pick list.
Here is my problem, this column is a list of materials for customers. Some customers have more than 1 different type of material, while others have one.
Therefore my pick list can look like:
Apple /n
Orange; Apple; banana/n
banana;peach /n
orange/n
I am trying to come up with something that when I pull the data from the mysql DB that my php seperates the materials and only provides unique items.
Here is my code for creating picklist:
<p><select size="1" name="material" ID="material" onChange="showfield(value);">
<option value=''><None></option>;
<?php
while ($row = mysql_fetch_array($query))
{
$rowmod = strtr($row['material']," ","_");
echo "<option value='$rowmod'>$row[material]</option>";
}
?>
Here is my mysql select:
$query="select distinct material from TABLE-A order by material";
Update:
I think my Mysql is right, I think I played around with the php strtr and I was able to remove the ; and add lines in, but now I do not know how to make it cycle through and create my
here is the new code:
$row[product]";
}
?>
some output from my $row will have only one product, some will have 2 or more, I wonder if I have to put another while loop after the $rowmod?
I have a feeling I am close, but hoping for some guidance.
First of all, you should make a material table, indexed with an auto_increment id, and use that ID in what you call TABLE-A in a column material_id. Like that you'll have a list of unique material in one table dedicated to it, where you can even add some columns for the details of the material, etc..
Then I am unsure of your needs/use-case, but it looks like you'll need a customer_material table to link a customer with its material(s) so that you know which customer uses which material. It would have an id auto-incremented, as it should always be for any table for better practices, a customer_id and a material_id, with an unique index on the both last columns (customer_id+material_id) to be sure you link one material to one customer only once and not many time each material for the same customer.
Then when you'll need to list the materials for a given customer, just use this query:
select m.id, m.name
from customer_material cm
join material m on cm.material_id = m.id
where cm.customer_id = YOUR_CUSTOMER_ID
If you need to list all materials uniquely, you;ll then need this query:
select m.id, m.name
from material m
order by m.name /* optional, to order by the material name */
And voila. As I am unsure of your use-case the schema of the DB might be a bit different, but I think anyway the main problem in your issue is that the DB is not well architected. Lemme know if I something is unclear here.
You mentioned that different customers have different materials, but that is not reflected in your SQL query because there is no WHERE clause, meaning that you are selecting all unique values from the materials table regardless of any condition. But with that aside, I think that if you change your code slightly you will get some data.
$query="select distinct `material` from `TABLE-A` order by material"
<p><select size="1" name="material" ID="material" onChange="showfield(value);">
<option value=''><None></option>;
<?php
while ($row = mysql_fetch_assoc($query))
{
$rowmod = strtr($row['material']," ","_");
echo "<option value='$rowmod'>$row['material']</option>";
}
?>
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.
I have a select statement showing the following results:
On_loan barcode
Y 12345
Y 12345
N 12345
N 12344
Y 12344
Each barcode for a book can have more than one copy. Users can place a book on hold. E.g user '1' has reserved book 12345 and 12344. The above results show: that the two books with barcode 12344- one is available, the other is unavailable. I want to be able to show two regions in PHP, the top showing books that are ready to take out(that were on hold) and the other showing books that are unavailable which have been placed on hold. From my select query i now want my select to check to see for each barcode 12345 and 12344 whether a book has been returned. If it has i will then use the hold_date to see if its the earliest Hold for the specific book.
I understand on_loan informs me whether a book has been returned, however how can i use 'N' from on_loan for each book. I believe distinct will not work.
How can i go about doing this.
My Hold table
has the following fields:
user
isbn
hold_date
I think you are asking for a way to check if a recently returned book is on hold for another customer, correct?
The book should actually have a unique barcode per each physical copy of a book in the library, as well as an ISBN for the book in general.
Holds would be placed by ISBN.
When a book is checked in, enter that copies barcode, then pull its ISBN number and see if another customer is waiting for it.
If so, set the status for that copy to 'hold', create a related library book to hold record relation.
Otherwise, set the book status to checked in.
Assuming there is a table 'copy' that has a record for each physical copy with unique barcode and relates to a table called 'book'
that has info about a book like ISBN and Author etc, and a table called 'hold' that has the hold info an ISBN (or better, book.id)
Here are the all the copies that are checked in and have a hold on them.
select * from copy left join book on book.id = copy.book_id where copy.status_id = get_book_status('in') and book.isbn in (select isbn from hold);
Maybe you should have a bookTitle table with ID, Barcode, link to barcode tables and then you could do a query to return all copies of a bookTitle and use queries to return barcodes that are on loan and not.
The ID makes it unique.
That's not quite a good database design, if you are asking this kind of questions.
First of all, you should transform this to the third normal form databse.
Then it will look like three tables: books (name, barcode, available_count), users(user_id, name) and a relationship table users_to_books(user_id, book_barcode, state), where state can be an menu with values "on hold" and "on hands".
After that you can do all kind of stuff with counting and checking.
I have a table in postgres called workorders. In it are various headings. The ones I am interested in are labor, date_out and ident. This table ties up with wo_parts (workorder parts). In this table are the headings I am interested in, part and workorder. Both are integers. (part auto number) The final table is part2vendor and the headings are retail and cost. Right, basically what happens is.....I create a workorder (invoice). This calls a part from part2vendor. I enter it and invoice it off. In workorder a row is created and saved. It is given an ident. In wo_parts, the part i used is recorded as well as workorder number and qty used. What I want to do is create a report in php that pools all this info on one page. IE. if i choose dates 2009-10-01 to 2009-10-31 it will pull all workorders in this range and tell me the total labour sold and then the PROFIT (retail less cost) of the parts I sold, using these 3 tables. I hope i have explained as clear as possible. any questions please ask me. Thank you very much for your time.
You will want to read up on SQL - keywords to look for include "aggregate", "SUM" and "GROUP BY".
You query will look something like (but this will certainly need correcting):
SELECT
SUM(wo.labor) AS tot_labor,
SUM(p2v.cost - p2v.retail) AS tot_profit
FROM
workorders AS wo
JOIN wo_parts AS wp ON wo.ident=wp.ident [?]
JOIN part2vendor AS p2v ON ...something...
WHERE
date_out BETWEEN '2009-10-01'::date AND '2009-10-31'::date;