javascript - Jquery infinity scroll loops over and over using Mysql/PHP -
i'm trying implement jquery infinity scroll found @ (http://infiniteajaxscroll.com/) mysql , php. , works, sort of.
i found similar example here quite different anyway (http://www.w3bees.com/2013/09/jquery-infinite-scroll-with-php-mysql.html)
the loading more works fine results being looped on , on again. mean display results in first page , when scroll down @ bottom , infinity scroll fires same results being showed instead of splitting first results in different pages.
here code, quite long :)
<?php $page = (int) (!isset($_get['s'])) ? 1 : $_get['s']; // shoutbox $sql = "select * shoutbox order id desc limit 20"; //prepare statement $statement = $dbconn->prepare($sql); //execute statement $statement->execute(); //count shouboxes $number_of_shoutbox = $statement->rowcount(); $number_of_posts_per_page = '10'; $total_pages = $number_of_shoutbox / $number_of_posts_per_page; ?> <div class="background_spacing"></div> <!-- end of background spacing --> <div id="header_background"> <div class="shoutbox-background text-center"> <div id="shoutbox"> <!-- shoutbox complete starts --> <div class="shoutbox_complete"> <div class="shoutbox_left"> <img src="images/girl-shoutbox.jpg"> </div> <div class="shoutbox_right background-white"> <div class="no-padding padding10"> <span class="font24 font-color-000000">napisi svoj</span> <span class="font24 username_male">shoutbox</span> <textarea class="shoutbox-form no-padding" placeholder="ostavite vi jedan shoutbox..."></textarea> <input type="submit" class="button_standard_pink float-right"> </div> </div> </div> <!-- shoutbox complete starts --> </div> <!-- end of id="shoutbox --> </div> </div> <!-- end of header_background --> <div class="margin30"></div><!-- spacing --> <div class="shoutbox-total"> <div id="shoutbox"> <div class="shoutbox-counter no-padding padding-10 background-shoutbox-counter"> <div class="font48 text-center"><?php count_shoutbox();?></div> <div class="text-center">shoutbox has been shouted!</div> <div class="text-center padding-10"><img src="images/default/default-shoutbox-shout.png"></div> <div class="text-center font10">invite friends shout!</div> </div> </div> <? //loop through shouboxes while($show_shoutbox = $statement->fetch(pdo::fetch_both)) { ?> <div id="shoutbox"> <!-- shoutbox complete starts --> <div class="shoutbox_complete_display"> <div class="shoutbox_left"> <img src="images/man-shoutbox.jpg"> </div> <div class="shoutbox_right-display background-box-light-gray"> <div class="no-padding padding-10"> <div class="font24 username_male" style="line-height:1;"><?php echo strtoupper($show_shoutbox['user_name']);?> <?php echo $show_shoutbox['user_age'];?> godine</div> <span class="font-color-898a8b">iz <?php echo strtoupper($show_shoutbox['country']);?> - traŽi <?php echo strtoupper($show_shoutbox['gender_search']);?> izmeĐu <?php echo strtoupper($show_shoutbox['age_from']);?> <?php echo strtoupper($show_shoutbox['age_to']);?> godina</span> <div class="margin15"></div> <div class="font20"><?php echo $show_shoutbox['text'];?></div> <div style="margin-top:52px;"> <table cellpadding="0" cellspacing="0" width="100%"> <tr> <td><div class="font18 font-bold">32,222</div> <span class="font-color-898a8b">achivements</span> </td> <td><div class="font18 font-bold">32,222</div> <span class="font-color-898a8b">achivements</span> </td><td><div class="font18 font-bold">32,222</div> <span class="font-color-898a8b">achivements</span> </td><td><div class="font18 font-bold">32,222</div> <span class="font-color-898a8b">achivements</span> </td> </tr> </table> </div> <!-- end of margin-top:60 --> </div> <!-- end of no-padding padding-10 --> <!-- comment start here --> <?php // d($detail); $type='question'; // include('like.php'); include('includes/comment.php'); ?> <!-- comment end here --> </div> <!-- end of shoutbox_right-display --> </div> <!-- end of shoutbox_complete --> </div> <!-- end of id="shoutbox --> <?php } // end of shoubox loop ?> </div> <!--page navigation--> <div id="nav"> <?php ($x = 2; $x <= $total_pages; $x++) { ?> <a href='welcome.php?p=shoutbox&s=<?php echo $x;?>' <?php if($x=='2') { echo 'class="next"';}?>></a> <?php } ?> </div> <script type="text/javascript" src="js/jquery.infinity.scroll.js"></script> <script type="text/javascript"> var ias = $.ias({ container: ".shoutbox-total .shoutbox_complete_display", item: "#shoutbox", pagination: "#nav", next: ".next" }); ias.extension(new iasspinnerextension()); ias.extension(new iastriggerextension({offset: 3})); ias.extension(new iasnoneleftextension({text: 'there no more pages left load.'}));
======================================================================== update code still giving errors in mysql:
tried @rob schmuecker solution getting errors in mysql.
here modified code pdo , rob schmuecker suggestion
// shoutbox $sql = "select shoutbox.shoutbox_id, shoutbox.text, shoutbox.date, shoutbox.time, shoutbox.approved, shoutbox.`new`, shoutbox.user_id, users.profile_image, users.user_name, users.user_age, users.country, users.gender_search, users.age_from, users.age_to shoutbox inner join users on shoutbox.user_id = users.id users.profile_image = '2' , shoutbox.approved = '1' , shoutbox.new = '0' , shoutbox.user_id != :admin_id , (shoutbox.user_id not in (select user_id users_blocked blocked_id = :user_id)) order shoutbox.shoutbox_id desc limit :limit_start :limit_row_amount"; //prepare statement $statement = $dbconn->prepare($sql); //execute statement $statement->execute(array( 'admin_id' => $admin_id, 'user_id' => $user_id, 'limit_start' => $limit_start, 'limit_row_amount' =>$limit_row_amount ));
you need implement page sql query.
below define 2 new variables, 1 total amount want retrieve each time , next amount of rows skip in each limit query, starting 0 if $page = 1
$page = (int) (!isset($_get['s'])) ? 1 : $_get['s']; $limit_row_amount = 20; if($page > 1){ $limit_start = $page * $limit_row_amount; } else { $limit_start = 0; } // shoutbox $sql = "select * shoutbox order id desc limit $limit_start, $limit_row_amount";
Comments
Post a Comment