加载所有数据后删除“加载更多”按钮

I'm trying to use a button to load more data from database with PHP. Till now I can count the results and the results that are showing. So when $count==$countAll, all the results are normally showing.

Can someone explain why this isn't working?

// Count all results
$allResults = $conn->prepare("SELECT*FROM tl_picture WHERE text LIKE '%$innerhtml%' ORDER BY id DESC");
$allResults->execute();
$countAll =$allResults->rowCount();
echo "Found results: ".$countAll."<br>";

//max 20 results showing
$statement = $conn->prepare("SELECT*FROM tl_picture WHERE text LIKE '%$innerhtml%' ORDER BY id DESC  limit 20");
$statement->execute();
$collection = $statement->fetchAll();
$count =$statement->rowCount();
echo "viewable results: ". $count;
<script>
<?php if($count==$countAll): ?>
        document.GetElementById('loadButton').style.display='none';
    } else {
        document.GetElementById('loadButton').style.display='block';
    }
    <?php endif; ?>
</script>

What you are trying to achieve is normally done with ajax calls. So the check for the visibility of the Load More button must be done with javascript after every asynchronous load. PHP runs on server side, once before sending the HTML document to your browser and doesn't help at all in this case.

Also you execute the sql query for all rows and then you do it again with limit this time. It is inefficient to run twice the same query just to get the number of all records.

The logic should be that you load 20 rows and each row should have an identifier. Using the identifier of the last loaded row, you ajax request the next 20 rows etc. If response data from ajax is empty, then you hide the Load More button (or disable it and set text to something like "No more records").

You might have an error in this script:

<script>
    <?php if($count==$countAll): ?>
        document.GetElementById('loadButton').style.display='none';
    } else {
        document.GetElementById('loadButton').style.display='block';
    }
    <?php endif; ?>
</script>

You might change it to something similar to:

<script>
    <?php if($count==$countAll): ?>
        document.GetElementById('loadButton').style.display='none';
    <?php } else { ?>
        document.GetElementById('loadButton').style.display='block';
    <?php endif; ?>
</script>

The problem might be that your } else { is in JavaScript, yet there might be no if for it?