简体   繁体   English

php pdo mysql 的问题

[英]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 .我想放$record手册,例如:我放了"design" ,只显示design category 中的最后一篇文章。
And one thing: table blog it's separate from table record .还有一件事: table blog它与 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.如果您想要最新的帖子,您可以更改您的 SQL 以选择它。 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.您需要将字符串 {DATE_FIELD} 与表中的实际日期字段交换。 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.编辑:如果日期未更改或更改的日期存储在另一个字段中,您还可以按您的 ID 进行排序。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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