首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP将foreach()放入表中

PHP将foreach()放入表中
EN

Stack Overflow用户
提问于 2013-10-04 05:19:51
回答 5查看 841关注 0票数 0

首先,我很高兴我偶然发现了这个网站,您的问题和答案帮助我完成了早期的任务:)现在我需要帮助:(我试图用代码做的是创建一个表,其中包含"Product“"Description”"Price“。在以下每一个标题下,我的数组"$productImage“都位于"Product”列下,依此类推。我的问题是,在使用foreach()函数时,我似乎无法弄清楚如何创建一个表。

任何帮助都将不胜感激!

代码语言:javascript
复制
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
 <body>

<?php

   $productImage = array('http://www.rudebaguette.com/assets/PlayStation4 FeaturedImage.jpg', 'http://cdn0.mos.techradar.futurecdn.net//art/games_consoles/Xbox%20One/Press%20shots/Xbox%20One%20family-580-90.jpg', 'http://www.blogcdn.com/de.engadget.com/media/2009/08/razer-naga-mmo-mouse-all-set-to-create-a-new-world-record11.jpg', 'http://cdn1.mos.techradar.futurecdn.net//art/gadgets/Google%20Glass/google_glass_grey-580-90.jpg', 'http://img1.targetimg1.com/wcsstore/TargetSAS//img/p/10/02/10029875.jpg');
   $description = array('PS4 ', 'Xbox One', 'Razer Naga', 'Google Glass', 'Magic Bullet');  
   $price = array('400', '350', '70', '300', '50');
   echo '<table>';
   foreach ($productImage as $pic)
   {
       echo '<tr>';
       echo '<td>';
       echo "<img src='".$pic."' width='200' height='180'>";
       echo '</td>';
       echo '</tr>';    
   }

   foreach ($description as $des)
   {
       echo '<tr>';
       echo '<td>';
       echo $des;
       echo '</td>';
       echo '</tr>'; 
   }

   foreach ($price as $m)
   {
       echo '<tr>';
       echo '<td>';
       echo $m;
       echo '</td>';
       echo '</tr>';
   }

?>
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-10-04 05:32:33

试试这个..。

代码语言:javascript
复制
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
  </head>
  <body>
    <?php
      $productImage = array('http://www.rudebaguette.com/assets/PlayStation4 FeaturedImage.jpg', 'http://cdn0.mos.techradar.futurecdn.net//art/games_consoles/Xbox%20One/Press%20shots/Xbox%20One%20family-580-90.jpg', 'http://www.blogcdn.com/de.engadget.com/media/2009/08/razer-naga-mmo-mouse-all-set-to-create-a-new-world-record11.jpg', 'http://cdn1.mos.techradar.futurecdn.net//art/gadgets/Google%20Glass/google_glass_grey-580-90.jpg', 'http://img1.targetimg1.com/wcsstore/TargetSAS//img/p/10/02/10029875.jpg');
      $description = array('PS4 ', 'Xbox One', 'Razer Naga', 'Google Glass', 'Magic Bullet');  

      $price = array('400', '350', '70', '300', '50');

      echo '<table>';
    echo "<tr><td>Product</td><td>description</td><td>Price</td></tr>";
    foreach ($productImage as $key=>$pic)
    {
        echo "<tr>";
            echo '<td>';
            echo "<img src='".$productImage[$key]."' width='200' height='180'>";
            echo '</td>';
            echo '<td>';
            echo $description[$key];
            echo '</td>';
            echo '<td>';
            echo $price[$key];
            echo '</td>';
        echo '</tr>'; 
    }
    ?>
  </body>
</html>
票数 0
EN

Stack Overflow用户

发布于 2013-10-04 05:25:14

您只需要一个foreach循环就可以这样做,但是您需要将它们放在同一个数组中:

代码语言:javascript
复制
<?php
// You can have an array of arrays, like such.
// This is called a multidimensional array
$array = array(
    array(
        'image' => '...',
        'desc'  => '...',
        'price' => '...'
    ),
    array(
        ...
    )
);
?>

<table>
    <?php foreach($array as $item): ?>
    <tr>
        <td><img src="<?php echo $item['image']; ?>: /></td>
        <td><?php echo $item['desc']; ?></td>
        <td><?php echo $item['price']; ?></td>
    </tr>
    <?php endforeach; ?>
</table>

希望这能有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2013-10-04 05:32:41

代码语言:javascript
复制
<?php
$productImage = array('http://www.rudebaguette.com/assets/PlayStation4 FeaturedImage.jpg', 'http://cdn0.mos.techradar.futurecdn.net//art/games_consoles/Xbox%20One/Press%20shots/Xbox%20One%20family-580-90.jpg', 'http://www.blogcdn.com/de.engadget.com/media/2009/08/razer-naga-mmo-mouse-all-set-to-create-a-new-world-record11.jpg', 'http://cdn1.mos.techradar.futurecdn.net//art/gadgets/Google%20Glass/google_glass_grey-580-90.jpg', 'http://img1.targetimg1.com/wcsstore/TargetSAS//img/p/10/02/10029875.jpg');

    $description = array('PS4 ', 'Xbox One', 'Razer Naga', 'Google Glass', 'Magic Bullet');  

   $price = array('400', '350', '70', '300', '50');

  ?>

   <table>
       <tr>
         <th>Image</th>
         <th>Description</th>
         <th>Price</th>
       </tr>
       <?php foreach ($productImage as $key => $value)
          { ?>
            <tr>
              <td><img src="<?php echo $value; ?>" /></td>
              <td><?php  echo $description[$key]; ?></td>
              <td><?php  echo $price[$key]; ?></td>
            </tr>  
          <?php }
       ?>
   </table>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19173804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档