Reputation: 12163
function displayList() {
$str = '';
$query = $this->db->query("SELECT * FROM data");
foreach ($query->result() as $row) {
$b = '<input name="completed" type="checkbox" />';
$a = $row->title;
$str = $b.$a;
}
return $str;
}
This script is only displaying the last field in the database. Why is this?
Upvotes: 0
Views: 72
Reputation: 24815
It is overwriting:
$str = $b.$a;
This string changes every loop again. If you want to make it an array, do this
$str[] = $b.$a;
If you want to add it to the text:
$str .= $b.$a;
Upvotes: 1
Reputation: 3644
it should be $str .= $b.$a;
You overwrite $str each time instead of adding new string at the end
Upvotes: 1
Reputation: 16373
Because you're not concatenating, you're reassigning. Do this:
$str .= $b.$a;
Otherwise the loop overwrites $str each time it runs, which explains why you're only seeing the last result.
Upvotes: 4