-1

I am using PHP and Ajax to get Data. Data get successfully, but after loaded data at the bottom of loaded data showing "1111111". Why?

I try to find solution on different websites, but i can't. please help me. Advanced Thanks.

please see the attached image

Here is my PHP code (load_more.php) :

<?php
    include '../../includes/session.php';
    $conn = $pdo->open();
    $id= $_POST['last_product_id'];
    $output = '';
        $stmt = $conn->prepare("SELECT *, COUNT(*) AS numrows FROM products WHERE id < :id ORDER BY id DESC");
        $stmt->execute(['id'=>$id]);
        $row = $stmt->fetch();
        $totalRowCount = $row['numrows'];
        $showLimit = 6;
        try {
            $stmt = $conn->prepare("SELECT * FROM products WHERE id < :id ORDER BY id DESC LIMIT $showLimit");
            $stmt->execute(['id'=>$id]);
            foreach($stmt as $row){
                $product_id = $row['id'];
                    if (!empty($row['old_price'])){
                        $cart_wish_btn = '
                            <small> <del class="taka_sign_sm text-secondary">'.number_format($row['old_price'], 0).'
                            </del> <span class="ml-2 badge border border-success rounded text-success badge-sm">'.number_format($current_price,0).'% OFF</span></small>';
                        }
                $current_price = 100 -(($row['price'] / $row['old_price']) * 100) ;
                $image = (!empty($row['photo'])) ? '../../images/products/'.$row['photo'] : '../../images/noimage.jpg';
                
                
                $output .= ''.include ('../product/item_view_5.php').'';
                
                
            }
                 if($totalRowCount > $showLimit){ 
                     $output .= '  
                        <div class="show_more_main" id="show_more_main'.$product_id.'">
                            <div id="'.$product_id.'" class="show_more"></div>
                            <div class="loding" style="display: none;"><div class="spinner-border spinner-border-sm text-primary"></div></div>
                        </div>
                    ';
                 }
        }
        catch(PDOException $e){
            $output .= $e->getMessage();
        }

    $pdo->close();
    echo $output;
?>

Here is my AJax:

$(document).ready(function() {
        //$(document).on('click', '.show_more', function() {
            $(window).scroll(function(){
                var ID = $('.show_more').attr('id');
                if(($(window).scrollTop() == $(document).height() - $(window).height()) && (ID != 0)){
                    $('.show_more').hide();
                    $('.loding').show();
                    $.ajax({
                        type: 'POST',
                        url: "../product/load_more.php",
                        data: 'last_product_id=' + ID,
                        success: function(html) {
                            $('#show_more_main' + ID).remove();
                            $('.load_data_table').append(html);
                        }
                    })
                }
            })
        });

Here is my HTML:

<div class="row row-cols-2 row-cols-sm-4 row-cols-md-6 px-2 load_data_table"></div>
<div class="show_more_main" id="show_more_main<?php echo $product_id; ?>">
<div id="<?php echo $product_id; ?>" class="show_more"></div>
<div class="loding" style="display: none;"><div class="spinner-border spinner-border-sm text-primary">
</div>
</div>
</div>
123xyz
  • 1
  • 1
  • 1
    It could be in your line `$output .= ''.include (...` that [php include prints 1](https://stackoverflow.com/questions/5086695/php-include-prints-1). – Nigel Ren Nov 06 '20 at 18:14
  • Ok. But Now what is the solution??? – 123xyz Nov 06 '20 at 18:22
  • Depends on what you are expecting ../product/item_view_5.php to do. Does it echo the data of return it as a string? – Nigel Ren Nov 06 '20 at 18:23
  • ../product/item_view_5.php ... this file contains default bootstrap card layout to show data. – 123xyz Nov 06 '20 at 18:27

1 Answers1

0

include returns 1 which you assign to $output. You probably want to buffer and get the output of the included file:

ob_start();
include('../product/item_view_5.php');
$output .= ob_get_clean();

If you are doing this frequently then build a function with the above code:

function get_template($file) {
    ob_start();
    include($file);
    return ob_get_clean();
}

And use it thusly:

$output .= get_template('../product/item_view_5.php');
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87