Hooch Posted September 5, 2008 Share Posted September 5, 2008 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 More sharing options...
boen_robot Posted September 5, 2008 Share Posted September 5, 2008 Try $pattern = "^1220316023"; just in case. Link to comment Share on other sites More sharing options...
jeffman Posted September 5, 2008 Share Posted September 5, 2008 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 More sharing options...
Hooch Posted September 5, 2008 Author Share Posted September 5, 2008 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 More sharing options...
Hooch Posted September 5, 2008 Author Share Posted September 5, 2008 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 More sharing options...
jeffman Posted September 5, 2008 Share Posted September 5, 2008 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 More sharing options...
Hooch Posted September 5, 2008 Author Share Posted September 5, 2008 Thanks DD, but I am trying that too.I just wanted to show my code a little better.I'm actually adding your code below what you see above.As you posted that last remark, I was trying it out.And yes it worked!!Thank you Link to comment Share on other sites More sharing options...
Hooch Posted September 5, 2008 Author Share Posted September 5, 2008 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 More sharing options...
jeffman Posted September 5, 2008 Share Posted September 5, 2008 Glad it works. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.