Whys is this creating extra tags?

I’m using PHP to generate the html for a table. The problem is that if there are n+1 rows generated on the table the code is generating n empty table row tags, all of them appearing prior to the rows made from the database info.

[php]
function getGameTable(){
$table ="";
$query = “SELECT player1, player2, winner, points, league, date, location FROM games”;
if($stmt = $this->connect->prepare($query)){
$stmt->execute();
$stmt->bind_result($player1, $player2, $winner, $points, $league, $date, $location);
$table = “”;
while($stmt->fetch()){
$table = $table. “

”.$player1."".
“”.$player2."".
“”.$points."".
“”.$league."".
“”.$date."".
“”.$location."";
$table = “”.$table."";
}
}
$stmt->close();
return $table;
}
[/php]

if there were 2 database rows the webpage output here looks like this minus my comment:

[php]

player1 player2 points league date location player1 player2 points league date location [/php]

So if there were 100 database rows i end up with 99

lines is didn’t want and they all appear at the beginning. What am I missing?

That is odd, but because of the way the code is written, it’s quite tricky to follow. Try replacing this:

[php] $table = “”;
while($stmt->fetch()){
$table = $table. “

”.$player1."".
“”.$player2."".
“”.$points."".
“”.$league."".
“”.$date."".
“”.$location."";
$table = “”.$table."";
}[/php]

with this:

[php] $table = “”;
while($stmt->fetch()){
$table .= “

”.$player1."".
“”.$player2."".
“”.$points."".
“”.$league."".
“”.$date."".
“”.$location." ";
}[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service