Dranreb Dranreb -4 years ago 141
HTML Question

php: WHILE continuous display of data

Okay I have a table called users and in that table contains:

Students Name: Grade:
1. Ben 1
2. Cathy 1
3. Tom 1
4. Mark 1
5. Lisa 1
6. Bernard 1
7. John 2
8. Macy 2
9. Rose 2
10. Maynard 2
11. Bill 2
12. Sherlock 2


So basically, I have 6 Grade 1 Students and 6 Grade 2 Students. I want to display them based on their grades using a two-columned Table:

----------------------------------
Grade 1
----------------------------------
Table 1 | Table 2
|
|


The purpose of this is I want to save space by not displaying all the Grades 1 in a single column, I want that table 1 and table 2 will each display 3 Grades 1 based on the records.

I tried using this code:

$sql = "SELECT * FROM users WHERE grade = '$grade1' LIMIT 3";
$result = mysqli_query($connectdb,$sql);

while($row=mysqli_fetch_assoc($result)) {
$name = $row['name'];
echo "<tr>";
echo "<td>";
echo $name;
echo "</td>";
echo "</tr>";

}


I placed this on the Table 1 and Table 2. It worked. However both tables displayed the same names:

----------------------------------
Grade 1
----------------------------------
Table 1 |Table 2
Ben | Ben
Cathy | Cathy
Tom | Tom


How can I make it using like this:

----------------------------------
Grade 1
----------------------------------
Table 1 |Table 2
Ben | Mark
Cathy | Lisa
Tom | Bernard


Forgive my lack of knowledge, I'm actually new to WHILE commands.

Answer Source

the problem is you are retrieving only 1st 3 students data and displaying it twice..

There are two ways 1st way

$sql = "SELECT * FROM users WHERE grade = '$grade1'";

$result = mysqli_query($connectdb,$sql);

$i=0;
while($row=mysqli_fetch_assoc($result)) {
 if($i==0 || $i==3){
   echo "<table>"
 }
   $name = $row['name'];
   echo "<tr>";
   echo "<td>";
   echo $name;
   echo "</td>";
   echo "</tr>";
 if($i==0 || $i==3){
    echo "</table>"
 }
$i++;
}

second way add one more, here you are retrieving the remaining 3 students data and need to display it in different table

$sql = "SELECT * FROM users WHERE grade = '$grade1' LIMIT 3,3";

$result = mysqli_query($connectdb,$sql);

while($row=mysqli_fetch_assoc($result)) {
   $name = $row['name'];
   echo "<tr>";
   echo "<td>";
   echo $name;
   echo "</td>";
   echo "</tr>";

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download