0

首先,我真的很高兴我偶然发现了这个网站,您的问题和回答帮助我完成了早期的作业 :) 现在我需要帮助 :( 我试图用我的代码做的是创建一个它应该有的表3 列“产品”“描述”“价格”。在以下每个标题下,我的数组“ $productImage”位于“产品”列下,依此类推。我的问题是我似乎无法弄清楚如何在使用该foreach()功能时制作表格。

任何帮助将不胜感激!

<!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>';
   }

?>

4

5 回答 5

2

你只需要一个 foreach 循环来做这样的事情,但你需要把它们都放在同一个数组中:

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

希望这可以帮助。

于 2013-10-04T05:25:14.610 回答
0

尝试这个...

<!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>
于 2013-10-04T05:32:33.997 回答
0
<?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>
于 2013-10-04T05:32:41.657 回答
0

试试这个代码,这将对你有所帮助。

echo "<table>";
for($index=0; $index<count($productImage); $index++)
{
    echo"<tr>";
    echo "<td><img src='{$productimage[$index]}' width='200' height='180'></td>";
    echo "<td>{$description[$index]}</td>";
    echo "<td>{$price[$index]}</td>";
    echo "</td>";
}
echo "</table>";

但对我来说,这不是一个好的解决方案,你应该使用这样的数组数组

$array = array(
    array(
        'image' => 'http://www.rudebaguette.com/assets/PlayStation4 FeaturedImage.jpg',
        'desc'  => 'PS4',
        'price' => '400'
    ),
    array(
        ...
    )
);

然后使用这个循环

echo "<table>";
foreach($array in $item)
{
    echo"<tr>";
    echo "<td><img src='{$item['image']}' width='200' height='180'></td>";
    echo "<td>{$item['desc']}</td>";
    echo "<td>{$item['price']}</td>";
    echo "</td>";
}
echo "</table>";
于 2013-10-04T05:35:56.937 回答
0

这是我喜欢使用的东西。它更通用一点。

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

     $first_row = TRUE;

     echo '<table>';

     foreach ($product_table as $product_row) {
         echo "<tr>";

         foreach ($product_row as $column_name => $column_value) {
             if ($first_row) {
                 printf("<th>%s</th>", $column_name);
                 $first_row = FALSE;
             } else {
                 printf("<td>%s</td>", $column_value);
             }
         }

         echo "</tr>";
     }

     echo "</table>";
于 2013-10-04T05:50:36.240 回答