2016-10-04 111 views
0
其他值

我有以下SQL从WordPress数据库的WordPress外取:循环通过数值基于PHP中

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包含字段名称,例如dateplace等。meta_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>元素,所以我猜测代码以某种方式用post_id为每行打印<li>元素,尽管并非所有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> 

回答

1

您需要有一个变量来存储帖子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'].' - '; 
} 
+0

嗨@ChristianF,谢谢。我已经使用一些基于您的新代码更新了原始帖子,但不幸的是,这些代码并不完全适用,但我认为这是正确的方向。 – hoegenhaug

+0

@hoegenhaug:在更新的例子中,第3行或第6行有一个拼写错误,我怀疑第6行。缺少的数据是因为您使用了'substr()',但是您没有在元素之间添加任何分隔符。因此,您可以删除最后一个元素的最后4个字符。 – ChristianF

+0

哦,是的,它应该是post_id,而不是m.post_id。我将如何在元素之间添加分隔符? – hoegenhaug