amirreza99 Posted January 23, 2015 Share Posted January 23, 2015 hi every body. i want a loop that takes and shows more than one record from database. and then i want to show these to user by ajax. indeed i made an html page that when user arrives at end it must do this. first i made loop that took one record from database and it worked fine. but for taking several records it went wrong. this is my html file that has interact by user webgoo-ajax.html and this is my php file that interacts by database ajax-get.php when user arrives at end of page naturally ajax must be run and show several records to user.but it does'nt. first it stays idle for 3 minutes and then loads my first record extremely. and a point: if these pages does'nt work on IE it will work on Firefox because it did'nt worked in IE for me too. i just want a for loop that work correctly for my need. thank you for everything Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 23, 2015 Share Posted January 23, 2015 (edited) This will cause an infinity loop. The condition $print=$printed+3; will always return TRUE. Maybe you meant $print<=$printed; for ($print=$printed; $print=$printed+3; $print++) I fail to see why you need the for loop. Because even if you did construct the loop correctly it you will find you'll get duplicate results being returned. Is the purpose of the loop to limit how many results are returned from your query? If so then you should use the LIMIT clause Edited January 23, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 23, 2015 Share Posted January 23, 2015 Relevant code for others: <?php if(isset($_GET['printed'])) { $printed=$_GET['printed']; $connection = mysqli_connect('localhost','root','','database') or die(mysqli_error($connection)); for ($print=$printed; $print=$printed+3; $print++) { $selectquery = "SELECT content,author FROM topics WHERE id='$printed'"; $selectresult = mysqli_query($connection,$selectquery); $selectcontent= mysqli_fetch_array($selectresult); $selectquery = "SELECT query FROM posts WHERE query='$printed'"; $selectresult = mysqli_query($connection,$selectquery ); $selectnumanswer= mysqli_num_rows($selectresult); echo $selectcontent ['content']; echo "".$selectcontent ['author']; echo "".$selectnumanswer; echo "<form method=GET action=answer-writing.php> <input type=text name=answer> <input type=hidden name=query value=$printed> <input type=submit name=answerbutton value=answerthis> </form>" ; } } else { echo '<META HTTP-EQUIV="Refresh" content="0; url=http://localhost/web-ajax.html">'; } ?> <script type="text/javascript"> var printed=1; function loadFile() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.write(xmlhttp.responseText); } } xmlhttp.open("GET","ajax-get.php?printed="+printed,true); xmlhttp.send(); } //]=]=> </script> Quote Link to comment Share on other sites More sharing options...
amirreza99 Posted January 24, 2015 Author Share Posted January 24, 2015 (edited) This will cause an infinity loop. The condition $print=$printed+3; will always return TRUE. Maybe you meant $print<=$printed; for ($print=$printed; $print=$printed+3; $print++)I fail to see why you need the for loop. Because even if you did construct the loop correctly it you will find you'll get duplicate results being returned. Is the purpose of the loop to limit how many results are returned from your query? If so then you should use the LIMIT clause Thank you for answer;but i changed comparation of variables as you said and it loaded only first record just like first time,that i told it in post above. Totally my problem is that i want to make a page that user visits and when user arrived at the end of page it loads several records from database at the continuance.but it's not enough to fetch one record from database,it must be several to strech lenght of page to give user an space to arrive at the end of page again. You can see my mean at yahooanswer.i want to do like that exactly. I just want a solution to make this.and i think it doesn't need mysql commands.just maybe you dont get my question. and thank you again and another question wich type of array i must use?fetch array or fetch assoc?maybe this made my code wrong. Edited January 24, 2015 by amirreza99 Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted January 24, 2015 Solution Share Posted January 24, 2015 Totally my problem is that i want to make a page that user visits and when user arrived at the end of page it loads several records from database at the continuance.but it's not enough to fetch one record from database,it must be several to strech lenght of page to give user an space to arrive at the end of page again.You can see my mean at yahooanswer.i want to do like that exactly. I just want a solution to make this.and i think it doesn't need mysql commands.just maybe you dont get my question. Ok, So you after an infinite scroll effect, whereby new content is loaded automatically when the user scrolls to the end of the page/html element. Basically in your PHP code you want to first implement a technique called pagination, but instead of outputting all the links to each page you only output the link for the next page. Now using javascript when you detect the user has scrolled to the bottom of the page, you then look for the link for the next page and get its url. Once you have the url you would do an ajax request to get the next page data from that url and append its contents to the current page. And so the overall effect will look like the content is continually scrolling. Couple of tutorials I found http://www.1stwebdesigner.com/infinite-scrolling-tutorial/ http://www.w3bees.com/2013/09/jquery-infinite-scroll-with-php-mysql.html You can find more by goggling terms such as "PHP Pagination inifante scroll" "Javascript infinite scroll" etc. Quote Link to comment 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.