0

I'm adding data from a MySQL database to an array, but the program doesn´t start the if´s

$nextPhotoID = array_map(function ($p) {
    return $p['id'];
},$nextPhoto);

if ($_GET['id'] != $nextPhotoID[count($nextPhotoID) -1]){
    $right = $nextPhotoID[array_search('id', $nextPhotoID) +1]; 

-- $right is undefined variable, latter in code, where i need to write it

}
if ($_GET['id'] != $nextPhotoID[0]){
    $left = $nextPhotoID[array_search('id', $nextPhotoID) -1];
}

-- same with $left

Why is it undefined variable? I don´t understand it at all. My SQL is working.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
vesallek
  • 11
  • 2
  • 1
    If the condition evaluates to false then `$right` is never set and therefore undefined. Similar for `$left`. If your logic requires them to be set in all cases then set them to a default value. – Computable Jan 01 '23 at 14:36
  • what should they have as default value? their id? – vesallek Jan 01 '23 at 14:44
  • Since it appears this code is allowing a user to cycle through a list of photos (to left or to right) it seems a good default for `$right` would be `$nextPhotoID[count($nextPhotoID) -1]` (keep selecting right-most) and similar for `$left` (set to `$nextPhotoID[0])` - I would expect your UI would not allow "moving beyond" limits but this would handle it in any case. – Computable Jan 01 '23 at 14:51
  • If i set it, it only takes the default value – vesallek Jan 01 '23 at 15:15
  • Well then, as mentioned above, that must be because your `if` statement never returns true. We don't know what data you've got so we can't tell you precisely why that is - you'll need to debug it – ADyson Jan 01 '23 at 15:44
  • i have data from mysql database, i have ids, names, type of the file. I tried var_dump on everything, and everything returns data, but the variables from if didn´t – vesallek Jan 01 '23 at 15:50
  • Ok. So then please provide a [mre] of the issue, then we can help more easily – ADyson Jan 01 '23 at 16:20
  • I had problem in the SELECT, but thanks anyways – vesallek Jan 23 '23 at 18:46

0 Answers0