-1

I have tried the code below to select images from different folders using the if...else statement

 $username=$_SESSION['username'];
 $db = mysqli_connect('localhost', 'root', '', 'registrations');

 $selectlevels="SELECT level from users WHERE username='$username'";
 $getlevels=mysqli_query($db,$selectlevels);

 while($lvls=mysqli_fetch_array($getlevels)){

 if ($lvls['level']=='1') {
 $dire="Annotated Dataset/images1/";
 $images = glob($dire. '*.{jpg,jpeg}', GLOB_BRACE);

 shuffle($images);
 $images=array_slice($images,0,6);

 return $images;
 }

 else{
  $dire="Annotated Dataset/images/";
  $images = glob($dire. '*.{jpg,jpeg}', GLOB_BRACE);

 shuffle($images);
 $images=array_slice($images,0,6); 
 return $images;
  }

  $_SESSION['images']=$images;
  }

The $_SESSION['images'] is used in the later code.

When i tried to execute my code, I could not fetch the folders. and in console, there are no errors. I checked the queries, it works perfect. I think, i did something wrong with the if else statement. Can someone help me out with this problems.

Krupal Panchal
  • 1,553
  • 2
  • 13
  • 26
Uviiii
  • 101
  • 8
  • Have you tried using the full/complete path to the images folder? – brombeer Oct 26 '19 at 12:02
  • yes, still not working. I tried without the if else statement just to select one folder, it was working then. – Uviiii Oct 26 '19 at 12:05
  • `$_SESSION['images']=$images;` is probably never hit because you `return` before. You could `var_dump($image)` before your `return`s to see if it contains anything. You also have duplicate code in there that could be reduced – brombeer Oct 26 '19 at 12:08
  • when i tried var_dump it shows the names of images in the array form – Uviiii Oct 26 '19 at 12:13
  • Sorry, had a typo in there: it's `var_dump($images)`, not `var_dump($image)`. – brombeer Oct 26 '19 at 12:19
  • No problem , i tried with correct name. When i tried it showed me the list of images in the array. I think i did something wrong while returning them, Can you help me out with that – Uviiii Oct 26 '19 at 12:22
  • Ok, so if your `$images` contains all the images you should place `$_SESSION['images']=$images;` before your two `return`s to set it - anything after `return` will not be executed. (Do you even need to `return` anything?) – brombeer Oct 26 '19 at 12:22
  • Actually now i figured it out, return was not needed in my code. Thanks for the help – Uviiii Oct 26 '19 at 12:26
  • as soon as you got your value you can break the loop using "break" so that you can have only one value – Pavan Kumar Oct 26 '19 at 12:27
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Oct 26 '19 at 14:52
  • [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Oct 26 '19 at 14:53

1 Answers1

0

Code after returns will not be executed. If you want your $_SESSION['images']=$images; to work you should remove the two return $images;

You could also avoid code repetition by changing your if statements to:

if ($lvls['level']=='1') {
   $dire="Annotated Dataset/images1/";
} else {
  $dire="Annotated Dataset/images/";
}
$images = glob($dire. '*.{jpg,jpeg}', GLOB_BRACE);
shuffle($images);
$images=array_slice($images,0,6);

since only the image path seems to change.

brombeer
  • 8,716
  • 5
  • 21
  • 27