I'm trying to convert some ancient code to normal PHP standards (I've read about the issues with mysql functions...), I'm using Codeigniter as my framework, this is the original code:
$query_pag_data = "SELECT id,name from messages LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$msg = "";
while ($row = mysql_fetch_array($result_pag_data)) {
$htmlmsg=htmlentities($row['message']);
$msg .= "<li><b>" . $row['msg_id'] . "</b> " . $htmlmsg . "</li>";
}
$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data
This is my attempt:
$this->load->model("main_model");
$result=$this->main_model->user_list($start, $per_page);
$msg = "";
foreach ($result->result() as $row){
$htmlmsg = htmlentities($row->name);
$msg .= "<li><b>" . $row->id . "</b> " . $htmlmsg . "</li>";
}
$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data
My model:
function user_list($start, $per_page) {
$query = $this->db->get('users', $start, $per_page);
return $query;
}
This is not working correctly though, I believe it has something to do with the while and how mysql_fetch_array works, I've tried a few solutions with a while loop, but don't seem to be getting there.
A bit more info: This is for an ajax pagination feature, my attempt above loads up results, but the foreach loop is now loading previous results in next pages.