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

时间:2018-04-24 07:24:24

标签: php sql wordpress

我试图将两个不同行的值显示在一行中的两列中,但我无法弄清楚如何检索数据。

这就是我的尝试:

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

我显示如下数据:

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>

我得到了这个:enter image description here

虽然我想说明这一点:enter image description here

我不确定是否应该在PHP,WP或SQL类别中发布此内容。

2 个答案:

答案 0 :(得分:0)

使用此代码:

仅用此示例替换您的表

&#13;
&#13;
$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
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

  

您的抓取结果似乎是耦合的,因此我建议您在以下规则中打印抓取结果:

/* 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>
  

最后,根据您上面提供的样本数据,您会发现它打印的第一个foreach

&#34;#&#34; =&#39; 285&#39;

&#34;喃&#34; =&#39; Bouquet Opalin&#39;

&#34; Prix HT&#34; =&#39; 25&#39;

  

打印的第二个foreach:

&#34;库存&#34; =&#39; 9&#39;

  

我为你做了这个例子,点击下面的链接。

For your reference

Table Data Same yours

Table Result

/* 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>";
?>