1

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

3
  • $value = chr(65+$x). Only works up to $x=25. Commented Jan 21, 2015 at 18:37
  • So are you trying to create 100 students divided across 4 classes? Or better stated X number of students divided into Y number of classes based on maximum of Z students per class? Why insert empty stings for id values? This seems problematic. Why names like 'A', 'B', 'C' for classes rather than something meaningful. If you just want to divide into Y number of classes, I would think integer value would suffice rather than applying arbitrary letters values (what happens after you exceed 26 classes?) Commented Jan 21, 2015 at 18:43
  • id auto increment. or do you have better code? *code edited Commented Jan 21, 2015 at 18:46

2 Answers 2

1
$range = range('A', 'Z');

$quota = 100;
$quotaperclass = 25;

for ($x = 0; $x <= $quota/$quotaperclass ; $x++) {
    $sql = "INSERT INTO classroom (id, name) VALUES ('', '".$range[$x]."')";

} 
Sign up to request clarification or add additional context in comments.

1 Comment

@MochamadSaepulM now - This was posted 6 mins. prior to that. If this works, consider accepting the answer.
0

Try this

$c = 'A';
for ($x = 0; $x <= $quota/$quotaperclass ; $x++) {
 $sql = "INSERT INTO classroom (id, name) VALUES ('', '$c')";
 $c++;
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.