...

View Full Version : Need help printing an array into an HTML table



hateyourtv
09-17-2005, 03:38 PM
I'm tyring to use a for loop to print an array into a two-column HTML table.
I can only use one for loop (no while loops) and one array.
I can't figure out a way to print it to the table correctly. It's supposed to have movie titles in the left column and movie times on the right.

<?php
$movies = array ("Movie Titles","Movie Times", "Charlie and the Chocolate Factory","1:30 | 4:00 | 7:00 | 10:00", "Cave","2:30 | 4:45 | 7:30 | 9:15", "Four Brothers","2:15 | 4:50 | 7:20 | 9:50", "Red Eye","2:45 | 5:00 | 7:05 | 9:30", "The Skeleton Key","2:30 | 5:00 | 7:25 | 9:55", "Junebug","2:30 | 5:05 | 7:45 | 10:20", "March of the Penguins","12:30 | 2:55 | 5:10 | 7:15", "Must Love Dogs","12:55 | 3:50 | 6:45 | 9:15", "Transporter 2","5:45 | 7:10 | 8:15 | 9:35", "Wedding Crashers","1:20 | 4:30 | 7:30 | 10:30");



echo "<table width=\"500\" border=\"1\" cellpadding=\"10\">";

for ($i=0; $i < count($movies); $i++)
{
echo "<tr>";
echo "<td>$movies[$i]</td>";
echo "<td>$movies[$i]</td>";
echo "</tr>";
}


echo "</table>";

?>

I have even tried using foreach() and making the movie titles the index, but then i can't figure out how to change the for loop to make it print correctly.
Help!

e-Raser
09-17-2005, 03:46 PM
I think if you make two arrays it'd be easier.

<?php
$moviestitles = array ("Movie Titles","Charlie and the Chocolate Factory","Four Brothers","Red Eye","The Skeleton Key","Junebug","March of the Penguins","Must Love Dogs","Transporter 2","Wedding Crashers");

$movietimes = array ("Movie Times","1:30 | 4:00 | 7:00 | 10:00","2:30 | 4:45 | 7:30 | 9:15","2:15 | 4:50 | 7:20 | 9:50","2:45 | 5:00 | 7:05 | 9:30","2:30 | 5:00 | 7:25 | 9:55","2:30 | 5:05 | 7:45 | 10:20","12:30 | 2:55 | 5:10 | 7:15","12:55 | 3:50 | 6:45 | 9:15","5:45 | 7:10 | 8:15 | 9:35","1:20 | 4:30 | 7:30 | 10:30");

echo "<table width=\"500\" border=\"1\" cellpadding=\"10\">";

for ($i=0; $i < count($movietitles); $i++)
{
echo "<tr>";
echo "<td>$movietitles[$i]</td>";
echo "<td>$movietimes[$i]</td>";
echo "</tr>";
}


echo "</table>";

?>

hateyourtv
09-17-2005, 03:55 PM
It /would/ be easier with two arrays, but this is for a school assignment, and my teacher specifically told me we could only use one :(
I'm obviously just starting to learn PHP so this has me totally stuck.

e-Raser
09-17-2005, 04:19 PM
ah, ok.
You could do this.

$movies = array(
array("Title", "time"),
array("Title", "time"),
);

echo "<table width=\"500\" border=\"1\" cellpadding=\"10\">";
foreach($movies as $movie)
{
echo "<tr>";
echo "<td>$movies[0]</td>";
echo "<td>$movies[1]</td>";
echo "</tr>";
}
echo "</table>";

I asked a friend so this wasn't mine ^^

hateyourtv
09-17-2005, 04:49 PM
When I do that..the output it's giving me says "array" instead of the titles and times....

hateyourtv
09-17-2005, 04:51 PM
Ah, nevermind...changed
<td>$movies[0]</td> to $movie[0]

Thanks!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum