Home fetch data from database for specific web pages
Reply: 0

fetch data from database for specific web pages

user2609
1#
user2609 Published in April 26, 2018, 1:46 pm

I am creating a website in which people can post some thing with expiry date. And i am storing all data in database with expiry date so on pages only valid data will be appeared and expired wont show on pages. Currently I am using this php script

    <?php
$servername = "******";
$username = "********";
$date=date("Y/m/d");
try {
    $conn = new PDO("mysql:host=$servername;dbname=******", $username, "*******");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $query = "select id, item1, item2, item3, expirydate, description from table WHERE expirydate>= ?";
    $statement = $conn->prepare($query);
    $statement->execute(array($date));
        foreach($statement->fetchAll(PDO::FETCH_ASSOC) as $row){
            echo"<div class='col-md-12'>";
                echo"<div class='agent-item'>";
                    echo"<div class='row'>";
                        echo"<div class='col-md-3'>";
                            echo"<img src='img/uploads/$row[item1]' class='img-responsive' alt=''>";
                        echo"</div>";
                        echo"<div class='col-md-6'>";
                            echo"<h6 class='mb-xs'>Description</h6>";
                            echo"<p>'$row[item2]'</p>";
                        echo"</div>";
                        echo"<div class='col-md-3'>";
                            echo"<p>address</p>";
                        echo"</div>";
                    echo"</div>";
                echo"</div>";
            echo"</div>";
        }
    }
catch(PDOException $e)
    {
        echo "Connection failed: " . $e->getMessage();
    }
?>

The Problem is that fetchAll fetches all data which i really don't want. I want to show 10 items per pages so i want to fetch 10 items and when user clicks next page so on page 2 i want to fetch next 10 items and show them on page 2. I am struggling for 2 days to achieve this but i failed every time even though i tried only fetch() method as well but it iterates only once in foreach loop. Can some one help me out how can i fetch only 10 items and on next page how can i fetch 10 more items which should be start after previous 10 items.

Please note: to track the items, choosing the id is not best choice because expiry date vary, that's mean if id 2 user have expiry date after 1 week so id 3 user have expiry date before that.

Thanks in advance

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302387 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO