Jump to content

Recommended Posts

Here is The Code:

if ($handle = @opendir("../../images/year/$year/")) {
    $blacklist = array('.', '..', 'index.php', 'index.html');
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {
	$stmt = $mysqli->prepare("SELECT id, image, title, caption, secureid FROM photogallery WHERE image = ? AND year = ? AND timeofday = ? LIMIT 5");
	$stmt->bind_param('sis', $file, $year, $timeofday);
	$stmt->execute();
	$stmt->store_result();
	$stmt->bind_result($id, $image, $title, $caption, $photoid);
	//$stmt->fetch();
	
	while ($stmt->fetch())
	{
		if (($id) && ($photoid)){
		$idcheck = "<font color=\"green\">Found in Database</font>";
		$admin = "<a href=\"../edit/photos.php?photoid=$photoid\">Edit</a>";
		$details = "<b>$title</b> $caption";
		} else {
		$idcheck = "<font color=\"red\">Not Found in Database</font>";
		$admin = "<a href=\"../add/photos.php?image=$file&year=$year&timeofday=$timeofday\">Add</a>";
		$details = "";
		}
		
		echo "$image $details $idcheck $admin<br>";
	}
	$stmt->close();
	
            echo "$file $details $idcheck $admin<br>";
		}
    }
    closedir($handle);
} else {echo "This image directory was not found.";}

 

What It is Doing:

Right now it is only showing the ones found in the database.

 

What I Want It To Do:

I want it to show all the results found in the database and the ones that are not found in the database. So I can see everything.

 

If you need any more information. Please ask.

Link to comment
https://forums.phpfreaks.com/topic/275190-directory-list-and-mysql/
Share on other sites

Ran some extra tests and changed coding slightly (getting different results):





if ($handle = @opendir("../../images/year/$year/")) {
    $blacklist = array('.', '..', 'index.php', 'index.html');
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {
		
	if($stmt = $mysqli->prepare("SELECT id, image, title, caption, secureid FROM photogallery WHERE image = ? AND year = ? AND timeofday = ?"))
	{
	$stmt->bind_param('sis', $file, $year, $timeofday);
	$stmt->execute();
	$stmt->store_result();
	$stmt->bind_result($id, $image, $title, $caption, $photoid);
	$stmt->fetch();
	$stmt->close();
	}
	
		if ((isset($id)) && (isset($photoid)) && (isset($image))){
		$idcheck = "<font color=\"green\">Found in Database</font>";
		$admin = "<a href=\"../edit/photos.php?photoid=$photoid\">Edit</a>";
		$details = "<b>$title</b> $caption";
		} else {
		$idcheck = "<font color=\"red\">Not Found in Database</font>";
		$admin = "<a href=\"../add/photos.php?image=$file&year=$year&timeofday=$timeofday\">Add</a>";
		$details = "";
		}
		
		echo "$file $image $details $idcheck $admin<br>";
	
		}
    }
    closedir($handle);
} else {echo "This image directory was not found.";}

What It is Doing:
Right now it is only reading the first file through the database and adding those results to the others file in the directory.

 

EXAMPLE: This is what it is doing now.

BoatDock2004.jpg BoatDock2004.jpg Take Off Found in Database Edit
Weigh-in2004.jpg BoatDock2004.jpg Take Off Found in Database Edit
FirstPlace2004.jpg BoatDock2004.jpg Take Off Found in Database Edit


What I Want It To Do:
I want it to show all the results regardless if found in the database or not and with the correct information (right now it is not showing the correct information afterwards as shown in the example above).

 

EXAMPLE: Notice how image files are now matching up. (They do not necessarily need to match up maybe return blank if not found in the database.)

BoatDock2004.jpg BoatDock2004.jpg Take Off Found in Database Edit
Weigh-in2004.jpg Weigh-in2004.jpg NOT Found in Database Edit
FirstPlace2004.jpg FirstPlace2004.jpg NOT Found in Database Edit

Edited by cutielou22
  • Solution

I'll move on to answering your question in a moment, but I'd like to hit another point first.

 

I would recommend against running a query for every single file. If you end up with 500 files, you'll be running 500 queries. I might suggest a different approach.

 

You could either get the entire list of files first and put it into an array and query using something like "where image IN ()".

 

Or depending on you database, you could get the entire list of files in the database and compare that as you run through the files. I would prefer the first option, but from what I remember with MySQLi, it's kind of a PITA to get it to work with arrays. I typically prefer PDO.

 

The reason you are getting the "BoatDock2004.jpg" again and again is because those values are being set once. When the statement returns no results, there are no replacements.

 

So the quick fix for you here is to set the values at the beginning of each loop.

if ($handle = @opendir("../../images/year/$year/")) {
    $blacklist = array('.', '..', 'index.php', 'index.html');
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {
            $id = "";
            $photoid = "";
            $image = "";
            $title = "";
            $caption = "";
            $idcheck = "Not found in database.";
            $admin = "<a href=\"../add/photos.php?image=$file&year=$year&timeofday=$timeofday\">Add</a>";
            $details = "";
            if ($stmt = $mysqli->prepare("SELECT id, image, title, caption, secureid FROM photogallery WHERE image = ? AND year = ? AND timeofday = ?")) {
                $stmt->bind_param('sis', $file, $year, $timeofday);
                $stmt->execute();
                $stmt->store_result();
                $stmt->bind_result($id, $image, $title, $caption, $photoid);
                $stmt->fetch();
                $stmt->close();
            }

            if ((!empty($id)) && (!empty($photoid)) && (!empty($image))) {
                $idcheck = "<font color=\"green\">Found in Database</font>";
                $admin = "<a href=\"../edit/photos.php?photoid=$photoid\">Edit</a>";
                $details = "<b>$title</b> $caption";
            }

            echo "$file $image $details $idcheck $admin<br>";
        }
    }
    closedir($handle);
} else {
    echo "This image directory was not found.";
}

 

Edits: - Fighting the code block formatting...

Edited by teynon
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.