SQL/PHP calculate percent of yes/no votes - php

This has me stumped I have Googled for ages now and have searched here but have had no joy on what is probably a very simple to do ...I dont know maybe im just searching with the wrong search terms i really dont know but heres my question ...
I have a basic voting script click yes vote goes up by 1, click no vote goes up by 1
SQL Table:
votes
id
item
yes
no
what i want to do is show the percent of wins (yes votes) over loses (no votes) rather then the actual number of votes so on the page rather then it saying
"Wins 400 loses 600"
it will calculate the percent between the yes and no and say:
"Wins 40% loses 60%"
as always thanks in advance for any help and assistance.
Update:
How can i get it to show at the moment i'm gettng "id #8 resource"
$query = "SELECT ((yes / (yes+no)) * 100) as yesPercent, ((no / (yes+no)) * 100) as noPercent FROM table WHERE id=1";
$result = mysql_query($query);
echo $result;
I assume its this line i got wrong (or probably the whole query *novice at work)
$result = mysql_query($query);
but have tried a few mysql_????() and none seem to work

As I understand it your table would hold
id - item - yes - no
1 - name - 400 - 600
Then you'd query
SELECT ((yes / (yes+no)) * 100) as yesPercent, ((no / (yes+no)) * 100) as noPercent FROM table WHERE id=1

It is very simple indeed.
You want to use this simple proportion
NumberYes : TotalVotes = Percentage : 100
And you can find percentage using the formula
Percentage = (NumberYes * 100) / TotalVotes

Related

Adding bonus to students score and should not exceeds the full mark

There is a section in my school web app that allows me to add bonus score for all students of a specific class at once. The math problem I am struggling to solve is when the student got full mark and nothing can be add up. also the same issue happened when the total score valve will exceeds the final mark limit !
so, using the following MySQL query, how can I update all student scores which will not at any circumstances exceeds the limit.
for Example: course limit is 60 and the current student final score is 59 of 60 and we would line to add bonus of 4 marks to all students !?
UPDATE
students
SET
final_score = final_score + 4
WHERE
studentid = 201
Thanks in advance
You can use LEAST() for this, which will apply the smallest of a set of values. This will require your query to know what the max score is.
UPDATE
students
SET
final_score = final_score + LEAST(modifier, max_score - final_score)
WHERE
studentid = 201
Where of course max_score - final_score will be the exact amount to reach a perfect score. If that exact amount is less than modifier, then the exact amount will be applied instead of modifier.
If you want to subtract modifier, and avoid a negative result, you can do this
UPDATE
students
SET
final_score = final_score - LEAST(modifier, final_score)
WHERE
studentid = 201
Would something like this work for you?
UPDATE
students
SET
final_score = final_score +
(CASE
WHEN final_score = 57 THEN 3
WHEN final_score = 58 THEN 2
WHEN final_score = 59 THEN 1
ELSE 4
END)
Seems like a homework question?
Clearly you don't want to give everyone +4, because it could exceed the course limit if someone scored i.e. an 58. But a student scoring 58 should get the 2 points extra right to get the max score right?
This means you can't filter student scores in the WHERE statement, using something like WHERE final_score < 60. Instead, try a way of using the number 60 in your SET statement so there is no way to surpass 60.
Good luck!

MYSQL - fastest way to get points around coords [duplicate]

This question already has answers here:
MySQL Great Circle Distance (Haversine formula)
(9 answers)
Closed 6 years ago.
I'm trying to get points around "me" by location and it's pretty simple :
select * from X where ((SQRT(POW(69.1 * (a.lat - 49.201441 ), 2) +
POW(69.1 * (16.161299 - a.lng) * COS(a.lat / 57.3), 2))) <='1') LIMIT 5
*
my location = 49.201441;16.161299
distance = 1 (mile)
This query returns me 5 points around "me" at a distance of 1 mile. This is rly fast, but just when my table have a ... I don't know, maybe 5000 rows.
I'm using this query on 200 000 rows table, and it's very slow! Maybe .. 2-7 sec and even if the limit is 1 = no difference.
Can someone explain it to me and help me? Thanks a lot guys!
BTW: If my query does not contains this part of query = it takes maybe 0.0008s... so second part of query is correct.
I have used this for finding people near you.
table name: user_location
id|username|latitude|longitude
i am not sure that the distances is for a mile, but what this does is takes the users lat minus each persons from the database. If the users lat and long are within 1 it will be put into the results.
The biggest problem is 1 isn't equal to 1 mile. it is closer to 10miles i believe (it was a while ago and might not be 10 miles i can't remember), but you can always change this. Another problem is that you basically always have to update people's locations. Which isn't that bad, but if you have a slow server with a lot of users it can slow you down.
It is fast and works... Not the best solution, but might work for you. For me it atleast showed a nice surrounding area.
$query = "SELECT * FROM `user_location` WHERE (".$user_lat." - latitude) <= 1 AND (".$user_lon." - longitude) <= 1 LIMIT 100";
I tested this with 10,000 lines and it was just under 3 seconds with 254 people near me. (randomly generated content)

Selecting every 100 rows from MySQL witha next button in PHP

I am looking to implement this code into a site but I have a few questions. What does the code mean exactly? How does it translate? "end as x" what is it doing here? does anyone have a recommendation as to how to best use it with php? I found this script on PHP and asked the creator but I want to ask the community to see if I get a quicker response.
To better clarify, I am trying to select every hundred rows. I am trying to create a next button that will pull every 100 rows with every click of the next button or arrow. Like a pagination but without the number of the pages.
SELECT * FROM storeCoins WHERE intId in (SELECT CASE(intId%100=0) WHEN 1 THEN intId else 0 END AS X FROM storeCoins ORDER BY 'year' ASC
I don't understand what the syntax means but would really like to understand this. Seems very efficient. If you can recommend how to best implement this using PHP that would be very helpful. I created a next button that only shows the next id in the database but not the next 100. My biggest interest is in understanding this sql code.
it can be as simple as...
For page 1 use
SELECT * FROM storeCoins ORDER BY 'year' ASC limit 0, 100
for page 2 us
SELECT * FROM storeCoins ORDER BY 'year' ASC limit 99, 100
for page 3 us
SELECT * FROM storeCoins ORDER BY 'year' ASC limit 199, 100
and so on

PHP / SQL - Show specific number of rows per query

I apologize if this question has been asked already, I'm sure it has I just was not able to find a similar question/answer that solved my problem.
I am pulling data from PostgreSQL and I have an SQL statement similar to:
$SQL = "SELECT * FROM my_view_table WHERE id='".$my_id."'";
From this I run my query. The request pulls from a view in PostgreSQL which looks through more tables. That is correct. I'm not sure how to limit the number of rows to display. I receive about 1,000 rows of information with about 20 columns of data so it takes an incredible amount of time to query.
Is there a way to ask for rows 0 - 100, then 101 - 200, etc, so I can pull 100 at a time to display? I know I'll have to use a little code to keep track of the count, but I just need some SQL help with querying "x to y" rows.
Thank you for the help with this issue. (If there is another very similar question that has already been answered a link to that would be a sufficient answer!)
I've posted the answer to my question down below.
I found the answer to this question from gnarly's suggestion of LIMIT on SQL
$sql = "SELECT * FROM my_table LIMIT X OFFSET Y";
Where LIMIT only gives the X number of rows you want, and OFFSET gives the Y starting point. So showing rows 0 through 30:
$sql = "SELECT * FROM my_table LIMIT 30 OFFSET 0";
And showing rows 31 through 60:
$sql = "SELECT * FROM my_table LIMIT 30 OFFSET 30";

How to perform page control?

I mean what the most efficient way to get information about the quantity of your page's items and make sql query with LIMIT that you need. or I should get all items and then crop array with php functions?
now I do 2 queries: first to count all items and second to get items that I need with LIMIT.
OK, I'll be more concrete. For example I need to show a question on my page and 20 answers to this question. At the bottom there shold be page control: links to the next, prev page and so on. I want to show proper number of links (number of answers/20) and when I go to any link I want to recieve proper answers (for example 41 to 60 on the 3d page). So what's the best way to get number of items (answers) to show proper number of links and to get proper answers for each link?
I guess your'e trying to say you want to know how many items/answers there is in the query but only read up to 20 items at at time, for pagination.
Firstly: You really should look for a pagination package; lots and lots of people have had the same problem before and there probably exists both free/opensource and proprietary solutions for your programming language and framework. (If you say what language you are using I'm sure someone can reccomend a solution for you.)
Anyway, I know I like to know how things work, so this is how it usually does:
As far as I know the pagination code calculates the pages by doing one query using select count(*) from tblX where something divide this number with the items-per-page number and use ceiling (e.g. 4.1 => 5).
For listing the results per page a new query is required; don't worry the count query is terribly much faster than getting every result discarding the ones you don't need DO NOT DO THAT (that's the recipie for becoming the top story on this page). Something like select * from tblX where something limit Y offset Z where Y is the number of items per page, and Z is the the (requested_page - 1)*Y; page 1 will have offset 0, page 2 have offset 20 (if thats what Y are) etc..
But do not try to implement this manually, it's unneccesary, tedious and error prone, much better to use your time customizing a readymade solution.
I'm assuming you want a count of the number of rows you'll be reading so as to do some pagination or similar? I don't understand your need for the LIMIT in the context of your question. However, if you just want a count of how many rows have been found, use one of the following.
You select the count of all rows such as:
select count(*) as counted, name, address
from contact
Or found rows:
SELECT SQL_CALC_FOUND_ROWS, name, address
from contact
This may be mysql specific I'm not sure.
Update:
For pagination you would do something like the following - (Psuedocode)
$rows = array($result)
$num_rows = sql_calc_found_rows
$per_page = 20
$pages = ceil($num_rows / $per_page)
page
$rows_this_page = array()
$rows_this_page = get_values($rows, (min index)$page_number * $per_page - $per_page, (max index)$page_number * $per_page - 1)

Categories