简体   繁体   English

PHP将foreach()放在表中

[英]PHP putting foreach() inside a table

First off, I'm really glad I stumbled on this website, your questions and answers helped me with my early assignments :) Now I need help :( What I was trying to do with my code was to create a table where it would have 3 columns " Product " " Description " " Price ". Under each of the following headers, I would have my arrays " $productImage " be under the " Product " column and so forth. My problem is that I just can't seem to figure out how to make a table while using the foreach() function. 首先,我很高兴我偶然发现了这个网站,您的问题和答案对我的早期作业有所帮助:)现在,我需要帮助:(我试图用我的代码做的事情是在一个表中创建一个表3列“ 产品 ”,“ 描述 ”,“ 价格 ”。在以下每个标题下,我会将数组“ $productImage ”放在“ 产品 ”列下,依此类推。我的问题是我似乎无法弄清楚在使用foreach()函数时如何制作表。

Any help will be greatly appreciated! 任何帮助将不胜感激!

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

?>

You would only need one foreach loop to do something like that, but you need to put them all in the same array: 您只需要一个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>

Hope this helps. 希望这可以帮助。

Try this... 尝试这个...

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

try this code this will help you. 试试这个代码,这将为您提供帮助。

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>";

but accoding to me this is not a good solution you should use array of arrays like this 但是对我来说这不是一个好的解决方案,您应该使用像这样的数组数组

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

and then use this loop 然后使用这个循环

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>";

here's something that I like to use. 这是我喜欢使用的东西。 it's a bit more generic. 比较通用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM