yeah, you just made a simple mistake (although I do like how you're doing the columns). Here is your original code:
$i = 1;
$cols = 3;
echo "<table border='1' width='100%'><tr>";
while ($row=mysql_fetch_array($query)) {
if ($i > $cols){
echo "</tr><tr>"; //create new row
$i = 1; //reset counter;
}
else{
echo "<td>" . $row["lname"] . "</td>";
$i++;
}
}
echo "</table>";
You made two mistakes. Firstly, look at the results you are getting. It's skipping 4, 8, 12, 16, and so on. Now, that means it's the part where if $i > $cols, right? $cols = 3, and this is happening where $cols = 4
Taking a look at that, you end the row, create a new one, but don't put a <td> to insert the persons data. All you do is create a new row <tr>, but don't input anything into it, which is why it was skipping over that record.
The second problem: you set $i = 1 (when you reset the counter). Now think about this. If $i = 4, and the max cols is 3, that means when $i = 4, that is equivalent to $i equaling 1. Which means after you put that record in, $i would have to equal 2 (because when it was at 4, that was your first result). Hopefully my explanation makes sense. Anyways, here's the corrected code:
$i = 1;
$cols = 3;
echo "<table border='1' width='100%'><tr>";
while ($row=mysql_fetch_array($query)) {
if ($i > $cols) {
echo "</tr><tr>"; //create new row
echo "<td>" . $row["lname"] . "</td>";
$i = 2; //reset counter
} else {
echo "<td>" . $row["lname"] . "</td>";
$i++;
}
}
echo "</table>";
Cgraz