简体   繁体   English

在同一表的同一列中回显不同的值

[英]Echo different values in the same column in the same table

I'm trying to display the values from two different rows into two columns in a single row but I can't figure out how to retrieve the data. 我正在尝试将两行中的值显示为同一行中的两列,但是我不知道如何检索数据。

This is what I tried: 这是我尝试的:

SELECT wp_posts.*, wp_postmeta.*
FROM wp_posts
LEFT JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.post_type='product' AND wp_postmeta.meta_key='_sale_price'
OR wp_postmeta.meta_key='_stock'
ORDER BY wp_posts.ID

I display the data like this: 我显示如下数据:

foreach($sth as $row)
{
?>
 <tr>
  <td><?php echo $row["ID"]; ?></td>
  <td><?php echo $row["post_title"]; ?></td>
  <td><?php echo $row["meta_value"]; ?></td>
  <td><?php echo $row["meta_value"]; ?></td>
 </tr>

I'm getting this: 我得到这个: 在此处输入图片说明

While I'd like to get this: 虽然我想得到这个: 在此处输入图片说明

I'm not sure whether I should post this in the PHP, WP or SQL category. 我不确定是否应该将其发布在PHP,WP或SQL类别中。

use this code: 使用此代码:

only replace your tables with this example 仅用此示例替换表

 $sql = "SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID=Orders.CustomerID ORDER BY Customers.CustomerName"; $result = mysqli_query($con, $sql); while ($row = mysqli_fetch_assoc($result)) { <tr> <td><?php echo $row["ID"]; ?></td> <td><?php echo $row["post_title"]; ?></td> <td><?php echo $row["meta_value"]; ?></td> <td><?php echo $row["meta_value"]; ?></td> </tr> ///your code here } 

It seems that your fetch results are coupled, so I'd suggest that you print the fetch results in the rule below: 看来您的提取结果是耦合的,所以我建议您按照以下规则打印提取结果:

/* Print this when $row is in odd number */
 <tr>
  <td><?php echo $row["ID"]; ?></td>
  <td><?php echo $row["post_title"]; ?></td>
  <td><?php echo $row["meta_value"]; ?></td>

/* Print this when $row is in even number */
  <td><?php echo $row["meta_value"]; ?></td>
 </tr>

In the end, according to the sample data you provide above, you will find that the 1st foreach it prints 最后,根据上面提供的示例数据,您将发现它打印的第一个foreach

"#" = '285' “#” ='285'

"Nom" = 'Bouquet Opalin' “ Nom” ='花束蛋白石'

"Prix HT" = '25' “ Prix HT” ='25'

2nd foreach it prints: 打印的第二个foreach:

"Stock" = '9' “股票” ='9'

I did the example for you, click the link below. 我为您做了示例,请单击下面的链接。

For your reference 供你参考

表数据相同

表结果

/* added at 2018-04-26 */

<?php
    /*  include your dbconfig   */
    include_once('../sit/dbConfig.php');

    $dbConfig = new dbConfig();

    $query = array();
    $query[0] = "SELECT * FROM wp_posts WHERE 1;";

    $column = " ID, post_title, meta_value ";
    $table = " wp_posts ";

    $arr = array();
    $arr = $dbConfig->sqlSelect1($column, $table);

    echo "<pre>";
    echo "1) Print raw array data which get from DB" . "<br><br>";
    print_r( $arr );
    echo "</pre>";
    echo "<br><br>";
?>  


<?php
    echo "<pre>";
    echo "2) Print in table format" . "<br><br>";

    echo "<table border='1' width=device-width >";

    echo "<tr>";
    echo "<td>#</td>";
    echo "<td>Nom</td>";
    echo "<td>Prix HT</td>";
    echo "<td>Stock</td>";
    echo "</tr>";

/* Print this when $row is in even number */
    foreach ($arr as $row => $key) {
        if ($row % 2 == 0) {
            echo "<tr> <td>";
                    echo $key["ID"];
            echo "</td> <td>";
                    echo $key["post_title"];
            echo "</td> <td>";
                    echo $key["meta_value"];
            echo "</td>";
        }


/* Print this when $row is in odd number */
        if ($row % 2 == 1) {
            echo "<td>";
                    echo $key["meta_value"];
            echo "</td> <tr>";
        }
    }

    echo "</table>";
    echo "</pre>";
?>

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

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