i want to count multiple values from 1 column(FieldValue) using PHP mysqli, and I can't do it.
`SubmissionValueId` `FormId` `SubmissionId` `FieldName` `FieldValue`
100567 33 1 nam1 A
100567 33 2 nam2 B
100567 33 3 nam3 A
100567 33 4 nam4 B
100567 33 5 nam5 A
100567 33 6 nam6 C
100567 33 7 nam7 B
100567 33 8 nam8 C
100567 29 8 nam8 D
100567 30 8 nam8 F
100567 25 8 nam8 C
my code is:
$dba = JFactory::getDbo();
$dbb = JFactory::getDbo();
$dbc = JFactory::getDbo();
$dba->setQuery("SELECT COUNT(`SubmissionValueId`) FROM #__submission_values WHERE FieldValue ='A' AND FormId =33");
$dbb->setQuery("SELECT COUNT(`SubmissionValueId`) FROM #__submission_values WHERE FieldValue ='B' AND FormId =33");
$dbc->setQuery("SELECT COUNT(`SubmissionValueId`) FROM #__submission_values WHERE FieldValue ='C' AND FormId =33");
$submissionsa = $dba->loadResult();
$submissionsb = $dbb->loadResult();
$submissionsc = $dbc->loadResult();
$formLayout = '<div id="counter">Submissions-A:'.$submissionsa.'</div><div id="counter">Submissions-B:'.$submissionsb.'</div><div id="counter">Submissions-C:'.$submissionsc.'</div>';
and the result is:
Submissions-A=3
Submissions-B=
Submissions-C=
BUT i expect this:
Submissions-A = 3
Submissions-B = 3
Submissions-C = 2
It seems Joomla database class use memory or session to get last query. Get result one by one:
$dba = JFactory::getDbo();
$dbb = JFactory::getDbo();
$dbc = JFactory::getDbo();
$dba->setQuery("SELECT COUNT(`SubmissionValueId`) FROM #__submission_values WHERE FieldValue ='A' AND FormId =33");
$submissionsa = $dba->loadResult();
$dbb->setQuery("SELECT COUNT(`SubmissionValueId`) FROM #__submission_values WHERE FieldValue ='B' AND FormId =33");
$submissionsb = $dbb->loadResult();
$dbc->setQuery("SELECT COUNT(`SubmissionValueId`) FROM #__submission_values WHERE FieldValue ='C' AND FormId =33");
$submissionsc = $dbc->loadResult();
Or simply, use one instance of database object
$db = JFactory::getDbo();
$db->setQuery("SELECT COUNT(`SubmissionValueId`) FROM #__submission_values WHERE FieldValue ='A' AND FormId =33");
$submissionsa = $db->loadResult();
$db->setQuery("SELECT COUNT(`SubmissionValueId`) FROM #__submission_values WHERE FieldValue ='B' AND FormId =33");
$submissionsb = $db->loadResult();
$db->setQuery("SELECT COUNT(`SubmissionValueId`) FROM #__submission_values WHERE FieldValue ='C' AND FormId =33");
$submissionsc = $db->loadResult();
Related
There are two tables that are going to work together.
1st Table:
AttendanceID TeacherID BatchID SubjectID SemesterID Date
32 110 8 9 1 2016-08-04
31 102 8 10 1 2016-07-17
30 108 6 22 3 2016-06-27
29 109 7 18 2 2016-06-27
28 109 8 13 1 2016-06-27
27 110 7 7 2 2016-06-27
26 110 8 9 1 2016-06-27
25 104 2 42 7 2016-04-20
24 104 5 35 5 2016-04-14
23 104 2 42 7 2016-04-14
22 102 2 41 7 2016-04-13
21 102 2 41 7 2016-04-10
20 102 6 23 3 2016-04-10
19 102 6 23 3 2016-04-10
2nd Table:
The first table is consist of unique rows which will be going to use for each new attendance list in table two.
For example: In the first table, the AttendanceID 21 is used in second table for Complete attendance of specific subject.
I want to calculate the percentage of all students in second table of a specific Subject and the total number can be get by the 1st table AttendanceID
What I did in PHP is: First I get the total number from 1st table with this query:
SELECT COUNT(AttendanceID) FROM attendances WHERE SubjectID = ? AND BatchID = ?"
Once I get the total number of attendances of specific subject and batch from first table I store it in variable $total then I write another query for getting obtained attendance from second table:
SELECT COUNT(AttendanceDetailID) FROM attendancedetail WHERE CollegeID = ? AND Status = 'present' AND SubjectID = ?"
After getting the obtained attendance I store it in variable $obtained
Once I get both values then I calculate the percentage in PHP like this:
if(!empty($total) && !empty($obtained)) {
$result = (($obtained * 100)/ $total);
$result = round($result);
}
Here is the complete code of PHP:
public function showStateOfAttendance($subjectID, $batchID){
$st = $this->conn->prepare("SELECT CollegeID, Name, Gender, Photo FROM students WHERE BatchID = ?");
$st->bind_param("i", $batchID);
$st->execute();
$st->store_result();
$num_rows = $st->num_rows;
$st->bind_result($college_id, $name, $gender, $photo);
$this->response['attendance'] = array();
while($st->fetch()) {
$this->calcultaionOfAttendance($subjectID, $college_id, $name, $gender, $photo, $batchID);
}
return json_encode($this->response);
$st->free_result();
$st->close();
}
public function calcultaionOfAttendance($subjectID, $studentID, $name, $gender, $photo, $batchID) {
$stmt = $this->conn->prepare("SELECT COUNT(AttendanceID) FROM attendances WHERE SubjectID = ? AND BatchID = ?");
$stmt->bind_param("ii", $subjectID, $batchID);
$stmt->execute();
$stmt->store_result();
$num_rows = $stmt->num_rows;
$stmt->bind_result($AttendanceID);
while($stmt->fetch()) {
$total = $AttendanceID;
}
$stmt->free_result();
$stmt->close();
$stmt2 = $this->conn->prepare("SELECT COUNT(AttendanceDetailID) FROM attendancedetail WHERE CollegeID = ? AND Status = 'present' AND SubjectID = ?");
$stmt2->bind_param("ii", $studentID, $subjectID);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($AttendanceDetailID);
while($stmt2->fetch()){
$obtained = $AttendanceDetailID;
}
if(!empty($total) && !empty($obtained)) {
$result = (($obtained * 100)/ $total);
$result = round($result);
$rating = ($result)/20;
$tmp = array();
$tmp['result'] = $result;
$tmp['total'] = $total;
$tmp['obtained'] = $obtained;
$tmp['rating'] = $rating;
$tmp['name'] = $name;
$tmp['college_id'] = $studentID;
$tmp['gender'] = $gender;
$tmp['photo'] = $photo;
array_push($this->response['attendance'],$tmp);
//var_dump(array($total, $obtained, $result, $rating, $studentID, $name));
}else if(empty($total)) {
$tmp = array();
$tmp['result'] = 0.0;
$tmp['total'] = 0.0;
$tmp['obtained'] = $obtained;
$tmp['rating'] = 0.0;
$tmp['name'] = $name;
$tmp['college_id'] = $studentID;
array_push($this->response['attendance'],$tmp);
//var_dump(array("0.0",$obtained, "0.0","0.0",$studentID,$name));
}else if(empty($obtained)) {
$tmp = array();
$tmp['result'] = 0.0;
$tmp['total'] = $total;
$tmp['obtained'] = 0.0;
$tmp['rating'] = 0.0;
$tmp['name'] = $name;
$tmp['college_id'] = $studentID;
array_push($this->response['attendance'],$tmp);
//var_dump(array($total, "0.0", "0.0","0.0", $studentID , $name));
}
}
Here is the android screen shot of the queries I did: The following result is for SubjectID = 23 And BatchID = 6
It get me the required result but I need better way to calculate this, is it possible to do this with single query?
Thanks
Try:
SELECT s.CollectID, s.Name, s.Gender, s.Photo,
(SELECT count(AttendanceID) from attendances WHERE SubjectID =? and BatchID = s.BatchID) as total,
(SELECT count(AttendanceDetailID) FROM attendancedetail WHERE CollegeID = s.CollectID and Status = 'present' and SubjectID = ?) as obtained
FROM students s
WHERE s.BatchID = ?
SELECT
SUM(`Status` = 'present') AS presentCount,
COUNT(*) AS totalCount,
(SUM(`Status` = 'present') * 100) / COUNT(*) AS percent
FROM
details
WHERE
BatchID = 2
AND SubjectID = 41
AND CollegeID = 1214
GROUP BY
AttendanceID
Based on your example you don't even need to access the first table. You can just calculate it directly from the data in the second.
Put your queries in subselects:
SELECT
(SELECT COUNT(AttendanceDetailID) FROM attendancedetail WHERE CollegeID = ? AND Status = 'present' AND SubjectID = ?)
/ (SELECT COUNT(AttendanceID) FROM attendances WHERE SubjectID = ? AND BatchID = ?)
* 100
Or (more readable) join two subqueries:
SELECT (obtained * 100) / total
FROM (
SELECT COUNT(AttendanceDetailID) AS total
FROM attendancedetail
WHERE CollegeID = ? AND Status = 'present' AND SubjectID = ?
) t
CROSS JOIN (
SELECT COUNT(AttendanceID) AS obtained
FROM attendances
WHERE SubjectID = ? AND BatchID = ?
) a
I have a problem.
$db = JFactory::getDBO();
$res2 = $_data['DateSubmitted'];
//res2 returns 2014-08-31 12:03:02
$res3 = $_data['UserIp'];
//res3 returns 109.173.20.143 for example
$resdate = date('Y-m-d H:i:s', strtotime($res2));
$SubmId = $db->query("SELECT `SubmissionId` FROM `rrr_submissions` WHERE `FormId` = '20' AND `DateSubmitted`='".$resdate."' AND `UserIp`='".$res3."'");
$db->setQuery("UPDATE `rrr_submission_values` SET `FieldValue` = '".$SubmId."' WHERE `FieldName`='7_Status' AND `SubmissionId`='5682'");
$db->query();
In rrr_submissions:
SubmissionId is INT(11) AUTO_INCREMENT
FormId is INT(11)
DateSubmitted is datetime
UserIp is varchar(15)
In rrr_submission_values:
FieldValue is text
FieldName is text
SubmissionId is int(11)
What am I do wrong? In the result I see "1" in the FieldValue.
Try loading the result row and then calling the field value.
$db = JFactory::getDBO();
$res2 = $_data['DateSubmitted'];
//res2 returns 2014-08-31 12:03:02
$res3 = $_data['UserIp'];
//res3 returns 109.173.20.143 for example
$resdate = date('Y-m-d H:i:s', strtotime($_data['DateSubmitted']));
$SubmId = $db->query("SELECT `SubmissionId` FROM `rrr_submissions` WHERE `FormId` = '20' AND `DateSubmitted`='".$resdate."' AND `UserIp`='".$res3."'");
$row = $db->loadRow();
$SubmId = $row['SubmissionId'];
$db->setQuery("UPDATE `rrr_submission_values` SET `FieldValue` = '".$SubmId."' WHERE `FieldName`='7_Status' AND `SubmissionId`='5682'");
$db->query();
i have such a table:
r_id date recipe_name
1 2012-05-20 Cheese Bread
1 2012-05-21 Cheese pie
2 2012-05-20 Spanish Bread
I would like to get all the data under r_id 1 to be in a single row how can i do that using Sql.I need to achieve something like this:
r_id recipe_name
1 Cheese Bread,Cheese pie
2 Spanish Bread
how can i do this using php too?
Use GROUP_CONCAT
SELECT r_id, GROUP_CONCAT(recipe_name)
FROM yourTable
GROUP BY r_id
Here's the php version
$query = "SELECT id, recipe_name FROM myTable";
$rs = mysqli_query($query);
$results = array();
while($r = mysqli_fetch_assoc($rs)) {
$results[$r['id']][] = $r['recipe_name'];
//$results[$r['id']][] = "<a href=''>".$r['recipe_name']."</a>";
}
foreach($results as $id => $recipes) {
print $id . ' ' . implode(',', $recipes) . "<br>";
}
I have this 3 sql questions:
$producent = mysql_fetch_array(mysql_query("
SELECT FieldValue
FROM `xy8vx_rsform_submission_values`
WHERE SubmissionId = '$id' AND FieldName = 'Producent'
"));
$model = mysql_fetch_array(mysql_query("
SELECT FieldValue
FROM `xy8vx_rsform_submission_values`
WHERE SubmissionId = '$id' AND FieldName = 'Model'
"));
$nr_ser= mysql_fetch_array(mysql_query("
SELECT FieldValue
FROM `xy8vx_rsform_submission_values`
WHERE SubmissionId = '$id' AND FieldName = 'Nr seryjny'
"));
Is it posible to get those 3 values with one sql question?
Yes, use IN clause.
SELECT FieldName , FieldValue
FROM `xy8vx_rsform_submission_values`
WHERE SubmissionId = '$id' AND FieldName IN ('Producent', 'Model', 'Nr seryjny')
And of course you need to use a loop to fetch the result.
// $sql is the above sql
$res = mysql_query($sql);
$result = array();
while ($row = mysql_fetch_assoc($res)) {
$result[$row['FieldName']] = $row['FieldValue'];
}
print_r($result);
To fetch the 3 values in 3 rows:
SELECT FieldValue
FROM `xy8vx_rsform_submission_values`
WHERE SubmissionId = '$id' AND
FieldName in ('Producent', 'Model', 'Nr seryjny')
or, to fetch the 3 values in 3 columns:
SELECT
(SELECT FieldValue
FROM `xy8vx_rsform_submission_values`
WHERE SubmissionId = '$id' AND FieldName = 'Producent') as Producent,
(SELECT FieldValue
FROM `xy8vx_rsform_submission_values`
WHERE SubmissionId = '$id' AND FieldName = 'Model') as Model,
(SELECT FieldValue
FROM `xy8vx_rsform_submission_values`
WHERE SubmissionId = '$id' AND FieldName = 'Nr seryjny') as NrSeryjny
Learn to and use the SQL IN operator.
$id_1=x;
$id_2=y;
$id_3=z;
$name_1="NAME X";
$name_2="NAME Y";
$name_3="NAME Z";
SELECT FieldValue
FROM `xy8vx_rsform_submission_values`
WHERE ((SubmissionId = $id_1 AND FieldName = $name_1) or (SubmissionId = $id_2 AND FieldName = $name_2) or (SubmissionId = $id_3 AND FieldName = $name_3));
Now, you have to fetch your result into an array of 3 rows.
a single query will produce result of these 3 query
$producent = mysql_fetch_array(mysql_query("
SELECT FieldValue
FROM `xy8vx_rsform_submission_values`
WHERE SubmissionId = '$id' AND (FieldName = 'Producent' OR FieldName = 'Model' OR FieldName = 'Nr seryjny')
"));
I´m trying to compare the rows, with the current date and hour,
$curDate = date("Y-m-d H").':00:00';
$query = "SELECT Id FROM Programacao WHERE Data1 = $curDate OR Data2 = $curDate OR Data3 = $curDate
OR Data4 = $curDate OR Data5 = $curDate OR Data6 = $curDate OR Data7 = $curDate";
$result = mysql_query($query);
if(!$result) {echo 'No program today';}
while ($row = mysql_fetch_array($result))
{
echo $row['Id'];
}
The row data in the database is like that: "2011-09-10 18:00:00"
But i always get : Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
any corrections?
You need quotes round your date literals in SQL:
"... WHERE Data1 = '$curDat' ..."
The resulting SQL should look like something like this:
... WHERE Data1 = '2010-12-17 15:00:00' ...
$result = mysql_query($query) or die(mysql_error());
will give you error needed to know what is wrong
in your case, you've forgotten quotes around dates
this one is better and will probably work
$query = "SELECT Id FROM Programacao WHERE Data1 = '$curDate' OR Data2 = '$curDate' OR Data3 = '$curDate'
OR Data4 = '$curDate' OR Data5 = '$curDate' OR Data6 = '$curDate' OR Data7 = '$curDate'";