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

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.

<?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>

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.

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

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.

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

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

<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