我将如何输出这个表?

发布于 2024-12-28 07:28:46 字数 1130 浏览 0 评论 0原文

我正在尝试输出某些产品的价格,这些产品均来自不同的供应商。产品位于行中,供应商位于列中。这似乎是一件很容易做的事情,但我不知道如何使用 PHP 来做到这一点。

产品存储在产品表中,供应商存储在供应商表中,价格存储在定价表中。有一些共同的 ID 字段将它们全部链接起来。定价表中的示例记录可能是:

product_id = 5, category_id = 2, vendor_id = 8, product_price = $5.50

在我的表中,供应商在各列中的顺序始终相同。但是,如何构建查询和 PHP 才能获得输出?我知道这是一个基本问题,但我对此比较陌生,需要一些帮助。如果您能帮我输出如下所示的表格,请先致谢。

如下:

pricesmodel_idvendor_idcategory_idprice

排序)

链接

我的

信息

表结构

编辑

网站以获取更多

用于

vendorsvendor_idvendor_namemodelsmodel_idmodel_namemodel_link

product_categoriescategory_idcategory_namecategory_order

<h2>Pet Foods</h2>
<table>
   <tr>
      <td>Product</td>
      <td>Vendor 1</td>
      <td>Vendor 2</td>
      <td>Vendor 3</td>
   </tr>

   <tr>
      <td>Cat Food</td>
      <td>$5</td>
      <td>$6</td>
      <td>$5.50</td>
   </tr>
</table> 

I'm trying to output prices for some products, all from different vendors. The products are in the rows and the vendors are in the columns. It seems like an easy enough thing to do but I can't figure out how I'd do this with PHP.

The products are stored in a products table, the vendors are in a vendor table, prices are in a pricing table. There are common ID fields that link them all up. A sample record from the pricing table might be:

product_id = 5, category_id = 2, vendor_id = 8, product_price = $5.50

In my table, the vendors will always be in the same order across the columns. But, how do I structure the query and PHP so I can get that output? I know this is a basic question but I'm relatively new to this and need some help. Thanks in advance if you can help me output a table like the one below.

Edit: My table structure is as follows:

product_categories

category_id

category_name

category_order (used to sort)

vendors

vendor_id

vendor_name

models

model_id

model_name

model_link (link to the website for more info)

prices

model_id

vendor_id

category_id

price

<h2>Pet Foods</h2>
<table>
   <tr>
      <td>Product</td>
      <td>Vendor 1</td>
      <td>Vendor 2</td>
      <td>Vendor 3</td>
   </tr>

   <tr>
      <td>Cat Food</td>
      <td>$5</td>
      <td>$6</td>
      <td>$5.50</td>
   </tr>
</table> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2025-01-04 07:28:46
$sql="SELECT product_id, product_price FROM pricing ORDER BY product_id, vendor_id";
$res=mysql_query($sql,$link);
if(mysql_num_rows($res)>0){
    $pId='';
    echo "<table><tr><td>Product</td><td>Vendor 1</td><td>Vendor 2</td><td>Vendor 3</td></tr>";
    while($row=mysql_fetch_array($res)){
        if($pId!=$row[0]){
            if($pId!=''){
                echo "</tr>";
            }
            $pId=$row[0];
            echo "<tr><td>$pId</td>";
        }else{
            $price=$row[1];
            echo "<td>$price</td>";
        }
    }
    echo "</tr></table>";
}

我假设您有相同的供应商,并且没有丢失供应商数据,但我仍然会进行一些检查,看看我是否没有错过任何供应商价格数据。你可以轻松做到这一点。
祝你好运!

$sql="SELECT product_id, product_price FROM pricing ORDER BY product_id, vendor_id";
$res=mysql_query($sql,$link);
if(mysql_num_rows($res)>0){
    $pId='';
    echo "<table><tr><td>Product</td><td>Vendor 1</td><td>Vendor 2</td><td>Vendor 3</td></tr>";
    while($row=mysql_fetch_array($res)){
        if($pId!=$row[0]){
            if($pId!=''){
                echo "</tr>";
            }
            $pId=$row[0];
            echo "<tr><td>$pId</td>";
        }else{
            $price=$row[1];
            echo "<td>$price</td>";
        }
    }
    echo "</tr></table>";
}

I have assumed in this that you have same vendors and no vendor data is missing, but I will still put some check to see if I haven't missed any vendor price data. you can easily do that.
Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文