I want to divide student group-wise and store them in database
students 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15.....20
Example: Total no.of students 20
Make 4 students per group (1,2,3,4 group1 and 5,6,7,8 group2 etc.....)
Then there should 5 groups created
i tried like this where the mistake i am doing in for loops.
$studentQuery = $conn->query("
select s.student_pid,i.email,s.student_email,s.student_fname,
s.student_lname,s.profile_pic from r_job_invitations i
LEFT JOIN
tbl_students s
ON
i.email = s.student_email
where i.id_job = ".$jobID." and inv_res = 1");
$totalIndividuals = mysqli_num_rows($studentQuery);
$groups = round($totalIndividuals/5,0,PHP_ROUND_HALF_DOWN);
for ($j = 1; $j <= $groups; $j++) {
for ($i = 0; $i <= $totalIndividuals; $i++) {
$GroupsQuery = $conn->query("INSERT INTO
r_test_group (student_id,job_id,group_name)
values ('".$studentResults['student_pid']."',
'".$jobID."','Group'".$j." )");
}
}
$stuData = array();
while($studentResults = $studentQuery->fetch_array()) {
$stuData[] = $studentResults;
}
$totalIndividuals = mysqli_num_rows($studentQuery);
$groups = round($totalIndividuals/$gd_individuals,0,PHP_ROUND_HALF_DOWN);
$count=0;
for($j = 0; $j<$groups; $j++) {
for($i =0; $i< $gd_individuals; $i++) {
if($stuData[$count]['student_pid']) {
echo "INSERT INTO r_test_group (student_id,job_id,group_name) values (".$stuData[$count]['student_pid'].", ".$jobID.",Group".$j." )";
$count++;
}
}
}
exit;
first forloop is to divide groups.
Second forloop is to divide for students.
You could use flooring to translate a student id to a group id:
INSERT INTO r_test_group
(student_id,job_id,group_name)
SELECT student_pid, id_job, CONCAT('group', FLOOR((student_id - 1) / 4) + 1))
FROM tbl_students s
JOIN r_job_invitations j ON j.email = s.student_email
Considering there is no gaps between the records
select Concat('group',(your_col - 1 ) / 4 + 1 )
From yourtable
Note : This will not work when there is gaps. If you have gaps then you need to generate row number for each row then replace then student_id with row number in my query.
I think your code giving you more student then you are looking for. in your code in first loop its running for number of groups time then for each group in next for loop its running for all students in each group.
$limit = (int)($totalIndividuals/$groups);
for($j =0; $j<$groups;)
{
for($i =0; $i<$totalIndividuals; $i++)
{
if($i % $limit == 0)
$j++;
$GroupsQuery = $conn->query("INSERT INTO r_test_group (student_id,job_id,group_name) values ('".$studentResults['student_pid']."', '".$jobID."','Group'".$j." )");
}
}
Related
I have this code and i would like to get total combination of all categories
my code so far
for ($k = 0; $k < 4; $k++) {
$result= $DB->query("SELECT total FROM ".$DB->prefix("mystat")." WHERE year='$year' AND category='$categoryname[$k]'");
$row = $DB->fetchArray($result);
$total=$row['total'];
echo $total++;
}
let say i have this data
A - 1
B - 2
C - 3
my current output
123
my desired output
6
How do i correct this ?
It is because you are doing echo in loop. And also you logic is wrong. change your code like:
$total = 0;
for ($k = 0; $k < 4; $k++) {
$result= $DB->query("SELECT total FROM ".$DB->prefix("mystat")." WHERE year='$year' AND category='$categoryname[$k]'");
$row = $DB->fetchArray($result);
$total +=$row['total'];
}
echo $total; // DO echo here
Also if you don't need categories and total separately and only need sum of all then its better to use SUM with group by category in sql.
i have code like
$quota = 100;
$quotaperclass = 25;
for ($x = 0; $x <= $quota/$quotaperclass ; $x++) {
$sql = "INSERT INTO classroom (name) VALUES ('A')";
for ($y = 0; $y <= 25 ; $x++) {
$sql = "INSERT INTO classroomstudent (studen_id, class_id) VALUES ('', 'A')";
}
}
I want if quota per class 25, total quota div quota per class. in my case result is 4 and then automated create 4 class like A, B, C, D and then add every student to the class.
my question is.
how to change value name automated like
loop 1 is A
loop 2 is B
loop 3 is C
etc
and how to add every student into class automatically.
thanks
$range = range('A', 'Z');
$quota = 100;
$quotaperclass = 25;
for ($x = 0; $x <= $quota/$quotaperclass ; $x++) {
$sql = "INSERT INTO classroom (id, name) VALUES ('', '".$range[$x]."')";
}
Try this
$c = 'A';
for ($x = 0; $x <= $quota/$quotaperclass ; $x++) {
$sql = "INSERT INTO classroom (id, name) VALUES ('', '$c')";
$c++;
}
I have a table (table3) in MYSQL with 99 text fields (field1, filed2, ...,field99). I wanted to count the non-empty values of each field so I wrote a simple php script with a for loop as below:
for($i = 1; $i <= 99; $i++)
{
$sqlstm = "SELECT COUNT(field$i) FROM table3 WHERE field$i IS NOT NULL AND field$i <> '';";
$r = #mysqli_query($dbc, $sqlstm);
if($r)
{
while($row = #mysqli_fetch_array($r))
{
echo "<p>$row[0]</p>\n";
}
}
else
{
echo "field $i error: " . mysqli_error($dbc);
}
}
But the loop stopped after showing four values (field1 to filed4) and no error message. I do have all 99 fields with data in table3 and I could run the query manually for any field. Could this due to browser timeout?
Can someone help me? Thanks.
$sqlstm = "SELECT COUNT(field$i) FROM table3
you are selecting the columns here, your table has 99 rows but apperantly only 4 columns
what you could better do is
$sqlstm = "SELECT * FROM table3 WHERE (column name) IS NOT NULL";
$count = 0
for($sqlstm as $one) {
$count = $count + 1;
}
echo($count);
or something along those lines
I want to show the array value $result[] from the for loop calculation. However, it shows me nothing on the page. Is there is anything wrong in the below code?
$sql= "SELECT * FROM items where itemID =3 ";
$result1= mysql_query($sql) or die (mysql_error());
while ($row= mysql_fetch_array($result1)){
$quantity[] = $row ['quantity'];
$month[] = $row ['month'];
}
$alpha = 0.3;
for($i = 1; $i > 12; $i++){
$result[$i] = ($quantity[$i] - $result[$i-1]) * $alpha + $result[$i-1];
}
foreach ($result as $key => $value ){
echo "$value";
}
Your for loop has an error. You have
for($i = 1; $i > 12; $i++)
but it should be
for($i = 1; $i < 12; $i++)
This is not directly the answer to your question, but there are few things that hasn't been mentioned that concern the way you query and process your data:
Your SELECT statement doesn't have specific order specified. Since order of records is not preserved you can get records out of correct order and get invalid calculations. Use ORDER BY (e.g. ORDER BY month) or make use of month values and extract exactly previous month's value from array(s) (if it is what you're doing in your code).
Your current code relies on the fact that the resultset from DB will contain (at least) 12 records. If for some reason it will produce less records your for loop will brake.
It's uncertain from the information in the question but it looks like you might need a year in your query unless the table contains records only for one year.
Now, you can calculate the whole thing on DB side with a query like this
SELECT i.month,
COALESCE((i.quantity - p.quantity) * 0.3 + p.quantity, 0) value
FROM items i LEFT JOIN items p
ON i.itemID = p.itemID
AND i.`year` = p.`year`
AND i.month = p.month + 1
WHERE i.itemID = 3
AND i.`year` = 2013
ORDER BY month
SQLFiddle
That's assuming (and I'm not sure about that) you actually need to read previous month's quantity values for your calculations and month column is of integer type
There is an obvious flaw in the logic. You try to get the $i index form $quantity. However, you can't be sure $quantity will have this index.
Supposing that itemId is not the primary key, I would do something like this:
$sql = "SELECT * FROM `items` WHERE `itemID` = 3";
$result1= mysql_query($sql) or die (mysql_error());
while ($row= mysql_fetch_assoc($result1)){
$quantity[] = $row ['quantity'];
}
$alpha = 0.3;
$i = 1
foreach ($quantity as $elem) {
if ($i >= 12)
break;
$result[$i] = ($elem - $result[$i-1]) * $alpha + $result[$i-1];
$i++
}
foreach ($result as $value ){
echo $value;
}
I have a database full of php time(); stored when a user logs in.
I know I should have created a value for their username and updated the login count but instead I have a database full of php time(); with their username
Here is how I find the amount of logins for individual users:
<?php
function to_date($timestamp) {
return date('Y-m-d', $timestamp);
}
$days = array();
$occurences = array();
$zero = array();
$query = mysql_query("SELECT `login_time` FROM `stats` WHERE `userid`='$someuserid' ORDER BY `login_time` ASC");
while($rows = mysql_fetch_array($query)) {
$days[] = to_date($rows['login_time']);
}
$days[] = to_date(time());
$occurences = array_count_values($days);
$days = array_unique($days);
sort($days);
for($i = 0; $i < count($days); $i++) {
$difference = isset($days[$i+1]) ? strtotime($days[$i+1]) - strtotime($days[$i]) : 0;
if($difference > 86400) {
$difference = ceil(abs($difference/86400));
$fill = $days[$i];
for($k = 0; $k < $difference-1; $k++) {
$fill += strtotime('+1 day', strtotime($fill));
$zero[] = to_date($fill);
}
}
}
echo "[";
for($i = 0; $i < count($zero); $i++) {
echo "[\"".$zero[$i]."\",0], ";
}
for($i = 0; $i < count($occurences); $i++) {
if($i == count($occurences)-1)
echo "[\"".$days[$i]."\",".($occurences[$days[$i]]-1)."]";
else {
echo "[\"".$days[$i]."\",".$occurences[$days[$i]]."], ";
}
}
echo "]";
?>
How could I apply this to all the users and find out who logged in the most for each day in the database? The output is encoded for jqplot.
SELECT
userid,
DATE(FROM_UNIXTIME(login_time-MOD(login_time,86400)+1)) as logindate,
count(*) as logincount
FROM stats
GROUP BY userid, logindate
Use the above query to get all the required data directly from the database and this will help you avoid doing the post processing using PHP
Additionally, if you would like to get only one record, which have maximum logins in a (any) day, the use the below query with an added SQL wrapper for the above query:
SELECT * FROM (
SELECT
userid,
DATE(FROM_UNIXTIME(login_time-MOD(login_time,86400)+1)) as logindate,
count(*) as logincount
FROM stats
GROUP BY userid, logindate
) tmptable
ORDER BY logincount DESC, logindate DESC LIMIT 1
Let me know if this helped you.
SELECT userid, COUNT(userid) AS 'login_count' FROM stats GROUP BY userid
All the answer may be correct or incorrect. But I know one thing that my answer is not correct.But in the case of Max logins , there is no need of such an answer. Thats all I know.