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
The other thread was getting out of hand, so I opted for a fresh start. I hope that's cool.

So, I've got this bit of code. It basically reads all the (image) files within a directory and displays them. Works great, but I'm looking to tweak it a bit. Here's the code:
PHP:
<?php
include('/home2/brian/public_html/head.inc');

$bg="#ffffff";			// background of the for+next cells
$fornext=1;				// display for+next arrows 1=yes 0=no
$next='<img src="http://www.brianellisrules.com/miscpics/next.gif" border=0>';// text displayed in the next field
$last='<img src="http://www.brianellisrules.com/miscpics/prev.gif" border=0>';// text displayed in the last field
$textlinks=1;			// display textlinks to the images 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); 

// ------ I'm thinking this part needs to be modified -------

//	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;}}

//	if there is already a pic selected...
if ($pic && !preg_match("/javascript/",$pic)){
	   echo "<image src=\"".$pic."\" alt=\"".$name."\" border=0>";
}

//---------- modified part ------------

$db = mysql_connect("localhost", "brian_stickers"); 
mysql_select_db("brian_stickers", $db);
$Result = mysql_query("select * from sticker_pics where pic='$pic'", $db); 
$desc = mysql_fetch_array($Result); 
echo ("<p class=pictitle>$desc[title]</p>");
echo ("<p class=picdesc>$desc[text]</p>");

//	if there is already a pic selected...
if ($pic && !preg_match("/javascript/",$pic)){
	// Display for+next arrows
 	// 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>";
		}
	// 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<a href=\"$SCRIPT_NAME?pic=".$pics[$selected]."\">$next</a>";
		}
}

echo "<br><br>";

//	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>";}
			}
	}
?>
And here's the code in action.

As you can see, when you first load the page, there's no image specified, so nothing is displayed. After one of the numbers is clicked, the picture is displayed and everything is fine.

It works as is, but I'd love to have it display the first image by default. I was thinking the bit of code I marked off with the row of hypens is what needs to be modified.... I tried deciphering it, but the preg_match is throwing me off... especially the "/javascript/" part since I thought this was php?

Anyhoo, back to the original problem of no image being displayed by default... I was thinking, since the listing of pics for each page is stored in an array, would it be possible to add some command to have it display $pics[0] by default when a page is first opened?

I realize I'm probably annoying as crap with all these (really lame) questions, but I do appreciate all the help I've gotten so far.
 

whocares

macrumors 65816
Oct 9, 2002
1,494
0
:noitаɔo˩
Re: Attn: php gurus v2

Originally posted by ber.com
I realize I'm probably annoying as crap with all these (really lame) questions, but I do appreciate all the help I've gotten so far.

Well not quite as crap, more of the *poop* level. Nah, just kidin'ya :p

My guess is you just need to initilize the $pic variable that tells the script which photo to read. Just try throwing in a if (!$pic) $pic = $pics[0]; more or less where you inserted your row of hiphens. See how that works. It should basically act as if you clicked on the "1" text on the page.
 

whocares

macrumors 65816
Oct 9, 2002
1,494
0
:noitаɔo˩
Originally posted by ber.com
That was pretty simple

To make it more 'fancy' you can try the following:
$pic or $pic = $pics[0];

This is basically the same statement in a more simple syntax. I'm not 100% sure it'll work though :p
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.