Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
Hi all, it's me again with another php question. I made another post about php and mysql in which I got off on a tangent about automatically displaying images in a folder. A lot of you guys helped out and I was psyched.

It seems the easiest way, right off the bat, is the suggestion to use: phpimageview. I checked it out and it seems to be simple enough for what I want to do. Maybe I can get started with this and get gutsy later on.

Anyhoo, what I want to do is shown in the attached picture. The top title/menu part is all taken care of. The "pick a state/links" part shouldn't be a problem for me now...

The picture, as well as the "next", "previous" and the numbers are all done with the phpimageview script. No problems there.

The problem is tieing in a picture description with each picture. Based on my limited experience thus far, it seems like the logical way to go about it would be to use another mysql database/field and add the descriptions in there. From there, do some sort of fancy-pants linking beyond the random image that was grabbed and the description of that picture from the mysql database.

Anyone have any better ideas? Any tips? Any pointers? Any code they want to do for me? ;)
 

Attachments

  • desired.jpg
    desired.jpg
    42.2 KB · Views: 239

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
Yup, I'd say definitely try to pull the description in from the DB. The question is: how can you integrate it into phpimageview's (PIV) code?

How does PIV set the image file? Variable? Filename? If you can figure out the variable and/or filename structure it uses, you can tack on a DB query to grab the image description.
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
Well, here's phpimageview in its entirety... I'm struggling to understand how it works...

It seems like he just opens whichever directory this file is in, and extracts all the *.jpg, *.gif, *.png files. There's an array in there somewhere... a lot of arrays maybe... a lot of variables.... head spinning...

ugh.
PHP:
<?php
$bg="#ffffff";	// background of the for+next cells
$fornext=1;	// display for+next arrows 1=yes 0=no
$next=" <b> Next </b> ";// text displayed in the next field
$last=" <b> Previous </b> ";// text displayed in the last field
$textlinks=1;	// display textlinks to the images 1=yes 0=no
$dropdown=0;	// display dropdown menu with names 1=yes 0=no
$dropdowntext="show";	// text display on the button next to the dropdown
$namedisp=0; 	// display name of the pic (capitalized filename) 1=yes 0=no
$xofy=0;		// display pic x of y 1=yes 0=no


$SCRIPT_NAME=$SERVER_VARS['PHP_SELF'];
$pic=$HTTP_GET_VARS['pic'];

//	the directory name 
$handle=opendir('.');
//	start HTML, you can tweak that!
echo "<div align=\"center\">\n\t<font face=\"verdana,arial,helvetica\" size=2>\n";

//	initialize variables
$pics=array();
$count=0;
//	read directory into pics array
while (($file = readdir($handle))!==false) {
	//	filter for jpg, gif or png files... 	
	if (substr($file,-4) == ".jpg" || substr($file,-4) == ".gif" || substr($file,-4) == ".png" || substr($file,-4) == ".JPG" || substr($file,-4) == ".GIF" || substr($file,-4) == ".PNG"){
	// 	you can apply other filters here...
		$pics[$count] = $file;
		$count++;
	//	don't forget to close the filter conditions here!
	}
}
closedir($handle); 

//	done reading, sort the filenames alphabetically, shade these lines if you want no sorting
// sort($pics);
// reset($pics);

//	define the selected picture, to highlight textlink, preselect dropdown and define for+next links
for ($f=0;$f<=sizeof($pics)-1;$f++){if ($pic==$pics[$f]){$selected = $f+1;}}

//	display dropdown if wanted...
if ($dropdown==1){
	echo "\t\t<!-- dropdown -->\n\t\t<form name=\"mainform\">\n\t\t\t<select name=\"pic\">\n";
	//	loop over all pics
	for ($f=0;$f<=sizeof($pics)-1;$f++){
		//	Capitalize filename for display
		$name=ucfirst(substr($pics[$f],0,-4));
		// if the pic is the selected one set selected status
		if ($pic==$pics[$f]){echo "\t\t\t\t<option value=\"".$pics[$f]."\" selected>".$name."</option>\n";}
		//	or simply render another option
		else{echo "\t\t\t\t<option value=\"".$pics[$f]."\">".$name."</option>\n";}
		}
	// close select statement and display show button with predefined text.
	echo "\t\t\t</select>\n\t\t\t <input type=\"submit\" value=\"".$dropdowntext."\">\n\t\t</form>\n\t\t<!-- end dropdown -->";
}

//	if there is already a pic selected...
if ($pic && !preg_match("/javascript/",$pic)){
	//	if the text should be displayed
	if ($namedisp==1){
		// Capitalize filename for display andf print it
		$name=ucfirst(substr($pic,0,-4));
		echo "\n\t\t<!-- imagename -->\n\t\t<b>".$name;
		}
	//	if pic x of y is selected, display it	
	if ($xofy==1){
		echo " ".$selected."/".sizeof($pics);
		}
	echo "</b>\n\t\t<!-- imagename end -->\n";
	// Display table with for+next arrows, and a black line around the image
   echo "\t\t<!-- table with image -->\n\t\t<table width=1 border=0 cellspacing=0 cellpadding=1 bgcolor=\"#000000\">\n\t\t\t<tr>\n\t\t\t\t<td bgcolor=\"".$bg."\">";
	// if for+next arrows are selected and the picture is not the first one, display last arrow
	if ($selected != 1 && $fornext==1){
		echo "<a href=\"$SCRIPT_NAME?pic=".$pics[$selected-2]."\">$last</a>";
		}
	else { echo "<font color=\"".$bg."\">$last</font>";}
		echo"</td>\n\t\t\t\t<td><image src=\"".$pic."\" alt=\"".$name."\" border=0></td>";
	// if for+next arrows are selected and the picture is not the last one, display next arrow
	if ($selected != (sizeof($pics)) && $fornext==1){
		echo"\n\t\t\t\t<td bgcolor=\"".$bg."\"><a href=\"$SCRIPT_NAME?pic=".$pics[$selected]."\">$next</a>";
		}
	else { echo"\n\t\t\t\t<td bgcolor=\"".$bg."\"><font color=\"".$bg."\">$next</font>";}
	echo"</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<!-- table with image end -->\n\t\t<!-- Textlinks--->\n\t\t";
}
//	if textlinks display is selected
	if ($textlinks == 1){
		// loop over images
		for ($f=0;$f<=sizeof($pics)-1;$f++){
			// add gaps between the links, unless it is the first one
			if ($f > 0) echo "  ";
			// if the link to the pic is the selected one, display a bold number and no link
			if ($pic==$pics[$f]){echo "<b>".($f+1)."</b>";}
			// otherwise display the link
			else{echo "<a href=\"$SCRIPT_NAME?pic=".$pics[$f]."\">".($f+1)."</a>";}
			// make linebreaks every 15 times!
			$isbr = strpos((($f+1)/15),".");
				if (!$isbr){echo "<br>";}
			}
	}
// close HTML :-)
echo"\n\t\t<!-- end textlinks -->\n\t</font></div>";
echo "\n\n<!-- Done with PHPImageview, get it at [url]http://www.onlinetools.org/phpimageview[/url] -->";
?>
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
It looks like it's just '$pic', so...

Just toss in a MySQL query that selects the description that matches the filename. Something like:

PHP:
$Link = mysql_connect ($Host, $User, $Password);
$Query = "select * from $TableName where pic='$pic'";
$Result = mysql_db_query ($DBName, $Query, $Link);
$desc = mysql_fetch_array($Result);
echo "$desc[text]";

The $desc[text] pulls the value out of the $desc array with the field name "text", so change it to whatever's appropriate.

The rest is, hopefully, clear. :)
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
Hmmm, it seems so simple... what's the catch? ;)

Thanks for the help (again). Hopefully I'll have some free time today so I can give it a shot.
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
It works! Woohoo!

Moving onward... is there a way to incorporate the $state variable now to display only pictures from a certain state? I could easily add a 'state' field to my new mysql table... but I'm not sure how to incorporate that into the existing PIV code... or if it's even possible.

The easy way out would be to start separate directories for each state, and just put this PIV in each directory (since it plucks up all the pics from its own directory). Maybe that's the way to go, for organizational purposes anyway.

Oh, and here's something that's kinda bugging me... how come it (PIV) doesn't display the first image by default? It starts up with no image... I can't seem to hack that far into the code to figure that out.
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
Noice!

The organizing by folders seems the best route to me.

You could always try manually specifying the first image for it to show by tossing a pic name in the URL:

stickers/nj/index.php?pic=nj1

Or something like that...it takes away some of the dynamic functionality, but give you what you want.
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
OK, I think I've mangled the phpimageview code enough to get it to do what I want... I think...

And I think I agree that using a folder for each state is the way to go as well.

Now what I'm planning on doing is using some kind of menu across the top to select which state to view, something simple like:

Pick a State
NJ - NY - MA


Where "NJ" and "NY" and "MA" would obviously take you to the folders of pictures for each state. Right now I wrote up a little "picturemenu.inc" file that just has standard hyperlinks. What I'd like to do is be able to make whichever the current folder is, plain text... For instance, assume you're checking out NJ pics:

Pick a State
NJ - NY - MA

I'm thinking I could use the command that grabs the current directory and set that to some variable... and use that variable to check which link should be plain text... but that's where my logic starts to fizzle...
 

sonofslim

macrumors 6502a
Jun 6, 2003
742
0
2 thoughts:

you could wrap each link in your .inc with a bit of code that echos out the <a> tag. ie:
PHP:
<? echo '<a href="state.php">' ?>state<? echo '</a>' ?>
and then add a conditional that controls whether or not the tag is actually echoed. for instance:
PHP:
<? if (!$state='ny')
   { echo '<a href="ny.php">'; ?> ny <? echo '</a>'; } ?>
so that in every case in which the state is not active, it's wrapped in the appropriate tag.


OR: you could use a similar conditional to add a CSS class selector to your link that would somehow indicate that this link is the current link:
PHP:
<a href="ny.php" <? if state='ny' { echo ' class="here" '; } ?> >ny</a>

i prefer this method myself, as it takes up less code, and also it seems to me to be semantically superior. think about it: what you're doing, essentially, is signifying location. you don't really need to change the structure of your document to do so, so why not accomplish this with presentation? i think it's perfectly acceptable to leave a link active when it points to the current page. otherwise, you've got a link that sometimes isn't a link, which to me is poor useability.

</preachy>
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
Ah, pages linking to themselves...one of the favorite targets of usability expert Jakob Nielsen. ;)

I say use a CSS class like sonofslim mentioned.

A cool way to do it is to write each link as:
<a href="whatever" class="$state">$state</a>

Then just have a class for the "here" class that references by the state variable.

a.$state {blah}
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
OK, I follow sonofslim, but not Rower...

irregardless, I'm going to need to get the state variable. I can get this by grabbing the name of the current directory. I can use the getcwd() function, but that displays the full path... how do I trim that down just to get the top-most directory? (note: it may be more than two characters in some instances... like Australia)




Robin... how do I differentiate the active link from any other link using your method? Let's say I have a set of links...


<a href="http://www.brianellisrules.com/stickers/pics/IL/">IL</a> -
<a href="http://www.brianellisrules.com/stickers/pics/MA/">MA</a> -
<a href="http://www.brianellisrules.com/stickers/pics/NJ/">NJ</a> -
<a href="http://www.brianellisrules.com/stickers/pics/NY/">NY</a>

If I use your method, and replace each state (IL, MA, NJ, and NY) with $state, then every link will show up as NY or NJ or whichever happens to be the current directory, right? Or am I not following something? I'm slow like that.
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
Sorry, I bungled that one. :eek:

Use the CSS I wrote and make your links:
<a href="http://www.brianellisrules.com/stickers/pics/IL/" class="IL">IL</a> -
<a href="http://www.brianellisrules.com/stickers/pics/MA/" class="MA">MA</a> -
<a href="http://www.brianellisrules.com/stickers/pics/NJ/" class="NJ">NJ</a> -
<a href="http://www.brianellisrules.com/stickers/pics/NY/" class="NY">NY</a>

That way when $state is "NY" the CSS will be:

a.NY { blah }
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
man, now I can't get either method to work....

PHP:
<?php
$cwd = getcwd();
$state = basename ($cwd);
echo $cwd;
echo "<br>";
echo $state;
echo '<h1>Pick a State!</h1>';
echo '<a href="http://www.brianellisrules.com/stickers/pics/IL/" class="IL">IL</a> - ';
echo '<a href="http://www.brianellisrules.com/stickers/pics/MA/" class="MA">MA</a> - ';
echo '<a href="http://www.brianellisrules.com/stickers/pics/NJ/" class="NJ">NJ</a> - ';
echo '<a href="http://www.brianellisrules.com/stickers/pics/NY/" class="NY">NY</a>';
?>


and my css file has an entry that looks like:

a.$state { color: black; text-decoration:none; }


any idea where it's screwing up? the links are coming out blue, which is my default styling for links.

I have it printing the value for $state, and that part is working correctly...
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
OK, I should've pointed this out, but that CSS needs to be in the PHP document in an inline statement in the head tag. CSS doesn't parse variables.

So add something like this in your head tag:
PHP:
if ($state) {
echo "<style type=\"text/css\">a.$state { color: black; text-decoration:none; }</style>";
}
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
This is getting beyond discouraging. I couldn't get either method to work for whatever reason, so I figured I'd cheat and throw something like this in there:

PHP:
$cwd = getcwd();
$state = basename ($cwd);
echo $state;
if ($state==pics) {
echo '<h1>Pick a State!</h1>';
}
else {
echo "<h1>Pictures from $State</h1>";
}

how do I get the value for $State to print out? I've tried various combinations of single and double quotes, but I'm having zero luck.

php is winning this battle.
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
You're setting the 'state' variable to the filename of the current page, then you're checking to see if the variable for state equals the string 'pics' and then you've got an uppercase 'State' variable in the second h1.

Where are we going here?
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
*nervous laugh*

variables... are.... case sensitive?

*nervous laugh*


call the idiot police on me for that one.

anyhoo, here's directory my structure:

/stickers/pics/
/stickers/pics/nj
/stickers/pics/ny
/stickers/pics/ma

by default, the user will be put into the /pics/ directory.

each directory has an index file which references the previous bit of code.... so if it's in the /pics/ directory, it'll say "pick a state" (with a link to all the states), where as if it's in one of the state directories, it'll say "pictures from ___". It was my work-around for not being able to figure out how to get the conditional css bit to work.


so, yeah... I'm just going to slowly back away with my tail between my legs... :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.