Look at array and count true or false from duplicate values - php

I have a table in SQL, where users can answer many times (the same users and different users). I want to count how many true or false values there are.
For example, like user 1 has 5 rows in my table, 3 times true, and 2 times false and user 9 has got 10 true and 1 false like that but I would not know what user numbers.
I would like output like
User 1 - 5x True 1x False, User 4 1x True 3x False etc. But I would not know what user and the user list can grow.

there is a simple (not recommended) solution using a loop:
$resultq = mysql_query('select value, user_id from answers');
$answers_per_user = array(); // positive answers per user
$totals_per_user = array(); // total answers per user
while($result = mysql_fetch_assoc($resultq)){
if($result['answer'])
$answers_per_user[$result['user_id']] += $result['answer']; // increment positive answer counter for user
$totals_per_user[$result['user_id']]++;
}
you would have an array holding positive answers per user and total answers per users which you can then use to calculate negative answers
the recommended solution is to use a group by sql statement that gives you all the calculated information.
$result = mysql_query('select sum(value) as positivecount, count(*) as total, user_id from answers group by user_id');
while($data = mysql_fetch_assoc($result)){
// $data will hold positivecount,total, and user_id giving you all the data you need for calculating negative answer values.
}
// alternatively, use a query like this for counting the answers that were 'beans':
// select sum(if(value = "beans", 1, 0)) as answered_beans, count(*) as total, user_id from answers group by user_id
see: http://dev.mysql.com/tech-resources/articles/wizard/page3.html

The most elegant solution for this problem is to actually have two SQL tables; one with one row for each user (userID, username, etc.) and one for each vote, wich could be multiple per user.
The following example will echo some information about the data.
<?php
$sqlusers = mysql_query("SELECT userid FROM user_table")//This line grabs all users from the database.
$users = mysql_fetch_array($sqlusers);//This line creates an array containing all users.
foreach($users as $key=>$currentuser){
$sqlvotes = mysql_query("SELECT userid, vote FROM vote_table WHERE userid = $currentuser[userid]");
$votes = mysql_fetch_array($sqlvotes);//obtain an array of votes the current user has submitted
$votefrequency = array_count_values($votes)//counts the amount of trues and falses in the $votes array, and returns an array with the [true] and [false] indexes containing their respective frequency.
echo "user ".$userid." has voted ".$votefrequency[true]." times true and ".$votefrequency[false]." times false/n";
echo "average vote:". (($votefrequency[true] - $votefrequency[false] > 0) ? "true" : "false" );
}

Related

How can I combine two MySQL rows as one and count results?

Basically, I have a database with 10 exam types. Each type has two parts, and is updated as pass or fail. I need to list the total count of exams that have not been completed (both parts passed).
I've tried this and it returns the count if either part shows pass, not both.
$query = sprintf(
"SELECT * FROM candidate_exams
WHERE gID='1' AND canID='%d' AND exResult='y'
GROUP BY gEID",
(int) $canID
);
$result = $con->query($query);
$rowCount = 10 - mysqli_num_rows($result);
'gID' is an identifier that tracks what group these 10 exams come from,
'canID' is a candidate identifier,
'gEID' is an exam type.
SELECT COUNT(*) FROM tabele WHERE type_a = 'fail' OR type_b = 'fail'
something like this?
It would help a lot to see your table structure to be able to answer this question properly.

Check if a value exists in mysql column

Is there a way to check if a value exists in a mysql column? I have table songs, and there are some columns, one of them is called 'agent_ip' where i will put a list/array of all user ip's that will visit the site. I need to check if current user ip is present in column 'agent_ip'. Here is some of my code:
public function voteSong($song_id, $case, $agent_ip) {
$query = $this->link->prepare("SELECT * FROM songs WHERE id = ? LIMIT 1");
$query->bindValue(1, $song_id);
$query->execute();
$rowcount = $query->rowCount();
if ($rowcount != 0)
{
if (!in_array($agent_ip, $r['ip']))
{
if ($case === 'like')
{
while($r = $query->fetch())
{
$vote = $r['votes'] + 1;
}
}
elseif ($case === 'dislike')
{
while ($r = $query->fetch())
{
if ($r['votes'] > 0)
{
$vote = $r['votes'] - 1;
}
else
{
$vote = 0;
}
}
}
$query = $this->link->prepare("UPDATE songs SET datetime = ?, votes = ?, agent_ip = ? WHERE id = ?");
$query->execute(array(date("Y-m-d H:i:s"), $vote, $agent_ip, $song_id));
}
}
}
The line if(!in_array($agent_ip, $r['ip'])) contains the wrong function which won't work, but i need an alternative for mysql. $r['ip'] variable is data from the 'agent_ip' column which look like this 127.0.0.1, 127.0.0.1, 127.0.0.1 (using 127.0.0.1 just for example, every 127.0.0.1 is a different ip)
If you're only checking against a single IP, why don't you just modify your query from:
"SELECT * FROM songs WHERE id = ? LIMIT 1"
To:
"SELECT * FROM songs WHERE id = ? AND agent_ip = ? LIMIT 1"
It seems a bit wasteful to query your whole result set when you are only querying against a specific IP and returning a single row.
EDIT: Your current method would be extremely inefficient, you are passing a unique agent_ip each time you want to query a song to check if the IP exists, that would be fine, but you are creating a new DB connection every time from which you pull back all info which belongs to that song.
Lets say we have 1 song, and 3IP's, currently the application would work like this:
1) Call the method, passing IP_1
2) Query the database getting all songs for ID1
3) Check if IP_1 is in the result set and do process
4) Call the method, passing IP_2
5) Query the database getting all songs for ID1
6) Check if IP_2 is in the result set and do process
7) Call the method, passing IP_3
8) Query the database getting all songs for ID1
9) Check if IP_2 is in the result set and do process
As you can see, there is a lot of repetition here which is going to hinder your apps performance as it scales, you would be so much better modifying your current function to accept a list of results for a song which is pre-queried only once and then recursively call a check function by passing that result array with your unique IP address.
UPDATE You stated I understand that i need to have 2 tables(1 = songs; 2 = votes). But i cannot imagine how i will get songs from database, arranged by votes quantity.
You should read SQL's JOIN documentation, the concept is simple - JOIN allows you to pull back a more detailed set of information based on what you want to query, in your example you may want to find out how many votes a specific song has.
Your tables may look like:
Songs
SONG_ID Primary Key
SONG_TITLE
SONG_DURATION
SONG_TAGS
Votes
VOTE_ID Primary Key
SONG_ID Foreign Key - (references the song_id table)
VOTE_RES Bool (either 0 for no, 1 for yes)
AGENT_IP Who sent the vote
You could then find out how many people said they liked the song by performing a join:
SELECT * FROM songs
JOIN votes
ON songs.song_id = votes.song_id
WHERE songs.song_id = 1
AND votes.vote_res = 1;
This would return all the song with the id of 1 and all of its associated likes. Hope that helps a bit :)
First you need to deserialize/decode the data from the column to the proper php array and then you can use in_array function. In your post edit you stated that you have a comma separated list of IP's, so to convert it to array you need to use an explode function:
$ip_list = explode(', ', $r['ip']);
now you can use the in_array function on the new array:
if(!in_array($agent_ip, $ip_list))

Find out how often a value is between 2 dates in mysql

Hi with the follow code I request what dates are all in my database without duplicates.
Then I save it to an array. In the array I also need an other value.
The value I need is how much users are in one day in the database without duplicates.
For Example the array must later lookslike 23.07.2013 - 10, 24.07.2013 - 50 (users).
I search for several hours but I don't find a good mysql query.
$query = "SELECT id, user, timestamp FROM stat WHERE timestamp BETWEEN '$datum1' AND '$datum2' GROUP BY timestamp";
$result = mysql_query($query,$db);
while($row = mysql_fetch_assoc($result))
{
mysql_num_rows($result);
$dataset1[] = array(strtotime($row['timestamp']),$number_of_users_on_this_day);
}
Try:
$query = "SELECT id, user, COUNT(*) as count FROM stat WHERE timestamp BETWEEN '$datum1' AND '$datum2' GROUP BY timestamp";
This will return the number of entries in the value 'count'
if you want distinct data, in place of * use
COUNT(DISTINCT id)
with whatever field you want to be unique in place of 'id'

PHP MySQL - Find Row ID

I have a table called "participants" that has 3 fields:
prt_id
prt_event_id
prt_participant_id
What I have is a select query with a where condition on event_id. The query returns let's say 20 rows (20 different participants). What I would like to do is to be able to figure out the row number for a given participant (prt_id).
SELECT *
FROM participants
WHERE prt_id = someinteger
While you can't specifically find a row ID using MySQL, you could do something like the following:
$conn = new mysqli(/*dbinfo*/);
$res = $conn->query("SELECT prt_id FROM participants");
$rowids = array(); $currid = 1;
while ($row = $res->fetch_object()) { // this is using the mysqli library
$rowids[$row->prt_id] = $currid;
$currid++;
}
This would give you an array of ids associated with prt_id.
You could do something like:
<?php
$counter = 1; // Start at one for first entry
$res = mysql_query("SELECT * FROM participants WHERE prt_id = 12");
while( $array = mysql_fetch_assoc($res) )
{
// Do something with the counter, store it into array with details
$counter++;
}
?>
This should do what you want inside MySQL (ie assign a rownum in the order of prt_id), but the performance will be dependent on the number of rows in the table so it's not optimal.
SELECT * FROM (
SELECT #tmp:=#tmp+1 rownum, p.*
FROM (SELECT #tmp:=0) z, participants p
ORDER BY prt_id
) participants
WHERE prt_id = 36;
Demo here.
Edit: This "doh level" rewrite uses an simple index range instead of a table scan, so should be much faster (provided prt_id is a PRIMARY KEY)
SELECT *, COUNT(p2.prt_id) ROWNUM
FROM participants p1
JOIN participants p2
ON p1.prt_id >= p2.prt_id
WHERE p1.prt_id=36;
Demo here.
you could just add an index column in your database, set it as int, primary key and auto increment. then when retrieving the row you retrieve the index number.
RowID is a feature of Oracle: http://docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns008.htm.
MySQL does not have something like that, you can basically emulate that by assign number to an array inside php as you retrieve each row, but that doesn't guarantee you the same number next time you retrieve that results. You probably have to settle for using one of the primary IDs

Add Friends Function - friend already added

I have an application to add friends. I need to have my script to check, if the users' already friends. I thought I could do this by a COUNT. I did like this:
$username = $_GET[user];
$ven_til_id = $_SESSION['userID'];
$num = 1;
if(isset($_GET['add_friend'])){
$check=("SELECT username,ven_til_id, COUNT(*) AS num FROM friends WHERE username=$username AND ven_til_id=$ven_til_id GROUP BY username,ven_til_id")or die(mysql_error());
$result=mysql_query($check);
if(!$result){
echo "Cant run query.";
} else {
$num = mysql_num_rows($result);
}
if($num>0){
header("Location: /profil/$username?add_error");
} else {
$sql=mysql_query("INSERT INTO friends (username,ven_til_id)VALUES('$username', '$ven_til_id')")or die(mysql_error());
header("Location: /profil/$username");
}
}
?>
But when I'm adding one friend it's fine. It adds it and everything is fine. But then when I try to add another, it says we're already friends. I guess it's because it's counting how many times my ID (ven_til_id) is listed in the tables.
You're missing a comma:
SELECT
username,ven_til_id COUNT(*) AS num FROM ...
should be
SELECT
username,ven_til_id, COUNT(*) AS num FROM ...
Also, your reference to the count field is incorrect - it should be the third column or $row[2]
You may want to make your code more robust by referring to fields by name eg $row['num']
One final thing to confirm is that the value being retrieved with a count is being treated as an integer not a string. I don't think it's the problem here but you may want to explicitly cast it to avoid possible issues later eg...
$num = (int) $row[2];
Option 1
Just select the appropriate rows and see how many records you get back...
SELECT username,
ven_til_id
FROM friends
WHERE username=$username
AND ven_til_id=$ven_til_id
Then just count the number of records returned using PHP - eg mysql_num_rows() (I think that's the correct function name)
Clarification:
Change
$row = mysql_fetch_row($result);
$num = $row[2];
to
$num = mysql_num_rows($result);
Option 2
Get MySQL to do the counting for you - in qhich case you need to tell it to group multiple record together...
SELECT username,
ven_til_id,
COUNT(*) as Num
FROM friends
WHERE username=$username
AND ven_til_id=$ven_til_id
GROUP BY username,
ven_til_id
Then just read the 3rd value of the first row (num) and you'll have a count
NB: The second method may be overkill if you're only ever expecting a 1 or a 0

Categories