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.

Link to comment
Share on other sites

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. Edited by jcbones
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.