简体   繁体   中英

an issue with php pdo mysql

I want to print last post from specific category
Could you please help me with the code?
I want to put on $record manual, for example: I put "design" , and just show the last post in design category .
And one thing: table blog it's separate from table record .

thanks

<?php $category = $record ['record']; { ?>
<?php foreach($db->query("select * from blog where category = '$category' order by id desc") as $row){ ?>
<li>
    <a href="<?php echo $row['image']; ?>">
        <div class="gallery-item"><img src="<?php echo $row['image']; ?>" alt="<?php echo $row['title']; ?>"></div>
    </a>
</li>
<?php } } ?>

If you want the most recent post, you could change your SQL to select it. Try something like this:

select * from blog where category = '$category' order by {DATE_FIELD} desc limit 1

You need to exchange the string {DATE_FIELD} with the actual date field in your table. This select would return the most recent dataset and only that one.

EDIT: You can also sort by youre id if the date isn't changed or the changed date is stored in another field.

select * from blog where category = '$category' order by id desc limit 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM