-1

我正在使用 Wordpress,我必须弄清楚如何从同一列中获取多个值并将它们转换为变量。

最后一步是我只希望数据显示在一个表格中,如下所示:

Bob Company1 <br>
Alex Company2

相反,我得到Bob Alex Company1 Company 2要么

Bob <br>
Alex<br>
Company1<br>
Company2

这是我正在使用的两个不同版本:

$sql = "SELECT meta_value as guest from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID  = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guest_name' UNION SELECT meta_value as company from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guests_company'";
$result = mysql_query($sql);
$NumberOfResults=mysql_num_rows($result);

if (mysql_num_rows($result) == 0) {
    echo "No Guests yet... stay tuned!";
    exit;
}

while(list($guest,$company)= mysql_fetch_row($result)) 
{ 
    echo "<table><tr><td>".$guest."</td><td>".$company."</td></tr></table>";
}

或者

$sql = mysql_query("SELECT meta_value as guest from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guest_name' LIMIT 2 UNION SELECT meta_value as company from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guests_company'");

$i = 1;
while ($get = mysql_fetch_array($sql))
{
    echo '<table><tr><td>'.$get["guest"].'</td><td>'.$get["company"].'</td></tr></table>';

    $i++;
}

任何帮助将不胜感激!!!谢谢!

编辑:如果将来有人想使用 Wordpress 中的高级自定义字段创建自己的小部件,这里或多或少是最终产品:

$sql = "SELECT p.ID AS post_id, g.meta_value as guest, c.meta_value as company, d.meta_value as date
FROM wp_posts p
JOIN wp_postmeta g ON g.post_id = p.id AND g.meta_key = 'guest_name'
JOIN wp_postmeta c ON c.post_id = p.id AND c.meta_key = 'guests_company'
JOIN wp_postmeta d ON d.post_id = p.id AND d.meta_key = 'show_date'
WHERE p.post_status = 'publish'
ORDER by d.meta_value DESC";

$query = mysql_query($sql);


echo '<table><tr><th>Guest</th><th>Company</th><th>Show Date</th></tr>';
while ($get = mysql_fetch_array($query)) {$newDate = date("m-d-Y", strtotime($get["date"]));
    echo '<tr><td><a href="http://yoururl.com/?p='.$get["post_id"].'">'.$get["guest"].'</a></td><td>'.$get["company"].'</td><td>'.$newDate.'</td></tr>';
}
echo '</table>';

非常感谢 doublesharp 在这方面的帮助。

4

2 回答 2

1

如果我正确理解您的问题,您必须编写代码以显示如下表格:

echo '<table>';
while ($get = mysql_fetch_array($sql))
{
    echo '<tr><td>'.$get["guest"].'</td><td>'.$get["company"].'</td></tr>';

}
echo '</table>';
于 2012-10-18T04:21:08.810 回答
0

如果 postmeta 值附加到不同的帖子,如查询中不同的“post_type”值所示,那么您将需要一些其他识别信息将它们链接在一起。如果有一个 post_id 同时具有 aguest_nameguest_companymeta_value 那么您可以加入它两次以获得结果。使用 aUNION总是会导致它们以不同的行返回。

// Join the postmeta table to posts twice, once for each meta_key. 
$sql = <<<SQL
SELECT p.ID AS post_id, g.meta_value as guest, c.meta_value as company
FROM wp_posts p
JOIN wp_postmeta g ON g.post_id = p.id AND g.meta_key = 'guest_name'
JOIN wp_postmeta c ON c.post_id = p.id AND c.meta_key = 'guests_company'
WHERE p.post_status = 'publish'
SQL;

// Execute the query
$query = mysql_query($sql);

// Start your table for output
echo '<table>';
while ($get = mysql_fetch_array($query)) {
    // Write the output of each row
    echo '<tr><td>'.$get["guest"].'</td><td>'.$get["company"].'</td></tr>';
}
// Close the table
echo '</table>';

如果这是在 Wordpress 中,我建议您使用该$wpdb对象进行查询。

于 2012-10-18T05:28:55.927 回答