Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing image names to text

Status
Not open for further replies.

DeepBlerg

Technical User
Jan 13, 2001
224
AU
What I have is a page of about 150 products with id codes. The page is generated in excel and outputted as html then php calls it and displays on web page.

There are close to 100 images that relate to each product which, when the user clicks product id code the image should open in a new window. All the images are named by their id, ex "id5053.jpg".

Instead of having 100 if statements I thought maybe there would be a way to make an array of all the images in the images folder minus the &quot;.jpg&quot; and use the array to replace all text that is matching in the page with the appropriate <a href> tags.

Anyone know or done this before?
 
I actually just finished a page that did a modified versionof this. Except, what it did was parse the page for \w*_thumb\.(jpg|gif|png), and displayed that on screen, with a link to it's larger image. So essentially it was a thumbnail browser, displaying anything that ended in _thumb, linking to the larger image. It should page at every 30 images, but I haven't gotten around to testing this functionality yet. It's worked in other places though, so I don't think I'll have too big of a problem.

I've gotten into the very good habit of commenting my code as I write it, so I'm really not gonna add anything else. If you get lost however, please post back and I'll be more than happy to elaborate. Note, I relied heavily on the PHP manual Directory section, Regular Expression(Perl Compatible) section and
Filesystem section.

here's the code I used:
Code:
<?
/*
 *eye/index.php
 * the pictures and whatnot on the site.
 */
define(&quot;IMGDIR&quot;,&quot;./pics&quot;); //the picture directory

/*showLinks
 * @params: st, where to start listing from
 * @returns: none
 * show the thumbnailed images from the directory
 */
function showLinks($st) {
	$st = (int)$st;		//where to begin the array display
	$files = array();	//grab the array of files so we can sort it
	$dir = dir(IMGDIR);
	$i = 0;
	while($file = $dir->read()) {
		if(preg_match(&quot;/(_thumb\.(jpg|gif|png))$/&quot;,$file) 
			&& !(is_dir($file))) { //only get _thumbs, since those are thumbnails
			//print(&quot;<br />&quot; .filemtime(IMGDIR . &quot;/$file&quot;).&quot;<br />$file&quot;);
			
			//use the $i++ so all files show. otherwise, files with 
			//the same mtime overwrite each other
			$files[filemtime(IMGDIR . &quot;/$file&quot;) . &quot;-&quot; . $i++] = $file;
		} //end if
	} //end while
	$dir->close();

	krsort($files); //sort the array by timestamp
	$ct = count($files);	//for the paging
	$files = array_slice($files,$st,30); //only use the part we need

	if(count($files) == 0) { //no images :(
		print(&quot;Under construction... please check again later&quot;);
	} else { 
		print(&quot;<table cellpadding=\&quot;3\&quot; cellspacing=\&quot;2\&quot; border=\&quot;0\&quot;>\n<tr>&quot;);
		//now we have the list, so lets start where we should
		//and show the links
		$i=1;
		foreach($files as $time=>$img) {
			$time = strftime(&quot;%m.%d.%Y&quot;,preg_replace(&quot;/-\d+/&quot;,&quot;&quot;,$time));
			print(&quot;<td><a href=\&quot;showpic.php?p=&quot; .urlencode($img). 
				&quot;\&quot;><img src=\&quot;pics/$img\&quot; border=\&quot;0\&quot; alt=\&quot;dated: $time\&quot; />
				</a></td>&quot;);
			if($i % 3 == 0) print(&quot;</tr>\n<tr>&quot;); //for the line breaks
			$i++;
		} //end foreach

		while(true){ //fill any possible gaps
			print(&quot;<td> </td>&quot;);
			if($i++ % 3 == 0) break;
		} //end while
		print(&quot;</tr><tr><td colspan=\&quot;7\&quot; align=\&quot;center\&quot;>&quot;);
		
		//begin paging mech
		for($i=0;$i<$ct;$i+=29) {
			print(&quot;<a href=\&quot;index.php?st=$i\&quot;>&quot; . ($i/29 + 1)	. &quot;</a> | &quot;);
		} //end for
	} //end if
} //end if
require(&quot;../includes/header.php&quot;);
print(&quot;<div class=\&quot;header\&quot; style=\&quot;padding-bottom: 10px;\&quot;>images </div>&quot;);
showLinks($HTTP_GET_VARS[&quot;st&quot;]);
require(&quot;../includes/footer.php&quot;);
?>

hth leo

------------
Leo Mendoza
lmendoza@garbersoft.net
 
Hi vasah20,
Thanks for posting your code. I have tried to modify it for my purposes but as I've never done this before it doesn't work.

btw, $MyStr = the content read from the html page

Here's what i've done:

if ($id == &quot;14&quot;) {
define(&quot;IMGDIR&quot;,&quot;./images/nally&quot;);
// the picture directory
$files = array();
$filend = array();
// grab the array of files so we can sort it
$dir = dir(IMGDIR);
$i = 0;
while($filend = $dir->read()) {
substr(&quot;filend[$i]&quot;, 0, -4);
// remove .jpg from the filename
$i++;
}
$i = 0;
while($file = $dir->read()) {
$badStr = &quot;$filend&quot;;
$goodStr = &quot;/<a href=\&quot;javascript:launchwin('\/images\/nally\/$file','newwindow',left=22,top=22,scrollbars=yes')\&quot;>$filend<\/a>/&quot;;
$MyStr = preg_replace($badStr, $goodStr, $MyStr);
$i++;
}
$dir->close();
}
print $MyStr;
 
Sorry, ignore my last post, here's the real one:

if ($id == &quot;14&quot;) {
$dir = opendir('./images/nally');
// the picture directory
$files = array(readdir($dir));
$filend = $files;
$arcount = count ($filend);
// grab the array of files
$i = 0;
while($i != $arcount) {
substr(&quot;filend[$i]&quot;, 0, -4);
// remove .jpg from the filename
$badStr = &quot;$filend[$i]&quot;;
$goodStr = &quot;/<a href=\&quot;javascript:launchwin('/images/nally/$file[$i]','newwindow',left=22,top=22,scrollbars=yes')\&quot;>$filend[$i]</a>/&quot;;
$MyStr = preg_replace($badStr, $goodStr, $MyStr);
$i++;
}
closedir($dir);
}
print $MyStr;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top