2

I am trying to extract data from an API based on information from two different tables, before cycling through a foreach and parsing this data into the query. I have constructed a series of queries which get me the exact data I'm looking for (name and poster of the first film in an array), however when parsing this data into the foreach which returns a list of a series of films, the variables I have predefined are only being brought back for the final list in the array, rather than cycling through them all, as expected.

Here's what I've got so far:

Processing:

// Get a user's Watchlists from the watchlists table
    $query = "SELECT * FROM watchlists WHERE user_id = " . $profile_info['id']; 
    $watchlist_result = mysql_query($query);
    $watchlists = array();
    while(($row = mysql_fetch_assoc($watchlist_result))) {
        $watchlists[] = $row;
    }

    // Count how many Watchlists a user has
    $watchlist_count = count($watchlists);

    // Echo details of each Watchlist
    echo "<pre>";
    print_r($watchlists);
    echo "</pre>";

    // For each Watchlist, select all of the films it contains and echo them out
    foreach ($watchlists as $key => $films) {
        // Get each Watchlist's ID from the watchlists table
        $watchlist_id = $films['watchlist_id'];
        echo "<pre>";
        print_r($watchlist_id);
        echo "</pre>";

        // Extract films from the watchlist_films table for each Watchlist, based on its unique ID and put them into an array
        $watchlist_film_query = "SELECT * FROM watchlist_films WHERE watchlist_id = " . $watchlist_id;
        $watchlist_film_result = mysql_query($watchlist_film_query);
        $watchlist_films = array();
        while(($row = mysql_fetch_assoc($watchlist_film_result))) {
            $watchlist_films[] = $row;
        }

        // Echo the new array
        echo "<pre>";
        print_r($watchlist_films);
        echo "</pre>";

        // Get the ID of the first film in each Watchlist
        $rt_id = $watchlist_films[0]['film_id'];

        // Echo the ID
        echo "<pre>";
        print_r($rt_id);
        echo "</pre>";

        // Initialise Rotten Tomates API
        include_once('/api/RottenTomatoes.php');

        /* Rotten Tomatoes */
        $rottenTomatoes = new RottenTomatoes('2b2cqfxyazbbmj55bq4uhebs', 10, 'uk');

        // Search Rotten Tomatoes for details on the first film in the array
        $rottenTomatoes->movieSearch($rt_id);

        //echo "<pre>";
        try {
            $result = $rottenTomatoes->getMovieInfo($rt_id);
            //print_r($result);
        } catch (Exception $e) {
            //print_r($e);
        }
        //echo "</pre>";

        // Get poster and name of first movie in array
        $thumbnail = $result['posters']['thumbnail'];
        $first_film_name = $result['title'];

        echo "<pre>";
        print_r($thumbnail);
        echo "</pre>";

        echo "<pre>";
        print_r($first_film_name);
        echo "</pre>";
    }

Now, I want to parse this information into an actual page and print out all the information for each Watchlist a user has.

Output:

if ($watchlist_count != 0) {?>
                        <ul class="unstyled"><?php
                            foreach($watchlists as $key => $watchlist_item) {?>
                                <li class="well list-item clearfix"><?php

                                    echo "<pre>";
                                    print_r($watchlist_item);
                                    echo "</pre>";

                                    echo "<pre>";
                                    print_r($watchlist_films);
                                    echo "</pre>";

                                    echo "<pre>";
                                    print_r($watchlist_id);
                                    echo "</pre>";

                                    echo "<pre>";
                                    print_r($thumbnail);
                                    echo "</pre>";

                                    echo "<pre>";
                                    print_r($first_film_name);
                                    echo "</pre>";?>

                                    <div class="row-fluid">
                                        <a href="watchlist.php?id=<?php echo $watchlist_item['watchlist_id']; ?>" title="<?php echo $watchlist_item['name']; ?> Watchlist">
                                            <img src="<?php echo $thumbnail; ?>" class="span1 pull-left" alt="<?php echo $first_film_name; ?> poster" title="<?php echo $first_film_name; ?> poster" />
                                        </a>

                                        <div class="span11 movie-info">
                                            <p class="search-title"><a href="watchlist.php?id=<?php echo $watchlist_item['watchlist_id']; ?>" title="<?php echo $watchlist_item['name']; ?> Watchlist"><?php echo $watchlist_item['name']; ?></a></p>

                                            <p class="search-synopsis"><?php echo $watchlist_item['description']; ?></p>
                                        </div>
                                    </div>

                                </li><?php
                            }?>
                        </ul><?php
                    } else {?>
                        <div class="well list-item">
                            You haven't created any Watchlists yet! Why not visit a movie page now and build your first?
                        </div><?php
                    }?>

The issue I'm having is that, while the processing section gets me exactly what I want (i.e. the name and thumbnail of the first film in each Watchlist), when I parse this information into the output, only the name and poster for the first film in the last Watchlist is shown, as below:

Result

MrLore
  • 3,759
  • 2
  • 28
  • 36
Alex Ryans
  • 1,865
  • 5
  • 23
  • 31
  • it looks like 'watchlist_item' is used as both an array and a value. – ethrbunny Mar 25 '13 at 19:31
  • Are you sure that `$profile_info['id']` can only contain numbers? Also note that `mysql_*` functions are deprecated (see the [red box](http://php.net/mysql_query)). – Marcel Korpel Mar 25 '13 at 19:37
  • @MarcelKorpel Yes, profiles can only be identified by their profile ID as a unique identifier, which will always be a number, as will Watchlists - each Watchlist and each user has a unique ID, and the two can be associated with each other, i.e. each unique user can have many Watchlists, each with its own, unique ID. – Alex Ryans Mar 25 '13 at 19:39

1 Answers1

1

In you processing you overwrite $watchlist_films, $thumbnail and $first_film_name in each loop of the foreach, so you only get the data for the last run.

You should add the data back to your main array for example at the end of you foreach loop like:

$films['watchlist_films']=$watchlist_films;
$films['thumbnail']=$thumbnail;
$films['first_film_name']=$first_film_name;

In the Output you can then access the data like for example:

$watchlist_item['thumbnail'];
$watchlist_item['first_film_name'];

So you Processing could look like this (shortened):

$query = "SELECT * FROM watchlists WHERE user_id = " . $profile_info['id']; 
$watchlist_result = mysql_query($query);
$watchlists = array();
while(($row = mysql_fetch_assoc($watchlist_result))) {
    $watchlists[] = $row;
}

// For each Watchlist, select all of the films it contains and echo them out
foreach ($watchlists as $key => $films) {
    $watchlist_film_query = "SELECT * FROM watchlist_films WHERE watchlist_id = " . $films['watchlist_id'];
    $watchlist_film_result = mysql_query($watchlist_film_query);
    $watchlist_films = array();

    //You don't need to load all data if you only want the first one:)
    $row = mysql_fetch_assoc($watchlist_film_result)
    $rt_id = $row['film_id'];

    // Initialise Rotten Tomates API
    include_once('/api/RottenTomatoes.php');

    /////Rotten Tomato Call /////
    //===> Rotten Tomatoe API call code goes here <=== //

    //You don't really need the whole films list
    //$films['watchlist_films']=$watchlist_films;
    $films['thumbnail']=$result['posters']['thumbnail'];
    $films['first_film_name']=$result['title'];
}

And Output:

if (count($watchlists)> 0) {?>
                    <ul class="unstyled"><?php
                        foreach($watchlists as $key => $watchlist_item) {?>
                            <li class="well list-item clearfix"><?php

                                <div class="row-fluid">
                                    <a href="watchlist.php?id=<?php echo $watchlist_item['watchlist_id']; ?>" title="<?php echo $watchlist_item['name']; ?> Watchlist">
                                        <img src="<?php echo $watchlist_item['thumbnail']; ?>" class="span1 pull-left" alt="<?php echo $first_film_name; ?> poster" title="<?php echo $watchlist_item['first_film_name']; ?> poster" />
                                    </a>

                                    <div class="span11 movie-info">
                                        <p class="search-title"><a href="watchlist.php?id=<?php echo $watchlist_item['watchlist_id']; ?>" title="<?php echo $watchlist_item['name']; ?> Watchlist"><?php echo $watchlist_item['name']; ?></a></p>

                                        <p class="search-synopsis"><?php echo $watchlist_item['description']; ?></p>
                                    </div>
                                </div>

                            </li><?php
                        }?>
                    </ul><?php
                } else {?>
                    <div class="well list-item">
                        You haven't created any Watchlists yet! Why not visit a movie page now and build your first?
                    </div><?php
                }?>

All not tested;-)

Neisklar
  • 29
  • 3
  • Thanks, I see what you mean. Where would the best place be to echo out these variables to get the information I need for each film. Could you please provide an example, by any chance? – Alex Ryans Mar 25 '13 at 19:36
  • Edited some more details in, mainly you need to store the data till all your processing is finished (best in your "main"-array, at the corresponding item). If you then, after the foreach do a print_r you will see what i mean – Neisklar Mar 25 '13 at 19:43
  • Excellent, thank you very much for your help. I'm just trying to implement your suggestions as above, however it seems the code fails between getting $rt_id and $films['watchlist_films']=$watchlist_films. Echoing out $rt_id gets the film IDs for the first film in each Watchlist, as expected, but echoing out $films['watchlist_films'] just gets me an empty array? – Alex Ryans Mar 25 '13 at 20:24
  • I shortened the code with parts not needed for it to function and i removed the Rotten Tomatoe call (indicated by the `/////Rotten Tomato Call /////` line) just to save space in the answer. So you need to put the call in, like in you initial code. – Neisklar Mar 25 '13 at 23:06