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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.