3

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.

0

1 Answer 1

2

try this:

function user_list($start, $per_page) {
   $query = $this->db->get('users', $per_page, $start);
   // flip $start/$per_page from your code
   return $query;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Gahhh, you're so right, damn you brain for forgetting Codeigniter syntax... will 'right' you up as soon as possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.