Pagination.js plugin not fetching data from database

Hello all,
I am trying to utilize a pagination plugin from pagination.js.org. I am having a dickens of a time trying to get this plugin to work with my database. I thought since it is a plugin I would have an easier time. Does anybody here know how to get this plugin to work with content (mainly photos) in a database? I would appreciate the help.

This is the php page that queries the database. I just converted it to json format, thinking it would make a difference. Whether in json or just a plain query, It appears to be fine. I can go directly to the page and the content loads fine from the database.

//theajax.php
$sql = "SELECT memberphotos.*
FROM memberphotos 
LEFT JOIN 
users 
ON 
memberphotos.user_id = users.id",

$result = $db->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows); 


foreach($rows as $row) { 
        $memberphoto = $row['photo'];
        echo '<div><p>' . $memberphoto . '</p></div>';
}

Now here is the ajax call I am currently making. I have tried many different things here but this last one gives me undefined values versus no response, which I have had for several days. However nothing from the database is being displayed. Please note, much of this is taken from the pagination.js website. I still have not got my head around the proper way to use this thing yet.

(function(name) {
        var container = $('#pagination-' + name);
        container.pagination({                
            dataSource: function(done) {
                $.ajax({
                    type: 'GET',   //I have also tried 'POST'
                    url: 'theajax.php', 
                    dataType: 'json',
                    beforeSend: function() {
                        container.prev().html('Loading ...');
                    },
                    success: function(response) {
                        done(response);
                    }
                });
            },
            locator: 'data',
            totalNumber: 120,
            pageSize: 2,
            showGoInput: true,
            showGoButton: true,
            formatGoInput: 'go to <%= input %>',
            
            callback: function(response, pagination) {
                window.console && console.log(22, response, pagination);
                var dataHtml = '<ul>';
                
                $.each(response, function (index, item) {
                    dataHtml += '<li>' + item.title + '</li>';
                });                    
                dataHtml += '</ul>';                    
                container.prev().html(dataHtml);
            }
        })
    })('demo2');

The HTML markup for the pagination is simply from the pagination.js.org website.

<section>        
    <div class="data-container"></div>
    <div id="pagination-demo2"></div>
</section>

If this is not enough information to determine the problem, I am more than happy to supply what is needed. Thanks in advance. Cheers.

Is there some reason you are not doing traditional pagination, on the server, where any searching/filtering would occur, and where the total number of matching rows is known, which determines the correct number of links to produce (you will notice that links in the demo examples reading flickr data don’t actually have the correct number of pages and the links all display the same page of data)?

The main use of this code is to get all the matching data and then paginate that data in the browser. This is only suitable for a relatively small number of items (< 200.)

To use this code to get data from a database, in a general-purpose way, you would need to use the dataSource URL choice, to get just the requested page of data, and use the totalNumberLocator, which should actually make an ajax request to the base dataSource URL, if this is even possible, to get the total number of matching rows.

That is interesting. I have not been able to find that limitation in the jquery or php manual. Can you point out the link to me so I can research that 200 item limitation further?

Definition:

adjective

  1. right or appropriate for a particular person, purpose, or situation.

It’s not a technical limitation, it’s a resource, performance, and user experience issue.

I found the problem…thanks. It is simply a front end pagination plugin. I just needed to know if anybody here had any experience in getting it to work. Perhaps not… Thanks anyway.

One other issue with CLIENT-SIDE pagination is that you keep sending calls to the server thru code that any person can hack. Just view-source of the page and you can see the AJAX calls. This is how hackers gain access to the server. They can create their own AJAX call to possibly send bad code to the database system. SERVER-SIDE pagination is more secure.

If one does not use server side validation then ALL code (PHP or AJAX) can be hacked regardless of technologies being used.

PHP can NOT be hacked as it is SERVER-SIDE only. Just AJAX calls to the PHP can be. You can NOT view PHP CLIENT-SIDE since it never makes it into the browser!

You should go into comedy, of course it can be hacked. Please do a little fact checking. Why do you think people use PDO to slow down hackers??? Read what is happening in the world.

Not comedy. You are wrong my friend. Those hacked systems are “repositories” of code. They store code in databases and therefore just the data was hacked. A huge difference in hacking database data and PHP code which can not be seen in a browser. SolarWinds is a repository of code and that is data. The data can be hacked, not the code on the site itself. ( Unless they stored a copy of the website code inside the repository.

If you store or display PHP code in a website, like the one your are reading on now, that PHP code can be hacked or just viewed. But, the PHP code on the server here can not be read directly or hacked since it is never seen in the browser.

I guess you are either Jerry Seinfeld or Jesus Christ of Nazareth turned programmer. If you can prove to me that raw php code without prepared statements, zero server side validation, zero sanitizing, zero tokens, relatively un-expiring cookies, weak or no password, or any security measures, can not be hacked, I will personally, by hand deliver to you the pink slip to my 2020 488 Ferrari. AND have my brother send you his hard earned degree in information security from USC.

I did NOT say it can not be hacked. I said you can’t see PHP from the server. Only whatever is sent into the browser. And, since JS is client-side, anyone can see that. You are reading more into what I said. View source of almost any page on the internet and you will see why they use for JS. Not secure, in my opinion!

This is an exact quote of what you said. It is time to put the gonja down sir. It is causing you harm.

I’m not going to argue this any further. The PHP runs on the server and never gets to a browser.
Yes, hackers can get into a server, but, that is hacking a server, not a website. Terminology may be
making you not understand what I said. I was here to help you, but, you just want to argue…

We will just have to agree to disagree on this matter sir.

Sponsor our Newsletter | Privacy Policy | Terms of Service