Jump to content

a for loop that fetches several fields by ajax from database


amirreza99
Go to solution Solved by Ch0cu3r,

Recommended Posts

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                                        ^_^

Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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 by amirreza99
Link to comment
Share on other sites

  • Solution

 

 

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.