Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP将foreach()放入表中_Php_Html - Fatal编程技术网

PHP将foreach()放入表中

PHP将foreach()放入表中,php,html,Php,Html,首先,我真的很高兴我偶然发现了这个网站,你的问题和答案帮助我完成了早期的作业:)现在我需要帮助:(我试图用代码创建一个表,其中包含3列“产品”“说明”“价格””。在以下每个标题下,我的数组“$productImage”将位于“Product”列下,以此类推。我的问题是,我似乎不知道如何在使用foreach()函数时生成表 任何帮助都将不胜感激 <!DOCTYPE html> <html> <head> <meta http-equ

首先,我真的很高兴我偶然发现了这个网站,你的问题和答案帮助我完成了早期的作业:)现在我需要帮助:(我试图用代码创建一个表,其中包含3列“产品”说明”价格””。在以下每个标题下,我的数组“
$productImage
”将位于“Product”列下,以此类推。我的问题是,我似乎不知道如何在使用
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>';
   }

?>


您只需要一个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>

: />
希望这有帮助。

试试这个

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


形象
描述
价格
" />

试试这段代码这会对你有所帮助

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>";
然后使用这个循环

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>";
echo”“;
foreach($item中的数组)
{
回声“;
回声“;
回显“{$item['desc']}”;
回显“{$item['price']}”;
回声“;
}
回声“;

这里有一些我喜欢使用的东西。它有点通用

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

我会尝试一下!非常感谢你的快速回复:)我会把结果发回来。再次感谢你!就我所寻找的,我对PHP和整个编码领域都是新手。我记得我的教授提到数组,他说它会让事情更有效。真是太感谢你们了!!!
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>";
     <?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>";