gpt4 book ai didi

php - 在 PHP 中根据其他值循环遍历值

转载 作者:行者123 更新时间:2023-11-29 11:15:19 24 4
gpt4 key购买 nike

我有以下 SQL 从 WordPress 外部的 WordPress 数据库获取:

SELECT p.post_name, m.meta_key, m.meta_value FROM wp_postmeta m INNER JOIN wp_posts p ON m.post_id = p.post_name WHERE p.post_type = 'something' LIMIT 0, 100;

post_name wp_posts 中包含特定帖子的 id,并匹配 post_idwp_postmetameta_key包含字段名称,例如 date , placemeta_value包含每个字段的值。

因此,每个帖子都有一组字段 ( meta_key ),每个字段都有一个值 ( meta_value )。

我试图为每篇文章循环遍历这些内容,但我尝试过的都没有成功。现在我有了这个,它显示了所有数据,但它循环遍历meta_values,而不是显示每个post_id 的meta_values。

$data = $result->fetch_assoc();
if ($result = mysqli_query($conn, $sql)) {
while ($row = $result->fetch_assoc()) { ?>
<li>
<?php
echo $row["meta_value"];
?>
</li>
<?php
} $result->close();
}
?>

这给了我:

<li>Post 1, meta value 1</li>
<li>Post 1, meta value 2</li>
<li>Post 2, meta value 1</li>
<li>Post 2, meta value 2</li>

但我需要

<li>Post 1, meta value 1 — Post 1, meta value 2</li>
<li>Post 2, meta value 1 — Post 2, meta value 2</li>

你有什么想法吗?

<小时/>

根据@ChristianF 的评论进行更新。 foreach 不起作用,所以我尝试使用 while 代替。

结果有点奇怪,例如。帖子 1 中的元值 1 重复了很多次。有很多空的<li>元素也是如此,所以我猜测代码会以某种方式打印 <li>具有 post_id 的每行元素,即使并非所有 meta_values 都需要打印。我使用 if 语句包含了其中几个。我已将生成的 HTML 插入到代码下方。

while ($row = $result->fetch_array()) {
if (!isset ($oldID)) {
$oldID = $row['post_id'];
}

if ($oldID != $row['post_id']) {
// Removing the last 3 characters from the output,
// as we don't have any more items after this one.
$postOut = substr ($postOut, 0, -4)."</li>\n<li>";
}

if ($row['meta_key'] == 'who_what') {
$postOut .= $row['meta_value'];
} else if ($row['meta_key'] == 'place') {
$postOut .= $row['meta_value'];
}

echo $postOut;
}

结果是巨大的,而且似乎也砍掉了一些值。我没有包含完整的结果。

<li></li>
</li>
<li></li>
</li>
</li>
<li>Meta_value from who_what key</li>
</li>
</li>
<li></li>
<li></li>
</li>
</li>
<li></li>
</li>
<li>Full meta_value from place key</li>
</li>
</li>
<li></li>
</li>
<li>Meta_value from place key missing last four characters</li>
<li></li>
</li>
</li>
<li></li>
</li>
<li>Meta_value from place key missing last four characters</li>
</li>
<li></li>
</li>
</li>
<li></li>
</li>
<li>Meta_value from place key missing last four characters</li>
</li>
</li>
<li></li>
</li>
</li>
<li></li>

最佳答案

您需要有一个变量,用于存储帖子 ID。然后检查每一行,如果与当前行不同,则打印一个新行。
换句话说:

// Start output with a list element.
$postOutput = '<li>':

foreach ($res->fetchArray () as $row) {
// Prime the old ID, so that we can check against it later on.
if (!isset ($oldID)) {
$oldID = $row['post_id'];
}

// First, we need to end the old list item if we have a new post.
if ($oldID != $row['post_id']) {
// Removing the last 3 characters from the output,
// as we don't have any more items after this one.
$postOut = substr ($postOut, 0, -3)."</li>\n<li>";
}

// Adding the list element with the expectation of more
// content to be atted to it later on.
$postOut .= "Post #, meta ".$row['meta'].' - ';
}

关于php - 在 PHP 中根据其他值循环遍历值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com