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++;
}
Related
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." )");
}
}
I have a small problem with the request to the base of the PDO.
I want to insert or update fields in one request. I want insert a few records to DB, but if there are already 'file_name' fields with the selected name then just to update the other fields. Below paste my php code.
For example: I adding 5 records id_product (eg. 1) and various other fields.
Example fields recived from the $_post:
(id_product, alt, file_name, main_photo)
1, xxx, 1_1, 0;
1, xxx, 1_2, 0;
1, xxx, 1_3, 1;
my PHP code:
$query = "INSERT INTO product_image (id_product, alt, file_name, main_photo) VALUES ";
$part = array_fill(0, count($_POST['file_name']), "(?, ?, ?, ?)");
$query .= implode(",", $part);
$query .= " ON DUPLICATE KEY UPDATE alt=VALUES(alt), main_photo=VALUES(main_photo)";
$stmt = $this->_db->prepare($query);
$j = 1;
$im_null = 0;
for ($i = 0; $i < count($_POST['file_name']); $i++) {
$stmt->bindParam($j++, $_POST['id_product'], \PDO::PARAM_INT);
$stmt->bindParam($j++, $_POST['alt'][$i], \PDO::PARAM_STR);
$stmt->bindParam($j++, $profile->base64_to_jpeg($_POST['file_name'][$i], APP_ROOT . '/uploads/products/' . $_POST['id_product'] . '_' . $_POST['file_nr'][$i]), \PDO::PARAM_STR);
($_POST['main_photo'][$i] == 1) ? $stmt->bindParam($j++, $_POST['main_photo'][$i], \PDO::PARAM_INT) : $stmt->bindParam($j++, $im_null);
}
$stmt->execute();
In this query inserts works good, but doesn't update, which is the second part of the request.
Move $j = 1; inside loop as it is you keep on incrementing $j.
$im_null = 0;
for ($i = 0; $i < count($_POST['file_name']); $i++) {
$j = 1;
Following is my code. The first part works perfectly, but the second loop is not producing any results. What this is doing is that it looks for the timetable, then takes out the classes in that, copies all the that and makes a new timtable with same data but a different name.
The other for loop is to add students into the classes of the time table. Ca some one be kind enough and help me out in this as I have been hitting my head on the wall for it now for 5 days.
Thank you in advance. The code:
<?php
$Q = "INSERT INTO time_table(name, term, year) VALUES
('".$name."', '".$term."', '".$year."')";
$res = $db->query($Q);
//for generating the max table id
$sql2 = "select MAX(table_id) as table_id
from time_table
";
$res2 = $db->query($sql2);
$count2 = $res2->num_rows;
$row2 = $res2->fetch_assoc();
$table_id = $row2['table_id'];
$Q = "SELECT class_id as tcid, day as d, teacher_id as tei, location as l
FROM class_time_table
WHERE term='".$copy."'";
$res = $db->query($Q);
$num_results = $res->num_rows;
for ($i = 0; $i <$num_results; $i++) {
$row = $res->fetch_assoc();
$Q4 = "SELECT * FROM students_class WHERE class_id = '".$row['tcid']."' and term = '".$copy."'";
$res4 = $db->query($Q4);
$row2 = $res4->fetch_assoc();
//for generating the max table id
$class_sysq = "select MAX(class_sys_id) as class_sys_id
from students_class
";
$class_sysr = $db->query($class_sysq);
$count_class_sys = $class_sysr->num_rows;
$class_row = $class_sysr->fetch_assoc();
$class_sys_idf = $class_row['class_sys_id']+1;
$Q5 = "INSERT INTO students_class (class_sys_id, teachers_id, location, max_students, class_term_fee, class_name, class_sub_name, term, year) VALUES ('".$class_sys_idf."', '".$row2['teachers_id']."', '".$row2['location']."', '".$row2['max_students']."', '".$row2['class_term_fee']."', '".$row2['class_name']."', '".$row2['class_sub_name']."', '".$term."', '".$year."')";
$res5 = $db->query($Q5);
//for generating the max table id
$max_c_id = "select MAX(class_id) as ci
from students_class
";
$r_mci = $db->query($max_c_id);
$count_class_sys = $r_mci->num_rows;
$mci_row = $r_mci->fetch_assoc();
$max_c_idf = $mci_row['ci'];
$query2 = "INSERT INTO class_time_table(class_id, teacher_id, table_id, location, day, term, year) VALUES
('".$max_c_idf."', '".$row['tei']."', '".$table_id."', '".$row['l']."', '".$row['d']."', '".$term."', '".$year."')";
$result2 = $db->query($query2);
$student_q = "SELECT students.first_name as fn, students.last_name as ln, students.email as e, students.mobile_phone as mp, students.home_phone as hp, students.gender as g, students.dob as dob, students.term_fee as tf, students.join_date as jd, students.date_added as da, student_attending_class.class_id as ci FROM students, student_attending_class, class_time_table where students.student_sys_id = student_attending_class.student_id and student_attending_class.class_id = class_time_table.class_id and class_time_table.class_id = '".$row['tcid']."'";
$student_res = $db->query($student_q);
$student_num_results = $student_res->num_rows;
for ($i = 0; $i < $student_num_results; $i++) {
$theRow = $student_res->fetch_assoc();
//for generating the new system id
$sql3 = "select MAX(student_sys_id) as ssi
from students";
$res3 = $db->query($sql3);
$count3 = $res3->num_rows;
$row8 = $res3->fetch_assoc();
$student_system_num = $row8['ssi']+1;
$query10 = "INSERT INTO students(student_sys_id, first_name, last_name, email, mobile_phone, home_phone, gender, dob, fee_due, registration_fee, term_fee, fee_paid, join_date, date_added) VALUES
('".$student_system_num."', '".$theRow['fn']."', '".$theRow['ln']."', '".$theRow['e']."', '".$theRow['mp']."', '".$theRow['hp']."', '".$theRow['g']."', '".$theRow['dob']."', '".$theRow['tf']."', 0, '".$theRow['tf']."', 0, '".$theRow['jd']."', '".$theRow['da']."')";
$result10 = $db->query($query10);
$query11 = "INSERT INTO student_attending_class(class_id, student_id, waiting_list) VALUES ('".$max_c_idf."', '".$student_system_num."', '0')";
$result11 = $db->query($query11);
}
}
?>
Don't use $i in the second loop but $n for instance.
i m not sure but you used same variable $i in both loop so maybe because of that your second loop not working. try another variable $j in second loop.
The code is poorly formatted, so it's easy to miss that you're using the same variable for both loops, hence when the second loop begins, the first loses track of its progress. Use foreach(), or a different variable name for the second loop.
You are probably getting a timeout due to having nested loops which both use the same variable, $i and it just keeps incrementing.
Try changing the second loop like so:
for($j = 0; $j < $student_num_results; $j++){
...
}
I have a problem using sqlite_last_insert_rowid in PHP.
CODE:
$database = new SQLiteDatabase('example.db');
$sql_1 = "INSERT INTO ex1(test1, test2) VALUES ('$test1','$test2')";
$database->queryExec($sql_1);
$ex1_ID = sqlite_last_insert_rowid($database);
$sql_2 = "INSERT INTO ex2 (ex1_ID, fname, lname) values ('$ex1_ID','$fname','$lname');";
$database->queryExec($sql);
I want to store ID from ex1 table to table ex2 in ex1_ID
CREATE TABLE ex1 (
ID INTEGER PRIMARY KEY,
test1 LONGTEXT,
test2 LONGTEXT
);
CREATE TABLE ex2 (
ID INTEGER PRIMARY KEY,
ex1_ID INTEGER,
fname LONGTEXT,
lname LONGTEXT,
FOREIGN KEY ( ex1_ID ) REFERENCES ex1 ( ID )
);
COMPLETE CODE:
$database = new SQLiteDatabase('example.db');
for ($i = 0; $i < 10; $i++) {
$test1 = "";
$test2 = "";
$lenght = rand(300, 400);
for ($x = 0; $x < $lenght; $x++) {
$test1 .= rand(0, 9);
$test2 .= rand(0, 9);
}
$sql_1 = "INSERT INTO ex1 (test1,test2) VALUES ('$test1','$test2')";
$database->queryExec($sql_1);
$ex1_ID = sqlite_last_insert_rowid($database);
for ($j = 0; $j < 3; $j++) {
for ($x = 0; $x < $lenght; $x++) {
$test1 .= rand(0, 9);
$test2 .= rand(0, 9);
}
$sql_2 = "INSERT INTO ex2 (ex1_ID, fname, lname) values ('$ex1_ID','$test1','$test2');";
$database->queryExec($sql_2);
}
}
I cant get the last ID from a ex1 table.
Please help!
UPDATED now that we have more information.
Since you use an object-model way to query your DB, did you try
$lastid = $database->lastInsertRowid();
?
I have four arrays:
$qtys = $_POST['qty'];
$colours = $_POST['colour'];
$sizes = $_POST['size'];
$names = $_POST['name'];
I want these to go into a mysql table like so:
qty | colour | size | name
-------------------------------------------
$qtys[0] | $colour[0] | $size[0] | $name[0]
$qtys[1] | $colour[1] | $size[1] | $name[1]
I've tried various things, including this:
foreach($qtys as $value) {
$i = 0;
mysql_query("INSERT INTO table (qty,colour,size,name) VALUES ('$qtys[$i]','$colours[$i]','$sizes[$i]','$names[$i]')");
$i++;
}
...but to no avail. Can anyone suggest a different INSERT statement that will get the data in the table? Do I need to iterate in the loop? I'm sure $qtys as $value is incorrect but have tried so many things with no luck.
First of all, you are resetting the counter each iteration, put $i = 0 outside of the loop if you want to do it this way.
$i = 0;
foreach($qtys as $value) {
mysql_query("INSERT INTO table (qty,colour,size,name) VALUES ('$qtys[$i]','$colours[$i]','$sizes[$i]','$names[$i]')");
$i++;
}
A better (cleaner) way would be something like:
for ($i = 0; $i < $qtys.count(); $i++) {
mysql_query("INSERT INTO table (qty,colour,size,name) VALUES ('$qtys[$i]','$colours[$i]','$sizes[$i]','$names[$i]')");
}
Also, are you sure that the arrays are always the same length? Otherwise you risk getting a index out of bounds problem..
Simply use for loop will be ok, AND REMEMBER ESCAPE USER INPUT.
for($i =0, $count = count($qtys); $i < $count; $i++) {
mysql_query("INSERT INTO table (qty,colour,size,name) VALUES ('$qtys[$i]','$colours[$i]','$sizes[$i]','$names[$i]')");
}
$sql = "INSERT INTO table (qty,colour,size,name) VALUES ";
$inserts = array();
for($i=0 ; $i < count($qtys); $i++) {
$inserts[] = "('$qtys[$i]','$colours[$i]','$sizes[$i]','$names[$i]')";
}
$sql .= implode(',', $inserts);
mysql_query($sql);
No need to run query again and again.