Guest User

Untitled

a guest
Apr 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. $qu = $db->prepare("SELECT * FROM comment ORDER by id desc");
  2.  
  3. $pageNum = $_GET['page'];
  4. $qu = $db->prepare("SELECT * FROM comment ORDER by id desc limit 0, $pageNum");
  5.  
  6. <script src="js/jsFunctions.js" type="text/javascript"></script>
  7. <script src="build/jquery.min.js"></script>
  8.  
  9. <script src="build/react.min.js"></script>
  10. <script src="build/react-dom.min.js"></script>
  11. <script src="build/browser.min.js"></script>
  12. <script type="text/babel">
  13.  
  14. //CommentList Component
  15.  
  16. var CommentList = React.createClass({
  17. getInitialState:function(){
  18. //setting initiall values
  19. //attach the scroll event listener to our scroll handler function
  20. window.addEventListener("scroll", this.handleScroll);
  21. return{
  22. comments:[], //comment array initially we have zero comment
  23. page:0, //for pagination
  24. loadingFlag:false, //to avoid multiple fetch request if user is keep scrolling
  25. url:"loaddata.php" //url to fetch comments in json format
  26. }
  27.  
  28. },
  29.  
  30. getComment:function(){
  31. //method to fetch comments will concat result to state.comment
  32. var nextPage = this.state.page+10; //display 10 records from databse on each scroll
  33.  
  34. $.get(this.state.url+"?page="+this.state.page, function(result) {
  35.  
  36. //$.get(this.state.url+"?page="+this.state.page, function(result) {
  37. if (this.isMounted()) {
  38. this.setState({
  39. // comments: this.state.comments.concat(result), //concating to existing comments
  40. comments: this.state.comments.concat(result), //concating to existing comments
  41. loadingFlag:false,
  42. page:nextPage
  43. });
  44.  
  45. loading("off");
  46. }
  47. }.bind(this));
  48. },
  49. componentDidMount: function(){
  50. //to load comments initially
  51. loading("on");
  52. this.getComment();
  53. },
  54. handleScroll:function(e){
  55. //this function will be triggered if user scrolls
  56. var windowHeight = $(window).height();
  57. var inHeight = window.innerHeight;
  58. var scrollT = $(window).scrollTop();
  59. var totalScrolled = scrollT+inHeight;
  60.  
  61. if(totalScrolled+100>windowHeight){ //user reached at bottom
  62.  
  63. if(!this.state.loadingFlag){ //to avoid multiple request
  64. this.setState({
  65. loadingFlag:true,
  66. });
  67. loading("on");
  68. this.getComment();
  69. }
  70.  
  71. }
  72. },
  73. render:function(){
  74. //creating comment node
  75. var CommentNode = this.state.comments.map(function (comment) {
  76. return (
  77. <div>
  78. Comment:{comment.content}<br/>
  79. Auther:{comment.author}<hr />
  80. </div>
  81. );
  82. });
  83. return (
  84. <div> {CommentNode} </div>
  85. );
  86. }
  87.  
  88.  
  89. });
  90. ReactDOM.render(
  91. <CommentList />,
  92. document.getElementById('component')
  93. );
  94.  
  95.  
  96. </script>
  97.  
  98. <?php
  99. header('Content-Type: application/json');
  100. $pageNum = $_GET['page'];
  101. //$pageNum = 16;
  102. //sleep(2);
  103. include("db.php");
  104. $insert = $db->prepare("INSERT INTO page_data(pagename)values(:pagename)");
  105. $insert->execute(array(
  106. ':pagename' => $pageNum
  107.  
  108. ));
  109. $qu = $db->prepare("SELECT * FROM comment ORDER by id desc limit 0, $pageNum");
  110.  
  111. $qu->execute(array());
  112. $gco = $qu->rowCount();
  113.  
  114. while($row = $qu->fetch()){
  115.  
  116. $data[] = array("id"=>$row['id'],"content"=>$row['content'],"author"=>$row['author']);
  117.  
  118. }
  119. echo json_encode($data);
  120. ?>
  121.  
  122. //This js file have nothing to do with React, only Loading function is defined here
  123. var loading = function(action) {
  124. // add the overlay with loading image to the page
  125. if(action=="on"){
  126. var over = '<div id="overlay">' +
  127. '<img id="loading" src="loader.gif" >' +
  128. '</div>';
  129. //$(over).appendTo('body');
  130. $('body').append(over);
  131. $('html, body').css("cursor", "wait");
  132. console.log("creating overlay");
  133.  
  134.  
  135. }
  136. else if(action=="off"){
  137. $("#overlay").remove();
  138. $('html, body').css("cursor", "auto");
  139. console.log("removing overlay");
  140.  
  141. }
  142.  
  143.  
  144. };
Add Comment
Please, Sign In to add comment