显示多维数组数组结果在表中,数据库中包含1列

时间:2022-10-10 21:17:57

I need help to display the result of my array in a table while filling the Description column with data from Mysql table base on product_ID on each line. Description now show the same Product description for each product_ID. it is not callind the description base on product_ID on the line.

我需要帮助才能在表格中显示我的数组结果,同时使用每行上product_ID的Mysql表中的数据填充Description列。描述现在显示每个product_ID的相同产品描述。它不是callind基于product_ID的描述。

<?php

foreach($_SESSION["cart_array"]as $item){
$i++;
$item_id=$item['part_id'];
$sql = mysqli_query($con,"SELECT * FROM   product_description        
     WHERE product_id='$item_id' LIMIT 1");
While($row=mysqli_fetch_array($sql)){
$product_name=$row["name"];      
}   
}
  ?>

 <table style="margin-left:50px; text-align:left;">
    <tr>
      <th style="width:20%; border-bottom:solid 1px #000;">Item ID</th>
      <th style="width:40%; border-bottom:solid 1px #000">Description</th>
      <th style="width:15%; border-bottom:solid 1px #000">Quantity</th>
      <th style="width:20%; border-bottom:solid 1px #000">Unit Price</th>
    </tr>
<?php foreach($_SESSION["cart_array"]as $item): ?>
    <tr>
          <td><?php echo $item['part_id'] ?></td>
          <td><?php echo $product_name ?></td>
          <td><?php echo $item['quantity'] ?></td>
          <td><?php echo $item['price'] ?></td>
    </tr>
<?php endforeach; ?>
</table>

2 个解决方案

#1


0  

You probably want this:

你可能想要这个:

While($row=mysqli_fetch_array($sql)){
    $product_name[$row['part_id']] = $row["name"];      
} 


<td><?php echo $product_name[$item['part_id']] ?></td>

so that you're building an ARRAY of product names. As is, your loop fetches all of the names, but trashes the previously retrieved ones.

这样您就可以构建产品名称的ARRAY。因此,您的循环将获取所有名称,但会删除先前检索的名称。

#2


1  

The problem is that your variable $product_name contains the name of the product from your last item.

问题是您的变量$ product_name包含上一个项目的产品名称。

if you format your code a bit better it might become clear that setting the name happens every loop.

如果您将代码格式化得更好,可能会清楚的是,每次循环都会设置名称。

foreach($_SESSION["cart_array"]as $item){
    $i++;
    $item_id=$item['part_id'];

    $sql = mysqli_query($con,"SELECT * FROM   product_description WHERE product_id='$item_id' LIMIT 1");

    While($row=mysqli_fetch_array($sql)){
        $product_name=$row["name"];      
    }   
} 

as your see each for loop you will set your $product_name and then continue by echoing it.

当您看到每个for循环时,您将设置$ product_name,然后通过回显它继续。

There are a few ways to solve this.

有几种方法可以解决这个问题。

1) if you want to keep doing 2 loops i would make $product_name an array and assign the name like $product_names[$item_id] = $row["name"];
and then echo like

1)如果你想继续做2个循环,我会让$ product_name成为一个数组并指定名称如$ product_names [$ item_id] = $ row [“name”];然后回声像

2) Do the query in the the second loop so like

2)在第二个循环中执行查询

<table style="margin-left:50px; text-align:left;">
<tr>
  <th style="width:20%; border-bottom:solid 1px #000;">Item ID</th>
  <th style="width:40%; border-bottom:solid 1px #000">Description</th>
  <th style="width:15%; border-bottom:solid 1px #000">Quantity</th>
  <th style="width:20%; border-bottom:solid 1px #000">Unit Price</th>
</tr>  
<?php 
    foreach($_SESSION["cart_array"]as $item): 
        $item_id=$item['part_id'];
        $sql = mysqli_query($con,"SELECT * FROM   product_description        
        WHERE product_id='$item_id' LIMIT 1");

        While($row=mysqli_fetch_array($sql)){
            $product_name=$row["name"];      
        }   
?>
<tr>
      <td><?php echo $item['part_id'] ?></td>
      <td><?php echo $product_name ?></td>
      <td><?php echo $item['quantity'] ?></td>
      <td><?php echo $item['price'] ?></td>
</tr>
<?php endforeach; ?>
</table>

Btw the $i++ does not seem to do anything so i would remove it regardless of what solution you chose

顺便说一下,$ i ++似乎没有做任何事情,所以无论你选择什么解决方案我都会删除它

#1


0  

You probably want this:

你可能想要这个:

While($row=mysqli_fetch_array($sql)){
    $product_name[$row['part_id']] = $row["name"];      
} 


<td><?php echo $product_name[$item['part_id']] ?></td>

so that you're building an ARRAY of product names. As is, your loop fetches all of the names, but trashes the previously retrieved ones.

这样您就可以构建产品名称的ARRAY。因此,您的循环将获取所有名称,但会删除先前检索的名称。

#2


1  

The problem is that your variable $product_name contains the name of the product from your last item.

问题是您的变量$ product_name包含上一个项目的产品名称。

if you format your code a bit better it might become clear that setting the name happens every loop.

如果您将代码格式化得更好,可能会清楚的是,每次循环都会设置名称。

foreach($_SESSION["cart_array"]as $item){
    $i++;
    $item_id=$item['part_id'];

    $sql = mysqli_query($con,"SELECT * FROM   product_description WHERE product_id='$item_id' LIMIT 1");

    While($row=mysqli_fetch_array($sql)){
        $product_name=$row["name"];      
    }   
} 

as your see each for loop you will set your $product_name and then continue by echoing it.

当您看到每个for循环时,您将设置$ product_name,然后通过回显它继续。

There are a few ways to solve this.

有几种方法可以解决这个问题。

1) if you want to keep doing 2 loops i would make $product_name an array and assign the name like $product_names[$item_id] = $row["name"];
and then echo like

1)如果你想继续做2个循环,我会让$ product_name成为一个数组并指定名称如$ product_names [$ item_id] = $ row [“name”];然后回声像

2) Do the query in the the second loop so like

2)在第二个循环中执行查询

<table style="margin-left:50px; text-align:left;">
<tr>
  <th style="width:20%; border-bottom:solid 1px #000;">Item ID</th>
  <th style="width:40%; border-bottom:solid 1px #000">Description</th>
  <th style="width:15%; border-bottom:solid 1px #000">Quantity</th>
  <th style="width:20%; border-bottom:solid 1px #000">Unit Price</th>
</tr>  
<?php 
    foreach($_SESSION["cart_array"]as $item): 
        $item_id=$item['part_id'];
        $sql = mysqli_query($con,"SELECT * FROM   product_description        
        WHERE product_id='$item_id' LIMIT 1");

        While($row=mysqli_fetch_array($sql)){
            $product_name=$row["name"];      
        }   
?>
<tr>
      <td><?php echo $item['part_id'] ?></td>
      <td><?php echo $product_name ?></td>
      <td><?php echo $item['quantity'] ?></td>
      <td><?php echo $item['price'] ?></td>
</tr>
<?php endforeach; ?>
</table>

Btw the $i++ does not seem to do anything so i would remove it regardless of what solution you chose

顺便说一下,$ i ++似乎没有做任何事情,所以无论你选择什么解决方案我都会删除它