Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help printing an array into an HTML table

    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!

  • #2
    New Coder
    Join Date
    Jul 2005
    Location
    Perth, Australia. Age: 13
    Posts
    81
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think if you make two arrays it'd be easier.
    PHP Code:
    <?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>";

    ?>
    I lost my signature, have you seen it?
    I have the power to erase you...

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Jul 2005
    Location
    Perth, Australia. Age: 13
    Posts
    81
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ah, ok.
    You could do this.
    PHP Code:
    $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 ^^
    Last edited by e-Raser; 09-17-2005 at 04:21 PM.
    I lost my signature, have you seen it?
    I have the power to erase you...

  • #5
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When I do that..the output it's giving me says "array" instead of the titles and times....

  • #6
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, nevermind...changed
    <td>$movies[0]</td> to $movie[0]

    Thanks!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •