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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl displaying problems

Status
Not open for further replies.

weibs

Programmer
Dec 17, 2008
61
US
I am having a display issue problem in my code in. The only way to tell you is to show you.

Now if you look at the second listing down, it should have only the house image next to it... not the no photo available image.

Here is an exerpt of the code for that part of the page and what I have tried
Code:
opendir DIR, "/home/prud/public_html/Prudential/thumbs";     # . is the current directory  
while ( $filename = readdir(DIR) ) {

if ($filename ne "$emps[0].jpg")  {
@list = ( $emps[0] );
 		%temp;
 		@list = grep { ++$temp{$_} < 2 } @list;
 	foreach (@list) {
if ($filename ne "$emps[0].jpg")  {
print <<OH;
   	<td valign=\"top\" align=\"center\" width=\"220\" class=\"realtortexttwo\"><img src="[URL unfurl="true"]http://www.prudentialjackwhitevista.com/Prudential/00000000tn.jpg"[/URL] width="124" height="93" border="1" alt="$emps[0].jpg" /><BR>
	No Photo Available</td>
OH
}}
}
} 
closedir DIR;

I have also tried
Code:
opendir DIR, "/home/prud/public_html/Prudential/thumbs";     # . is the current directory  
while ( $filename = readdir(DIR) ) {
if ($filename eq "$emps[0].jpg")  {
print <<OH;
   	<td valign=\"top\" align=\"center\" width=\"220\" class=\"realtortexttwo\"><br><br><img src="[URL unfurl="true"]http://www.prudentialjackwhitevista.com/Prudential/thumbs/$emps[/URL][0].jpg" width="100" height="75" border="1" alt="$emps[0].jpg" /><br></td>
OH
}else{
@list = ( $emps[0] );
 		%temp;
 		@list = grep { ++$temp{$_} < 2 } @list;
 	foreach (@list) {
if ($filename ne "$emps[0].jpg")  {
print <<OH;
   	<td valign=\"top\" align=\"center\" width=\"220\" class=\"realtortexttwo\"><img src="[URL unfurl="true"]http://www.prudentialjackwhitevista.com/Prudential/00000000tn.jpg"[/URL] width="124" height="93" border="1" alt="$emps[0].jpg" /><BR>
	No Photo Available</td>
OH
}}
}

} 
closedir DIR;

Can someone figure out why it's displaying the No Photo Available image next to the house image?

Thanks in advance.
 
What is in the @emps array? You use it to see if $filename is equal or not equal to $emps[0] which is always only the first element of the array. Some lines of the code appear to be nothing useful, mainly these lines:

Code:
@list = ( $emps[0] );
         %temp;
         @list = grep { ++$temp{$_} < 2 } @list;

@list has only one thing in it, $emps[0], whatever that is, then you check it using a hash to see if there are duplicates, but of course there can't be because you just defined @list with only one thing in it.

Then you loop through @list which also seems unecessary since all it has in it is $emps[0].

To me it looks like it could be written like this:

Code:
opendir DIR, "/home/prud/public_html/Prudential/thumbs";     # . is the current directory  
while ( $filename = readdir(DIR) ) {
   if ($filename eq "$emps[0].jpg")  {
      print qq(<td valign="top" align="center" width="220"
class="realtortexttwo"><br><br><img src="[URL unfurl="true"]http://www.prudentialjackwhitevista.com/Prudential/thumbs/$emps[/URL][0].jpg"
width="100" height="75" border="1" alt="$emps[0].jpg" /><br></td>);
   }
   else {
      print qq(<td valign="top" align="center" width="220"
class="realtortexttwo"><img src="[URL unfurl="true"]http://www.prudentialjackwhitevista.com/Prudential/00000000tn.jpg"[/URL]
width="124" height="93" border="1" alt="$emps[0].jpg" /><BR>No Photo Available</td>);
   }
}
closedir DIR;

But then all it will do is display $emps[0] (or the no photo image) unless you are doing something with @emps that can't be seen in the code you posted.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 

The $emps[0] array is coming from a mySql database

Code:
$sth = $dbh->prepare ("SELECT * FROM events WHERE active='yes' order by CityName");
	$sth->execute ();
 
	while( @emps = $sth->fetchrow) {
 	$MLSNumber  = $emps[0];

The mlsnumber is always the name of the image so that's why the check between the $filename and the $emps[0] to see if there is a match else display the no photo available image.

I haven't tried your code yet, I will do so tomorrow. Thank you for taking the time to look and responde.

 
The grepping is not doing anything, lets use an example:

Code:
$emps[0] = 'frog';
@list = ( $emps[0] );
%temp;
@list = grep { ++$temp{$_} < 2 } @list;
for (@list) {
   print "$_\n";
}

Of course this will only ever print "frog", and that is exactly what your code is doing with whatever the value of $emps[0] is at that point in your code.

Your other bit of code has the value of $emps[0] assigned to $MLSNumber which is not used in the original code you posted.

Try this and see what the values being compared are:

Code:
opendir DIR, "/home/prud/public_html/Prudential/thumbs";     # . is the current directory  
while ( $filename = readdir(DIR) ) {
   if ($filename eq "$emps[0].jpg")  {
      print qq(<td valign="top" align="center" width="220"
class="realtortexttwo">$filename eq $emps[0]</td>);
   }
   else {
      print qq(<td valign="top" align="center" width="220"
class="realtortexttwo">$filename ne $emps[0]</td>);
   }
}
closedir DIR;

report back what gets outputted.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Can you just take a step back here and tell us what the overall code is supposed to acheive?

From the two snippets you've given us, it looks like there's an outer loop which is looping through the "events" table in your database.

Within that there's an inner loop which goes through your thumbnails directory and output a "no photo" image for every one that doesn't match, in addition to the matching image if there is one.

Is that really what you want?

It seems more likely that you'd want to print just one image for each event, whether it be the matching one or a "no photo" one. In that case there's no need to loop through the directory each time looking for the file, just do something like this:
Code:
$sth = $dbh->prepare ("SELECT * FROM events WHERE active='yes' order by CityName");
$sth->execute ();
 
while( @emps = $sth->fetchrow) {
  $MLSNumber  = $emps[0];

  # Check to see if a thumbnail file exists...
  if (-e "/home/prud/public_html/Prudential/thumbs/$emps[0].jpg") {
    # Yes it does!
    print qq(<td valign="top" align="center" width="220"
class="realtortexttwo"><br><br><img src="[URL unfurl="true"]http://www.prudentialjackwhitevista.com/Prudential/thumbs/$emps[/URL][0].jpg"
width="100" height="75" border="1" alt="$emps[0].jpg" /><br></td>);
  } else {
    # No it doesn't.
    print qq(<td valign="top" align="center" width="220"
class="realtortexttwo"><img src="[URL unfurl="true"]http://www.prudentialjackwhitevista.com/Prudential/00000000tn.jpg"[/URL]
width="124" height="93" border="1" alt="$emps[0].jpg" /><BR>No Photo Available</td>);
  }
}


-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Thanks for that ChrisHunt, it's getting better, but there is still a problem. Take a look

It is now displaying the right image next to each event, but not just one image.

And yes, my final goal here is to produce just one image next to the event. Either a no photo availabe if there isn't a match in the thumbs directory, or the appropriate image if there is one.

Thanks for your help.
 
Clearly looping through the "thumbs" directory is not the correct way to go about this. Chris' suggestion looks like it should work if you also add the code to display the listing description inside the if/else block where the script decides if there is an image or not. Something like:

Code:
$sth = $dbh->prepare ("SELECT * FROM events WHERE active='yes' order by CityName");
$sth->execute ();


while( my @emps = $sth->fetchrow) {
  my $MLSNumber  = $emps[0];

  # Check to see if a thumbnail file exists...
  if (-e "/home/prud/public_html/Prudential/thumbs/$emps[0].jpg") {
    # Yes it does!
    print qq(<td valign="top" align="center" width="220"
class="realtortexttwo"><br><br><img src="[URL unfurl="true"]http://www.prudentialjackwhitevista.com/Prudential/thumbs/$emps[/URL][0].jpg"
width="100" height="75" border="1" alt="$emps[0].jpg" /><br></td>
[red]<td valign="top" class="realtortexttwo" width="480">$emps[?]</td></tr></table>[/red]);

  } else {
    # No it doesn't.
    print qq(<td valign="top" align="center" width="220"
class="realtortexttwo"><img src="[URL unfurl="true"]http://www.prudentialjackwhitevista.com/Prudential/00000000tn.jpg"[/URL]
width="124" height="93" border="1" alt="$emps[0].jpg" /><BR>No Photo Available</td>
[red]<td valign="top" class="realtortexttwo" width="480">$emps[?]</td></tr></table>[/red]);
  }
}

Where "$emps[?]" is whatever variable(s) that display the description part of the listing. There should be no need to loop through the directory where the thumbnail images are stored.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Chris gets 99% the credit.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Remind to never move to Anchorage, I thought Southern California was expensive!

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yes, thank you as well Chris!! Without the two of you I'm not sure what I would have done.

And yes, Anchorage is expensive, but when you get away from Anchorage it relaxes a little on the prices.

 
Hi Kevin,

If you think those prices are high, you should take a 5hr drive up the coast and see what we have up here. Prices around here are at least double.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top