Jump to content

select distinct from a directory


Hooch
 Share

Recommended Posts

Hey all. Need some help here. I have a directory of many images. These images are tagged as follows...1220315898-1.jpg1220315898-2.jpg1220315898-3.jpg1220315973-2.jpg1220316023-1.jpg1220316023-3.jpgThese are images linked to news items. We can have up to 3 images linked to it. Hence the -1, -2 or -3. Now some images may be deleted, (this is an option I am including) that's why you see the odd line up. So I need to be able to show only the images with certain dates tagged to them.Here's my code so far...

		$dir  = "images/fun_stuff/";//Directory to chose from		$pattern = 1220316023;//This is the date from the DB we need		// Open a known directory, and proceed to read its contents		if (is_dir($dir)) 		{			if ($dh = opendir($dir)) 			{				while (($subject = readdir($dh)) !== false) 				{					preg_match($pattern, $subject, $matches); 					print_r($matches);				}			closedir($dh);			}		}

Unfortunately this code shows all the images, when it should print out1220316023-1.jpg1220316023-3.jpgonly.Thank you for any help.

Link to comment
Share on other sites

Since you're passing an integer instead of a regular expression, I suspect it's being evaluated as true for everything matched against it. Try this instead:$pattern = '/1220316023/';And why are you collecting $matches? That's for back references. Try just this:

if (preg_match($pattern, $subject) ){	echo $subject;}

EDIT: I added a right parenthesis.

Link to comment
Share on other sites

I took the while statement out, but it's still not working..

		$dir  = "images/fun_stuff/";//Directory to chose from		// Open a known directory, and proceed to read its contents		if (is_dir($dir)) 		{			if ($dh = opendir($dir)) 			{				$subject = readdir($dh); 				preg_match($pattern, $subject, $matches); 				print_r($matches."<br />");				closedir($dh);			}		}

boen_robot...it's still same result.Deirdre's Dad, I even tried $pattern = '/^1220316023/';and still a no go

Link to comment
Share on other sites

Here's a better set up of my code.

<?PHPsession_start(); include'includes/db.php';$news = mysql_query("SELECT * FROM `fun_stuff` WHERE `id` = '8' ") or die(mysql_error());$r = mysql_fetch_array($news);$pattern = '/^1220442774/';//This is the date from the DB we need$dir  = "images/fun_stuff/";//Directory to chose from// Open a known directory, and proceed to read its contents	   if (is_dir($dir))		{			if ($dh = opendir($dir))			{				while (($subject = readdir($dh)) !== false)				{					preg_match($pattern, $subject, $matches);					print_r($matches."<br />");				}								closedir($dh);			}		}?>

This code should only show 2 results.But what the page reveals is ...ArrayArrayArrayArrayArrayArrayArray

Link to comment
Share on other sites

I TOLD you not to use $matches in this context. It's not what you seem to think. I tested this code and it works:

				while (($subject = readdir($dh)) !== false)				{					if ( preg_match($pattern, $subject) ) {						echo $subject . "<br>";					}				}

I was 99% sure it would work, but you had me guessing.FWIW, if you plan to regularly cycle through a loop hundreds of times (I don't know if you are) the manual recommends using strpos() or strstr() instead. In the long haul, it's faster.

Link to comment
Share on other sites

Here's my working code thanks to DD

<?PHPsession_start(); include'includes/db.php';$news = mysql_query("SELECT * FROM `fun_stuff` WHERE `id` = '8' ") or die(mysql_error());$r = mysql_fetch_array($news);$pattern = '/^'.$r['date'].'/';//This is the date from the DB we need$dir  = "images/fun_stuff/";//Directory to chose from// Open a known directory, and proceed to read its contents	   if (is_dir($dir))		{			if ($dh = opendir($dir))			{				while (($subject = readdir($dh)) !== false)				{					if ( preg_match($pattern, $subject) ) 					{						echo '<br /><img src="images/fun_stuff/'.$subject.'" class="pic_border_larger" style="background-color: #E7E7E7; float: left; margin: 0 0 10px 10px; clear: left; margin:5px;" width="" height=""/>';					}				}			}		}?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...