Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to output the array $Movies as an HTML table, here's the function;

function print_movies($table= 'Movies'){
    $db= $GLOBALS['db'];

    $Movies = get_info('Movies');

    print_r($Movies);

    echo "<table border='1'>
            <tr>
                <th>Movie Title</th>
                <th>Year Released</th>
                <th>Was it a book?</th>
                <th>Genre</th>
            </tr>"

    for($i=0;$i<count($Movies);$i++)){
        ?>
        <tr><td><?php echo $Movies[$title]; ?></td>
        <tr><td><?php echo $Movies[$year_released]; ?></td>
        <tr><td><?php echo $Movies[$is_book]; ?></td>
        <tr><td><?php echo $Movies[$genre]; ?></td>
        <?php
    }

    echo "</table>";
    ?>
}

And here's my HTML;

<?php
    print_movies($Movies);
?>
share|improve this question
7  
<?php echo $Movies[$i][$title]; ?> –  MonkeyZeus Jan 23 at 22:06
    
You're also missing closing semicolon after the header row </tr>"; –  Set Sail Media Jan 23 at 22:06
    
You forgot to tell us what the problem is. –  jeroen Jan 23 at 22:06
3  
I am 99% sure I was asked to do this exact task when I went to college –  MonkeyZeus Jan 23 at 22:07
3  
you should look into foreach, it will change your life. –  Arian Jan 23 at 22:09
show 4 more comments

2 Answers

Your PHP openning and closing tags were not located properly.

You also forgot to put a semicolon after your first echo.

<?
    function print_movies($table= 'Movies'){
    $db= $GLOBALS['db'];

    $Movies = get_info('Movies');

     print_r($Movies);

    echo "<table border='1'>

    <tr>
    <th>Movie Title</th>
    <th>Year Released</th>
    <th>Was it a book?</th>
    <th>Genre</th>
    </tr>";

     for($i=0; $i<count($Movies); $i++){ 
         ?>
    <tr><td><? echo $Movies[$title]; ?></td>
    <tr><td><? echo $Movies[$year_released]; ?></td>
    <tr><td><? echo $Movies[$is_book]; ?></td>
    <tr><td><? echo $Movies[$genre]; ?></td>
        <?
        }

    echo "</table>";


    }
    ?>
share|improve this answer
add comment

Not sure how the array is setup but this should work.

<tr>
    <?php
    foreach($Movies as $movie){
        echo "<td>".$movie[$title]."</td>";
        echo "<td>".$movie[$year_released]."</td>";
        //...
    }
    ?>
</tr>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.