Jump to content

Pagination


chr0nos

Recommended Posts

Hello,

 

I have a function, this function uses switch to determine which kind of query to run, and after that is done, I use a simple variable:

$this->messages = $stmt->fetchAll(PDO::FETCH_ASSOC);

to hold all of the information (it is in my understanding that this variable holds it in an array). Anyways, when this function is called, I can use foreach to display the results, but my question is, is there a more efficient way of doing this? I am assuming using LIMIT and pagination would be better, but I have tried that and I came into a dead end because I had no idea on how to return multiple entries from one function.

function Message($username){

            $query = "SELECT * FROM msg WHERE `to` = '".$this->userid."' && `to_viewed` = '0' && `to_deleted` = '0' ORDER BY `created` DESC";   
            $stmt = $this->connection->prepare($query);
            $stmt->execute();

    $data = $stmt->fetch(PDO::FETCH_ASSOC);
    $num_rows = $stmt->fetchColumn();

        if($num_rows) {
            $i=0;
            $this->messages = array();
            while($row = $data) {
// want to return these 3 for each result
                $this->messages[$i]['id'] = $row['id'];
                $this->messages[$i]['title'] = $row['title'];
                $this->messages[$i]['message'] = $row['message'];

                $i++;
            }
        } else {
            return 1; 
        }
    }

If I want to return the 3 variables, then the iteration would stop, and wouldn't continue until the loop is done.. can anyone help me out? I have been bashing my head over this for a bit.

Link to comment
https://forums.phpfreaks.com/topic/281291-pagination/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.