Displaying 12 images from dir with option to view next 12 etc

S

sly

Help

I am writing a script that will read out the contents of an image
directory (that has .jpg images) and printing them out with links to
view the next 12, but I am always getting the last 2 images of the
first view in the next. (last 2 images are the same as the first 2)


splice(@images, 0, $dis);

mime();

print qq (<table width="650" border="0" align="center" cellpadding="0"
cellspacing="0"><tr><td><img src="http://intranet/snaps/i/title.gif"
/></td></tr><tr><td>\n);

$count = $count; # sets count value to count!

foreach $row(@images) {

my @images = split(/\s/, $row);

if (@images) {

# sort {$b cmp $a}
foreach (sort @images) { # sorts @images array

if (/.jpg$/){ # only prints if ends with .jpg

print qq (<span class="polar_bg">);
print qq (<img src="/snaps/mms/@images" height="126" border="0"
class="mms_i"/>\n);

print qq (<form method="post" name="tiaf"
action="http://intranet/cgi-bin/snaps_store.pl">);

print qq (<input type="hidden" name="IMGid" value="@images">\n);

print qq (<input type="hidden" name="vote" value="1">\n);

print qq (<input type="submit" name="submit" id="bsubmit" value="vote
for this image">);

print qq (</form>);

print qq (</span>);

} # ends filter .jpg

} # ends foreach sort

} # ends if @images

++$count; # increment count each time the foreach is run through

if ($count > (1 + $dis)) { # if count top 6 images print the below and
exit

print qq (</td></tr></table>\n);

NEXTLinks();

exit; } # ends if count

} # ends foreach split

print qq (</td></tr></table>\n);

NEXTLinks();


sub NEXTLinks {

print qq (<table width="650" align="center"><tr><td>\n);

#print qq (<a
href="javascript:history.back(-1);">back</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);
print qq (<a
href="http://intranet/cgi-bin/snaps.pl?state=read">0</a>&nbsp;&nbsp;\n);
print qq (<a
href="http://intranet/cgi-bin/snaps.pl?state=next&dis=10">1</a>&nbsp;&nbsp;\n);
print qq (<a
href="http://intranet/cgi-bin/snaps.pl?state=next&dis=20">2</a>&nbsp;&nbsp;\n);

print qq (</td></tr></table>\n);
}

At the moment I am manually writting the links, does any one have a
better way of doing this or some advice ?

Many thanks
 
A

A. Sinan Unur

$count = $count; # sets count value to count!

Huh?

The code you posted is neither runnable nor readable.

Please consult the posting guidelines for this group for invaluable
information on how to help yourself and help others help you.

Sinan
 
B

Brian Wakem

sly said:
Help

I am writing a script that will read out the contents of an image
directory (that has .jpg images) and printing them out with links to
view the next 12, but I am always getting the last 2 images of the
first view in the next. (last 2 images are the same as the first 2)
href="http://intranet/cgi-bin/snaps.pl?state=read">0</a>&nbsp;&nbsp;\n);
print qq (<a
href="http://intranet/cgi-bin/snaps.pl?state=next&dis=10">1 said:
print qq (<a
href="http://intranet/cgi-bin/snaps.pl?state=next&dis=20">2</a>&nbsp;&nbsp
\n);


I can't decipher most of that, but the dis=10 and dis=20 look like offsets
in multiples of 10 to me, and as each page has 12 images on then of course
page2 will start with the last two images of page1.
 
P

Paul Lalli

sly said:

Help us help you. Please read the Posting Guidelines for this group.
I am writing a script that will read out the contents of an image
directory (that has .jpg images) and printing them out with links to
view the next 12, but I am always getting the last 2 images of the
first view in the next. (last 2 images are the same as the first 2)

This description of the output is difficult to follow. Please provide
sample input, desired output, and actual obtained output, so that we
can easily see what results you're getting that you don't want.
splice(@images, 0, $dis);

What is $dis? Where did it come from?

What is mime()? Where was it declared? What is it doing?

Please reduce your problem to a SMALL but COMPLETE script that we can
run by copy and pasting.
print qq (<table width="650" border="0" align="center" cellpadding="0"
cellspacing="0"><tr><td><img src="http://intranet/snaps/i/title.gif"
/></td></tr><tr><td>\n);

$count = $count; # sets count value to count!

This is the single most absurd line of code + comment I've ever seen.
What do you believe this is doing?
foreach $row(@images) {

Either you declared $row not in the shortest scope possible (A Bad
Idea), or you're not using strict (an Inexcusably Bad Idea).
my @images = split(/\s/, $row);

Gahh. So now we have to keep track of two different scopes of the
variable @images, made more difficult by your lack of consistent
indentation.
if (@images) {

# sort {$b cmp $a}

Please do not include commented-out code.
foreach (sort @images) { # sorts @images array

if (/.jpg$/){ # only prints if ends with .jpg

No, only prints if ends with any character followed by 'jpg', including
a file named, for example "thisisnotajpg".
print qq (<span class="polar_bg">);
print qq (<img src="/snaps/mms/@images" height="126" border="0"
class="mms_i"/>\n);

print qq (<form method="post" name="tiaf"
action="http://intranet/cgi-bin/snaps_store.pl">);

print qq (<input type="hidden" name="IMGid" value="@images">\n);

print qq (<input type="hidden" name="vote" value="1">\n);

print qq (<input type="submit" name="submit" id="bsubmit" value="vote
for this image">);

print qq (</form>);

print qq (</span>);

} # ends filter .jpg

} # ends foreach sort

} # ends if @images

It'd be a heck of a lot easier to see what } ends what { if you would
properly indent your code. (Please don't respond "My newsreader strips
the indentations." If that's true, use a better newsreader. Hell, even
Google Groups eventually wisened up to that one).
++$count; # increment count each time the foreach is run through

if ($count > (1 + $dis)) { # if count top 6 images print the below and exit

print qq (</td></tr></table>\n);

NEXTLinks();

exit; } # ends if count

} # ends foreach split

print qq (</td></tr></table>\n);

NEXTLinks();


sub NEXTLinks {
< snip a bunch of print qq{} statements
}

At the moment I am manually writting the links, does any one have a
better way of doing this or some advice ?

Yes, shorten your code, make it into something we can run by copy and
pasting, enable strict and warnings, and then try again. Your code is
not legible enough for me to discern what you're trying to do, what you
are doing, or anything in between.

Paul Lalli
 
S

sly

Thanks, taking a step back on this as I am sure my approach wasn't
right.

Have read the guidelines now as well!!

I can print out all the images from my directory but I don't want a big
long list, I want to display 12 on each page with a link to the next
12. Can anyone point me in the right direction ? My logic before was
to pass a value through the $dis variable and then remove these via a
splice.

use strict;
use CGI ':standard';

my ($row);
my $state = param('state');
my $dis = param('dis');


my $dir_location = "/intranet/content/snaps/mms/";

opendir(IMGS,"$dir_location") || Error ('open', 'directory');
my @images = readdir (IMGS);
closedir (IMGS);

@images = reverse (@images);


if ($state eq "read") {

mime();

print qq (<table width="650" border="0" align="center" cellpadding="0"
cellspacing="0"><tr><td><img src="http://intranet/snaps/i/title.gif"
/></td></tr><tr><td>\n);

foreach $row(@images) {

my @images = split(/\s/, $row);

if (@images) {

foreach (@images) {

if (/.jpg$/){

print qq (<span class="polar_bg">);
print qq (<img src="/snaps/mms/@images" height="126" border="0"
class="mms_i"/>\n);

print qq (<form method="post" name="tiaf"
action="/cgi-bin/snaps_store.pl">);

print qq (<input type="hidden" name="IMGid" value="@images">\n);

print qq (<input type="hidden" name="vote" value="1">\n);

print qq (<input type="submit" name="submit" id="bsubmit" value="vote
for this image">);

print qq (</form>);

print qq (</span>);


}
}
}
}


print qq (</td></tr></table>\n);


exit;
}

else { mime(); print qq (State not set); exit; }

sub mime {

print "Content-type: text/html\n\n";
print qq (<link href="/snaps/snaps_style.css" rel="stylesheet"
type="text/css">\n);

}

sub Error {
print "Content-type: text/shtml\n\n";
print "there was an error opening the $dir_location directory\n\n";
exit;
}

Thanks for the helping
 
P

Paul Lalli

sly said:
Thanks, taking a step back on this as I am sure my approach wasn't
right.

Have read the guidelines now as well!!

.... but you've decided not to follow them?
You haven't quoted any context in your reply.
You haven't enabled warnings in your code.
You haven't posted code with any amount of indentation.
You haven't asked Perl for debugging help, by including the $! in the
failure message of system calls.

Reading the Posting Guidelines is pointless if you don't actually do
what they ask.

Paul Lalli
 
M

Matt Garrish

Paul Lalli said:
This is the single most absurd line of code + comment I've ever seen.
What do you believe this is doing?

I don't think it could be any clearer that it's setting the value of $count
back to the value of $count. This ensures that the value of $count is always
the value of $count, regardless of whether the value of $count has been
changed to some other value of $count... : )

Matt
 
S

sly

I thought I was!! this isn't something I am used to so apologies, but
thanks for the tips.

I am looking to find away to display so many images on one page and
provide links to view the rest. The above script works in that it
prints the whole conetents I just need pointing in the right direction.
I use -w but if I use, use warnings: the script won't run.
Sorry I tried to make it more compact and readable, obviously didn't
help.
This isn't something I have done before, will look into how it works
and what I need to do.

Thanks
Sly
 
J

Jürgen Exner

sly said:
I thought I was!!

I hope you are. Would be very sad if you were.
And it would also be the first confirmed case of someone from the great
beyond to make contact with this world.
this isn't something I am used to so apologies, but
thanks for the tips.


I am looking to find away to display so many images on one page and
provide links to view the rest. The above script

There is no script "above". Above this posting (for me) there is a another
posting titled "Can PHP programmers pick up Perl quickly". That doesn't have
a script, either (and probably wouldn't be related to whatever you are
trying to tell people anyway).
Usenet is a distributed and asynchronous communication medium. Because of
the way it works you can never assume that
- articles arrive in any deterministic sequence
- a particular article is accessible to a reader at a given time
- a particular article has ever been accessible to a reader
- a particular article will ever be accessible to a reader at all
Therefore it has been a proven practise to quote enough context from the
preceeding article such that the new article is understandable even is
someone hasn't read the preceeding article.
I use -w but if I use, use warnings: the script won't run.

That is a strong indication that somethink is wrong in the code. Disabling
warnings is like removing the light bulb of the "motor oil" warning light
when it comes on instead of finding and fixing the real problem. Yes, there
are rare cases where the warning (both, "motor oil" and Perl's "use
warnings") are bogus. But then you turn them off individually only after you
are absolutely certain what the real problem is.

jue
 
P

Paul Lalli

Jürgen Exner said:
That is a strong indication that somethink is wrong in the code. Disabling
warnings is like removing the light bulb of the "motor oil" warning light
when it comes on instead of finding and fixing the real problem. Yes, there
are rare cases where the warning (both, "motor oil" and Perl's "use
warnings") are bogus. But then you turn them off individually only after you
are absolutely certain what the real problem is.

In this case, I think it's probably more likely that the OP is using a
version of Perl prior to 5.6.0, which did not have warnings.pm.
Therefore, specifying -w on the shebang (which the OP didn't show in
his original post) would "work", but using the `use warnings;` line
would cause a fatal error.

Of couse, that is only a complete guess, as the OP didn't bother
mentioning *how* the script "won't run".

Paul Lalli
 
S

sly

Thanks for the help on the warnings.. think your right our unix box is
running an old version.. am using -w now.

The second paste of code runs fine, it displays all the images in the
directory but I only want to display 12 at a time with links to view
the rest. Any advice would be great.

Sly
 
P

Paul Lalli

sly said:
Thanks for the help on the warnings.. think your right our unix box is
running an old version.. am using -w now.

The second paste of code runs fine, it displays all the images in the
directory but I only want to display 12 at a time with links to view
the rest. Any advice would be great.

I have given you a multitude of advice, which you continue to ignore.
Explain to me why I should be helping you?

You *STILL* have not:
quoted any context in your replies
posted code that has a consistent indentation scheme, so we can read
your code
shown that you are using $! in your error outputs.

In the most general case, (which has absolutely nothing to do with
Perl, of course, and everything to do with general algorithm design):
Read a list of whatever you want to print
determine where to start your list (based on a query parameter?)
determine how many list items you want to output (probably hardcoded)
loop from the start position to the start position + limit
print one list item per iteration.


[thoroughly untested]
my @items = readdir $dh;
my $start = param('start') || 0;
$start ||=0 if $start < 0;
my $limit = 12;
if ($start > 0){
print qq{<a href="index.cgi?start=},
$start - $limit,
qq{">Previous</a>\n};
}
foreach my $item (@items[$start..$start+$limit-1]){
print $item;
}
if ($start + $limit < @items){
print qq{<a href="index.cgi?start=},
$start + $limit,
qq{">Next</a>\n};
}

Now, I ask that you *please* go read the posting guidelines, and start
following all the advice contained therein.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top