Ajax, php, mysql not working mysql limit
Posted
by
Hofmeister Ákos
on Stack Overflow
See other posts from Stack Overflow
or by Hofmeister Ákos
Published on 2014-08-24T10:18:31Z
Indexed on
2014/08/24
10:19 UTC
Read the original article
Hit count: 263
I have 3 files.
list.php
$articles = $mysqli->query("SELECT mainPictureBig, title, writer, writeDate, link FROM articles WHERE category=$this->category ORDER BY writeDate DESC LIMIT 0,10");
while($article = mysqli_fetch_row($articles))
{
echo "<a href=\"".$this->url."/".$article[3]."/".$article[4]."\"><div id=\"listElement\">
<div id=\"listElementWallpaper\" style=\"background-image: url('category/img.jpg');\"></div>
<div id=\"listElementContent\"><div id=\"listElementTitle\">".$article[1]."</div>".$this->giveWriter($article[2]).", ".$this->giveDate($article[3])."</div>
</div></a>";
}
$maximumElements=ceil($numberOfContent / 10) * 10;
It's working, so there is no problem, it lists the first 10 elements from the sql table, and i got the $numberOfCOntent part also. Than i have a button:
echo "<div id=\"listMore\"><div id=\"buttonOne\" onclick=\"listMore($this->category,$maximumElements)\">Load more</div></div>";
There is also no problem, i load the .js file, and it looks like:
var from = 10;
function listMore(categoryId, maximum)
{
$( "#listMore" ).slideUp( 200, function() {
$( "#listMore" ).html("<center>Loading..</center>");
$( "#listMore" ).slideDown( 500, function() {
$.post( "http://localhost/ajax.php", {type: "listMore", id: categoryId, sqlFrom: from} )
.done(function( elements )
{
$("#listBody").append( elements );
if(maximum > from+10)
{
from = from+10;
$( "#listMore" ).slideUp(200, function() {
$( "#listMore" ).html("<div id=\"buttonOne\" onclick=\"listMore("+categoryId+","+maximum+")\">Load more</div>");
$( "#listMore" ).slideDown(200);
});
}
else
$( "#listMore" ).slideUp(200);
});
});
});
}
And it's also working, and the problem is in the PHP file. As you can see, i'm sending an integer the "from" variable, the PHP file:
$articles = $mysqli->query("SELECT mainPictureBig, title, writer, writeDate, link FROM articles WHERE category=$id ORDER BY writeDate DESC LIMIT $from,10");
It's also working, but not loading the next 10, only the next 9. So it skips the very next row and loads the rows only after the very first, so only 9. I tried to write here only the important part of the code, so i skipped some echo part etc. Any idea?
© Stack Overflow or respective owner