0

I'm stuck on this for a while now: I'm trying to combine two arrays, one is every image in a folder, and the other one is the description of the picture from the SQL Database. In the database I've added the filename and the apartment name. To make my query work I need it to be in a loop, but to show the results of my query, it needs to become a loop, so I would think it must be possible to combine it right? But I can't seem to figure out how to combine these two arrays in a loop. I've tried with two loops and this is what I've got:

<?php

$directory = "images/photos/";
$images = glob('images/photos/*.{jpg,png,gif}', GLOB_BRACE);

foreach ($images as $image) {
    $sqlaa = "SELECT * FROM `afbeelding` WHERE filename = ' . $image . '";
    $titles = mysqli_query($link, $sqlaa);

    while ($row = $titles->fetch_array()) {
        echo '<form action="" method="post"><li>
              <a href="' . $image . '" title="' . $row['apartment'] . '" >
                  <img style="width:150px;height:150px;" src="' . $image . '" />
              </a>
              </li>';
    }
}
?>

If anyone knew, that'd be great! Thanks in advance

6
  • Do you have one row in afbeelding table per image? Commented Dec 28, 2016 at 14:57
  • 1
    You may be able to remove your loop over $images. Instead of WHERE filename = $value you could do WHERE filename IN ($listOfValues). In general, I find that having to do a loop involving a repeated query means I have either designed my database poorly or I can revise my query to get all the data at once. Commented Dec 28, 2016 at 14:59
  • @VitaliiStrimbanu Ofcourse, filename and apartment, so every file can be linked at an apartment Commented Dec 28, 2016 at 15:00
  • @SurrealDreams When I delete my $image loop, my images won't show anymore. Its a loop for echoing every image in a folder. Would be pointless without it Commented Dec 28, 2016 at 15:03
  • 1
    How is it pointless? you have your $directory and you have a filename field from database, $directory . $row['filename'] should do the trick. Commented Dec 28, 2016 at 15:07

3 Answers 3

2

You can try making the loop over the data and then check the image in the loop, this could be faster and avoids using a query inside a loop.

<?php

$directory = "images/photos/";
//$images = glob('images/photos/*.{jpg,png,gif}', GLOB_BRACE);

$sqlaa = "SELECT filename,apartment FROM `afbeelding`";
$titles = mysqli_query($link, $sqlaa);
while($row = $titles->fetch_array())
{
   $image=$row["filename"];
   if (file_exists($directory.$image)){
     echo '<form action="" method="post"><li>
      <a href="'.$image.'" title="' . $row['apartment'] . '" >
      <img style="width:150px;height:150px;" src="'.$image.'" />
      </a>
      </li>';
  }
}
?>

I don't know if the image is stored using the full relative path "images/photos/img.png" or the base filename only "img.png".

If the data in afbeelding is huge, you can improve the sql query using an IN filter

function imageNames($item){
    return "'$item'";
}

$images = glob('images/photos/*.{jpg,png,gif}', GLOB_BRACE);

$names=array_map("imageNames",$images);
$filter_names=implode(",",$names);

$sqlaa = "SELECT filename,apartment FROM `afbeelding` WHERE filename IN ($filter_names)";

And then proceed with the loop, without checking files.

Sign up to request clarification or add additional context in comments.

Comments

1

How about... going about it differently.

You already have the filenames in database. So just poll your database with your available data, and then just echo it. I'm assuiming it's stored as images/photos/filename.jpg in your database. But without a data sample as is in your database I can't speculate more.

$directory = "images/photos/";

foreach ($images as $image) {
    $sqlaa = "SELECT * FROM `afbeelding` WHERE filename like '%images/photos/%'";
    $titles = mysqli_query($link, $sqlaa);

    while ($row = $titles->fetch_array()) {
        echo '<form action="" method="post"><li>
              <a href="' . $image . '" title="' . $row['apartment'] . '" >
                  <img style="width:150px;height:150px;" src="' . $row['filename'] . '" />
              </a>
              </li>';
    }
}

1 Comment

You're absolutely right, @VitaliiStrimbanu thank you too, you were right. I was thinking too difficult
0

first: loops on db-calls are not really best practice..

A possible result to your question:

<?php

$directory = "images/photos/";
$images = glob('images/photos/*.{jpg,png,gif}', GLOB_BRACE);

$images = array_flip($images);

// get data first
foreach($images as $image => $v) {
    $sqlaa = "SELECT * FROM `afbeelding` WHERE filename = ' . $image . '";
    $titles = mysqli_query($link, $sqlaa);

    if ($title->num_rows == 1) {
        $row = $titles->fetch_array();
        $images[$image] = $row['apartment'];
    } else {
        $images[$image] = '';
    }
}

// render data
foreach ($images as $filename => $description) {
    echo '<form action="" method="post"><li>
        <a href="'.$filename.'" title="' . $description . '" >
            <img style="width:150px;height:150px;" src="'.$filename.'" />
        </a>
        </li>';
}
?>

Not really nice, but plain simple..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.