0

I have a function that i pass a variable as reference into that an array gets stored into. Before, it also took a second reference variable to store an image url. When it used both of these everything worked fine and i did not get an undefined variable error. Now i have removed the image part and included it into the array that is stored in the single reference variable and it tells me that the variable i am passing is undefined.

Here is my function:

<?php
function retrieve_t($movie_title, &$movie_data) {
    $url = "http://www.imdbapi.com/?t=".urlencode($movie_title);
    $content = file_get_contents($url);
    if(substr($content, 2, 8) == "Response")
        return false;
    $data_array = explode("\",\"", substr($content,2,strlen($content)-4));
    $data_array2 = array();
    foreach ($data_array as $value) {
        $temp = explode("\":\"", $value);
        array_push($data_array2, $temp[0]);
        array_push($data_array2, $temp[1]);
    }
    $movie_data = array($data_array2[0] => $data_array2[1],
                            $data_array2[2] => $data_array2[3],
                            $data_array2[4] => $data_array2[5], 
                            $data_array2[8] => $data_array2[9], 
                            $data_array2[10] => $data_array2[11],
                            $data_array2[12] => $data_array2[13],
                            $data_array2[14] => $data_array2[15],
                            $data_array2[16] => $data_array2[17],
                            $data_array2[18] => $data_array2[19],
                            $data_array2[22] => $data_array2[23],
                            "img_url" => $data_array2[21]);
    return true;
}
 ?>

And here is where it is called:

<?php include "imdb_search.php"; ?>
<?php 
if(isset($_GET["add"]) && $_GET["title"])
    if($_GET["add"] == "yes") {
        include "db_movie_add.php"; 
        $title = $_GET["title"];
        if(retrieve_t($title, $movie_data))
            add_movie($movie_data);
    }
 ?>
 <html>
<head>
    <title>First Page</title>
</head>
<Body>
    <form>
        Title: <input value="<?php if (isset($_GET["title"])) echo $_GET["title"]; ?>" type="text" name="title" /><br />
        <input type="submit" value="Search" />
    </form>
    <?php
        if(isset($_GET["title"])) {
            $title = $_GET["title"];
            if(retrieve_t($title, $movie_data)) {
                echo "<img src=\"{$movie_data["img_url"]}\" width=\"200\"><br \>";
                echo "Title: {$movie_data["Title"]}<br \>";
                echo "<form><input type=\"hidden\" name=\"title\" value=\"{$title}\"><input type=\"hidden\" name=\"add\" value=\"yes\"><input type=\"submit\" value=\"Add\" /></form>";
            } else {
                echo "Sorry, movie was not found!";
            }
        }
    ?>
</Body>
</html>
Troy Cosentino
  • 4,658
  • 9
  • 37
  • 59

1 Answers1

0

You are using $movie_data before you define it.

Just set it to null first:

<?php include "imdb_search.php"; ?>
<?php 
if(isset($_GET["add"]) && $_GET["title"])
    if($_GET["add"] == "yes") {
        include "db_movie_add.php"; 
        $title = $_GET["title"];
        $movie_data = null;
        if(retrieve_t($title, $movie_data))...
Petah
  • 45,477
  • 28
  • 157
  • 213
  • `$movie_data` is read as a reference by the function. So there should be no error. Or is it an old PHP version? – Déjà vu Jul 11 '12 at 02:46
  • It doesn't matter if it is a reference in the function, you still ***must*** define it first, even though it is just going to be overwritten. In older versions, and when `error_reporting` is less strict it doesn't matter. – Petah Jul 11 '12 at 02:48
  • @ring0 thats exactly what i was thinking, especially since it worked when i had two reference variables and they weren't defined previously. I added setting it to null to begin with and still get the same error. "Notice: Undefined variable: movie_data in /Users/troycosentino/Sites/php_sandbox/firstpage.php on line 23" – Troy Cosentino Jul 11 '12 at 02:52
  • @TroyCosentino as in you example the place where `movie_data` is used is not line 23, can you please show us the full, accurate code for `firstpage.php` – Petah Jul 11 '12 at 03:02
  • Very sorry, I dont know why but when i was saving my file in my editor it was not actually saving it.. so everything i edited did not change anything. Once i was able to save it it all worked fine – Troy Cosentino Jul 11 '12 at 04:29
  • @TroyCosentino So if my answer helped to solve your problem, please click the tick :) – Petah Jul 11 '12 at 05:07
  • @Petah i actually did not need to have that, you don't need to define a variable before passing it by reference. I just wasn't saving properly and there was still 3 parameters in the file that was running, therefore causing the error. Thank you though! – Troy Cosentino Jul 11 '12 at 16:14