Reputation: 21
I'm trying to write a php script that queries a database for the semesters a user is registered for by first creating a while loop. Then I'm trying to create a for loop in order to determine the name and level of the classes they're registered for.
//USER ID
$id=1;
//SEMESTER QUERY
$qry="SELECT * FROM semesters where mem_id='$id'";
$result=mysqli_query($con, $qry);
//GRAB VALUES FROM CURRENT SEMESTER
while ($row=mysqli_fetch_array($result)){
$semesterID = $row[1];
echo '<div id="s'.$semesterID.'">';
//FOR LOOP TO GRAB ROWS['c1'] TO ['c7']
for ($i = 1; $i <= 7; $i++)
{
$class = $row['c'.$i.''];
//IF CLASS_ID IS VALID CONTINUE SEARCH
if ($class!=0){
$qry="SELECT * FROM allclasses where class_id='$class'";
$result=mysqli_query($con, $qry);
$row=mysqli_fetch_array($result);
$classname = $row[1];
$classlvl = $row[2];
echo ''.$classname.' '.$classlvl.'';
}
}
}
Database Information
The semesters table is composed as such:
| mem_id [0] |semnum [1] | semname [2] | c1 [etc...] | c2 | c3 | c4 | c5 | c6 | c7 |
In this php script I'm trying to:
row[1]
semesterID
for the current semester, then echo itrow['c1']
through row['c7']
for current semesterclassname
and classlvl
Problem:
The code actually works on the first for loop but afterward it display a Notice: Undefined Index
for c2
through c7
I've tried researching a solution but I've only seen this notice with a script trying to receive POST
values...and they recommended using isset and checking the variable but I'm not sure how it would apply in this case.
Upvotes: 1
Views: 634
Reputation: 3419
I think this line causes your trouble
$row=mysqli_fetch_array($result);
It's in your subloop and you overwrite your $row
variable of the outer loop so it's columns are not available in the runs from 2 to 7 anymore. Rename one of them.
Upvotes: 4