0

I am literarily driving myself nuts with this problem.

I have this code in PHP:

 private function searchpage($data) {

  $file = @file_get_contents("./assets/template/search.html");
  $content = "";
  $pages = "";

 foreach ($data as $keys) {
 //var_dump($keys); This gives the correct information,I will show the    
 output later on.

        $content = str_replace("[PHOTO]",$keys['photo'], $file);
        $content = str_replace("[NAME]",$keys['name'], $content);
        $pages .= $content;
  }
 return $pages;
}

The var_dump from $keys looks like this

  array(1) { [0]=> array(7) { ["_id"]=> string(1) "2" ["_user"]=>    
  string(1) "1" ["_short"]=> string(4) "test" ["_long"]=> string(9)   
  "test long" ["name"]=> string(4) "test" ["_dat"]=> string(10) "2017-  
   08-29" ["photo"]=> string(92) "https://s-media-cache-     
   "}

as you can see, I use the correct name tags.

The STR_REPLACE doesn't work though. I keep getting this error:

Undefined index: name in     
/Applications/MAMP/htdocs/test/lib/_layout.php on line 144
Undefined index: photo in /Applications/MAMP/htdocs/test/lib/_layout.php on line 145

The strange thing is: I do EXACTLY the same thing on a different HTML page, and there it works perfectly.

I have no clue what's wrong with this code. I do request the function. The data I give to the function is correct. It does open the correct HTML page. The only thing which isn't correct is the str_replace.

Robbert
  • 31
  • 5
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – GrumpyCrouton Aug 31 '17 at 18:06
  • I've seen that one, but haven"t found the solution for my problem. – Robbert Aug 31 '17 at 18:08
  • Try replacing `$content = str_replace("[PHOTO]",$keys['photo'], $file); $content = str_replace("[NAME]",$keys['name'], $content);` with `$content = str_replace("[PHOTO]",$keys[0]['photo'], $file); $content = str_replace("[NAME]",$keys[0]['name'], $content);`. The `var_dump` shows that `$keys` holds an array of data. – Lal Aug 31 '17 at 18:09
  • `$keys[0]['name']` try this way – vadim_hr Aug 31 '17 at 18:11
  • Don't but answers in comments, please. If it is good enough to answer here, it is good enough to make an answer. –  Aug 31 '17 at 20:46

1 Answers1

1

Your var_dump says that $keys holds an array of data. Thus, in PHP, as you may know, if you are trying to access an array variable, you must specify the index also.

Thus, it should be $keys[0]['photo'] instead of $keys['photo']. Similarly, it should be $keys[0]['name'] instead of $keys['name'].

Please replace

    $content = str_replace("[PHOTO]",$keys['photo'], $file);
    $content = str_replace("[NAME]",$keys['name'], $content);

with

    $content = str_replace("[PHOTO]",$keys[0]['photo'], $file);
    $content = str_replace("[NAME]",$keys[0]['name'], $content);
Lal
  • 14,726
  • 4
  • 45
  • 70