Jump to content

PHP: Insert an associated array variable into a table


mstevens

Recommended Posts

Good evening,

 

I am working on a program that takes images, and casts them into an associated array, I then want to display thumbnails of those images into a 3 column by 4 row table...

 

[image][image][image]

[image][image][image]

[image][image][image]

[image][image][image]

 

(For my visual friends)

 

here is my current, pardon for the mess, but right now, functionality is more important.

<!DOCTYPE html>
<html>
<head>
	<title>Zodiac Gallery</title>
</head>
<body>
	
<h2 style="text-align:center">Zodiac Gallery</h2>
<p>Click a thumbnail image to see enlarged view.</p>
<?php

$ZodiacArray = array (
	"Images/rat.jpg" => "Rat",
	"Images/ox.jpg" => "Ox",
	"Images/tiger.jpg" => "Tiger",
	"Images/rabbit.jpg" => "Rabbit",
	"Images/dragon.jpg" => "Dragon",
	"Images/snake.jpg" => "Snake",
	"Images/horse.jpg" => "Horse",
	"Images/goat.jpg" => "Goat",
	"Images/monkey.jpg" => "Monkey",
	"Images/rooster.jpg" => "Rooster",
	"Images/dog.jpg" => "Dog",
	"Images/pig.jpg" => "Pig");

foreach ($ZodiacArray as $image => $zodiac)
	{
		echo "<table>
			<tr>
				<td><img src="$image"  alt="$zodiac" style="width:68px;height:65px"></td
				<td><img src="$image" alt="$zodiac" style="width:68px;height:65px"></td>
				<td><img src="$image" alt="$zodiac" style="width:68px;height:65px"></td>
			</tr>
			<tr>
				<td><img src="$image" alt="$zodiac" style="width:68px;height:65px"></td>
				<td><img src="$image" alt="$zodiac" style="width:68px;height:65px"></td>
				<td><img src="$image" alt="$zodiac" style="width:68px;height:65px"></td>
			</tr>
			<tr>
				<td><img src="$image" alt="$zodiac" style="width:68px;height:65px"></td>
				<td><img src="$image" alt="$zodiac" style="width:68px;height:65px"></td>
				<td><img src="$image" alt="$zodiac" style="width:68px;height:65px"></td>
			</tr>
			<tr>
				<td><img src="$image" alt="$zodiac" style="width:68px;height:65px"></td>
				<td><img src="$image" alt="$zodiac" style="width:68px;height:65px"></td>
				<td><img src="$image" alt="$zodiac" style="width:68px;height:65px"></td>
			</tr>
		</table>";

?>

Help is welcome, thank you.

So basically,

echo "<table>";
$i = 0;
foreach ($ZodiacArray as $image => $zodiac)
	{
	  echo ($i % 4 == 0) ? <tr> : null;
          echo '<td><img src="$image"  alt="$zodiac" style="width:68px;height:65px"></td>';
          echo ($i++ % 4 == 0) ? </tr> : null;		
}
echo '</table>';
I think that is error free, but as always, I could be wrong.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.