首先,我很高兴我偶然发现了这个网站,你的问题和答案帮助了我早期的作业:)现在我需要帮助:(我试图用我的代码做的是创建一个表格,它将有3列“产品”“描述”“价格”。在以下每个标题下,我的数组都会为“{ {1}}“位于”产品“列下等等。我的问题是我在使用$productImage
函数时似乎无法弄清楚如何制作表格
任何帮助将不胜感激!
foreach()
答案 0 :(得分: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>
希望这有帮助。
答案 1 :(得分: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>
答案 2 :(得分: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>
答案 3 :(得分: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>";
答案 4 :(得分: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>";