man5 Posted February 24, 2014 Share Posted February 24, 2014 So I am working with a script that allows load more scrolling, much like youtube's comments section. The script I found uses mysqli and I am using PDO. So I need a bit of help converting some code below. Origional mysqli $results = mysqli_query($connecDB,"SELECT COUNT(*) FROM paginate"); $get_total_rows = mysqli_fetch_array($results); //total records //break total records into pages $total_pages = ceil($get_total_rows[0]/$item_per_page); My PDO code $results = DB::getInstance()->query("SELECT COUNT(*) FROM guestbook"); $get_total_rows = $results->results(); //break total records into pages $total_pages = ceil($get_total_rows[0]/$item_per_page); With the code above, I get the following error. "Object of class stdClass could not be converted to int" Quote Link to comment https://forums.phpfreaks.com/topic/286490-need-help-converting-mysqli-query-to-pdo/ Share on other sites More sharing options...
ginerjm Posted February 24, 2014 Share Posted February 24, 2014 YOu have posted code that is unfamiliar to me. What is 'db::getinstance'? What is '$results=>results()'? And which line is giving the error? Quote Link to comment https://forums.phpfreaks.com/topic/286490-need-help-converting-mysqli-query-to-pdo/#findComment-1470484 Share on other sites More sharing options...
man5 Posted February 24, 2014 Author Share Posted February 24, 2014 (edited) Here are the two functions in a DB.php class. That's where I am getting those from. and the line that's giving me error is " $get_total_rows = $results->results();" public static function getInstance() { if(!isset(self::$_instance)) { self::$_instance = new DB(); } return self::$_instance; } public function query($sql, $params = array()) { $this->_error = false; if($this->_query = $this->_pdo->prepare($sql)) { $x = 1; if(count($params)) { foreach($params as $param) { $this->_query->bindValue($x, $param); $x++; } } if($this->_query->execute()) { $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); $this->_count = $this->_query->rowCount(); } else { $this->_error = true; } } return $this; } Edited February 24, 2014 by man5 Quote Link to comment https://forums.phpfreaks.com/topic/286490-need-help-converting-mysqli-query-to-pdo/#findComment-1470487 Share on other sites More sharing options...
man5 Posted February 24, 2014 Author Share Posted February 24, 2014 No one else? Quote Link to comment https://forums.phpfreaks.com/topic/286490-need-help-converting-mysqli-query-to-pdo/#findComment-1470533 Share on other sites More sharing options...
man5 Posted February 25, 2014 Author Share Posted February 25, 2014 So i sort of fixed the problem with this code. $get_total_rows = $results->count(); and it works if I remove [0] from the code below. $total_pages = ceil($get_total_rows[0]/$item_per_page); The only problem with that is, after getting second row of entries, the "load more" button becomes unactive. Quote Link to comment https://forums.phpfreaks.com/topic/286490-need-help-converting-mysqli-query-to-pdo/#findComment-1470554 Share on other sites More sharing options...
Solution man5 Posted February 25, 2014 Author Solution Share Posted February 25, 2014 Btw here is the full javascript for it. <script type="text/javascript"> $(document).ready(function() { var track_click = 0; //track user click on "load more" button, righ now it is 0 click var total_pages = <?php echo $total_pages; ?>; $('#results').load("fetch_pages.php", {'page':track_click}, function() {track_click++;}); $(".load_more").click(function (e) { //user clicks on button $(this).hide(); //hide load more button on click $('.animation_image').show(); //show loading image if(track_click <= total_pages) //make sure user clicks are still less than total pages { //post page number and load returned data into result element $.post('fetch_pages.php',{'page': track_click}, function(data) { $(".load_more").show(); //bring back load more button $("#results").append(data); //append data received from server //scroll page to button element $("html, body").animate({scrollTop: $("#load_more_button").offset().top}, 500); //hide loading image $('.animation_image').hide(); //hide loading image once data is received track_click++; //user click increment on load button }).fail(function(xhr, ajaxOptions, thrownError) { alert(thrownError); //alert any HTTP error $(".load_more").show(); //bring back load more button $('.animation_image').hide(); //hide loading image once data is received }); if(track_click >= total_pages-1) { //reached end of the page yet? disable load button $(".load_more").attr("disabled", "disabled"); } } }); }); </script> Quote Link to comment https://forums.phpfreaks.com/topic/286490-need-help-converting-mysqli-query-to-pdo/#findComment-1470556 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.