首先,我很高兴我偶然发现了这个网站,您的问题和答案帮助我完成了早期的任务:)现在我需要帮助:(我试图用代码做的是创建一个表,其中包含"Product“"Description”"Price“。在以下每一个标题下,我的数组"$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>';
}
?>发布于 2013-10-04 05:32:33
试试这个..。
<!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-04 05:25:14
您只需要一个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-04 05:32:41
<?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>https://stackoverflow.com/questions/19173804
复制相似问题