Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In below code, I display three posts in table, means three posts in single row with load more button at their bottom now problem here is when I click show more the next row should have next three posts, but the first post of next row is second post of first row, and when ever I click show more same happens with rest of row, every time the first post of new row is second post of previous row. Looks like javascript is catching id of first post instead of last in a row so how to make it right?
JavaScript
<script type="text/javascript">
$(document).ready(function(){
$(document).on('click','.show_more',function(){
var ID = $(this).attr('id');
$('.show_more').hide();
$('.loding').show();
$.ajax({
type:'POST',
url:'ajax_more.php',
data:'id='+ID,
success:function(html){
 $('#show_more_main'+ID).remove();
 $('.posts').append(html);
}
});        
});
});
</script>

PHP
<?php
$sql = "SELECT * FROM posts ORDER BY id desc limit 3";
$query = $db->prepare($sql);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
$ID = $row['blogs_id'];
?>

<div class="posts">
<table>
<tr>
<?php do { //horizontal looper?>
<td>
<div><h2><?php echo $row['title']; ?></h2></div>          
<div><p><?php echo $row['body']; ?></p></div>
<img src='<?php echo $row['pic']; ?>'>
<div><p><?php echo $row['about']; ?></p></div>
</td>
<?php
$row = $query->fetch(PDO::FETCH_ASSOC);
if (!isset($nested_List)) {
$nested_List= 1;
}
if (isset($row) && is_array($row) && $nested_List++%3==0) {
echo "</tr><tr>";
}
} while ($row); //end horizontal looper 
?>
</table>
<div class="show_more_main" id="show_more_main<?php echo $ID; ?>">
<span id="<?php echo $ID; ?>" class="show_more" title="Load more posts">Show more</span>
<span class="loding" style="display: none;"><span class="loding_txt">Loading...</span></span>
</div>
</div>


ajax_more.php

if(isset($_POST["id"]) && !empty($_POST["id"])){ 
$id=$_POST['id']; 
$query = $db->prepare("SELECT * FROM posts WHERE id < :getid ORDER BY id desc LIMIT 3");
$query->execute(array(':getid'=>$id));
$row = $query->fetch(PDO::FETCH_ASSOC);
$ID = $row['blogs_id'];
?>

<table>
<tr>
<?php do { //horizontal looper?>
<td>
<div><h2><?php echo $row['title']; ?></h2></div>          
<div><p><?php echo $row['body']; ?></p></div>
<img src='<?php echo $row['pic']; ?>'>
<div><p><?php echo $row['about']; ?></p></div>
</td>
<?php
$row = $query->fetch(PDO::FETCH_ASSOC);
if (!isset($nested_List)) {
$nested_List= 1;
}
if (isset($row) && is_array($row) && $nested_List++%3==0) {
echo "</tr><tr>";
}
} while ($row); //end horizontal looper 
?>
</table>
<div class="show_more_main" id="show_more_main<?php echo $ID; ?>">
<span id="<?php echo $ID; ?>" class="show_more" title="Load more posts">Show more</span>
<span class="loding" style="display: none;"><span class="loding_txt">Loading…</span></span>
</div>
<?php 
} 
?>
Posted
Updated 29-Oct-15 1:18am
v3

1 solution

Try using this
var ID = $($(this).attr('id')).val();


or this

<span id="tableid" value="<?php echo $ID; ?>" class="show_more" title="Load more posts">Show more</span>


and in your JS
var ID = $('#tableid').val();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900