Click to See Complete Forum and Search --> : Looping Output Help


HDC
06-25-2006, 10:58 AM
I've got a snippet of perl code that I can't figure out. I don't know much about this language so be patient. Basically I have a huge perl script and this snippet looks at a flat text file and then output 5 of the latest database entries on a page that I choose.

The problem is that I have a link and graphics hard coded into this snippet and when I don't have 5 entries in the database it still outputs 5 times with the links and graphics. I do have it setup that if no records are found at all it does give me an output of "... has no Classified Listings" which is fine.

So basically if I only have 1 record I will get a bullet list of 5 links with graphic.

Is it possible when it loops and outputs on my page that it doesn't show a bullet with these links and graphic if a record is not present?


Thanks for the help!


my $results = " ";
my $open = 1;
#open file, extract data
open(dat,"<${root}data/classifieds/$input{'login'}.dat") or $open = 0;
my @dlines = <dat>;
close dat;
if ($open)
{
my $dlines_length = @dlines;
my $i = 0;
my @total;
for (my $i=0; $dlines_length > $i; $i++)
{

my @entry = split(/\|/, @dlines[$i]);
my $part = qq($entry[1]\|$entry[0]\|$entry[2]\|$entry[3]\|$entry[4]);
push(@total, $part);

}
#sort by date
@total = sort @total;
@total = reverse @total;

for (my $j=0; 5 > $j; $j++)
{

my @listing = split(/\|/, $total[$j]);
$results .= qq(<li><a href=pm.cgi?action=app_display&app=classifieds&app_id=$listing[1]&profile_name=$values{'profile_name'}> $listing[2] - $listing[3] <img src=present.gif border=0></a></li>\n);
}
$values{'classifieds'} = $results;
}else{

$values['classifieds'] = '<li>Your events file could not be opened.</li>';
}
if($values{'classifieds'} eq "")
{
$values{'classifieds'} = qq(<li>$values{'profile_name'} has no Classified Listings.</li>\n);
}

CyCo
06-25-2006, 11:48 AM
How about this?my @listing = split(/\|/, $total[$j]) or last;

HDC
06-25-2006, 01:03 PM
Excellent CyCo, works great! That was easy.

1 more question for you. Not sure what is going on but the links that are created from this snippet do not have a %20 in the url link created by the browser when a link has a space in it. This causes the url to be cut short and then go to a non-exsisting page.

Some of my links have spaces in the profile name. I'm not sure if its the script or not but some of the other scripts I have work just fine in this way.


Here is an example of what I see in the url:


Currently... Bad:
/cgi/members/pm.cgi?action=app_display&app=event&app_id=7&profile_name=AMD

Good:
/cgi/members/pm.cgi?action=app_display&app=event&app_id=7&profile_name=AMD%20Band



Thanks!

CyCo
06-25-2006, 02:28 PM
It seems you need a pair of quotation marks.$results .= qq(<li><a href="pm.cgi?action=app_display&app=classifieds&app_id=$listing[1]&profile_name=$values{'profile_name'}"> $listing[2] - $listing[3] <img src=present.gif border=0></a></li>\n);

HDC
06-25-2006, 02:43 PM
CyCo, that was it! Thanks a million, I owe ya one!

CyCo
06-26-2006, 03:59 PM
;)

HDC
06-29-2006, 09:28 AM
Just noticed this.... It seems the output (... has no Classified Listings) only appears if the file or folder (data/classifieds/$input{'login'}.dat) has not been created yet. After this file or folder has been created if there are still no entries in it then I don't get this message, my output is just blank.

Ultimately I would like to have it give me the "no Listing" output when there is no file or folder(currently) and if the file/folder exsists but has no entries inside it. The file is just a pipe delimited text file.

Is this possible?

HDC
06-30-2006, 05:38 PM
Anybody able to help on this one. Not sure if this is possible anyway.

Thanks!

CyCo
07-02-2006, 10:32 AM
Of course it's possible. But, after simulating this output, I can't seem to re-create the scenario you describe.

HDC
07-08-2006, 10:29 AM
CyCo/Anyone,

After looking into this further, I have 3 different snippets of the same code like below on top of each other in the cgi script and the values are different for each of the snippets like this ['classifieds'], ['entries'], and ['calendar']
and the output works fine when I have entries but just not the "Has No Listing" message when I have none.

I found that only the last one shows up correctly when I have no listings.

Any ideas on this?



my $results = " ";
my $open = 1;
#open file, extract data
open(dat,"<${root}data/classifieds/$input{'login'}.dat") or $open = 0;
my @dlines = <dat>;
close dat;
if ($open)
{
my $dlines_length = @dlines;
my $i = 0;
my @total;
for (my $i=0; $dlines_length > $i; $i++)
{

my @entry = split(/\|/, @dlines[$i]);
my $part = qq($entry[1]\|$entry[0]\|$entry[2]\|$entry[3]\|$entry[4]);
push(@total, $part);

}
#sort by date
@total = sort @total;
@total = reverse @total;

for (my $j=0; 5 > $j; $j++)
{

my @listing = split(/\|/, $total[$j]);
$results .= qq(<li><a href=pm.cgi?action=app_display&app=classifieds&app_id=$listing[1]&profile_name=$values{'profile_name'}> $listing[2] - $listing[3] <img src=present.gif border=0></a></li>\n);
}
$values{'classifieds'} = $results;
}else{

$values['classifieds'] = '<li>Your events file could not be opened.</li>';
}
if($values{'classifieds'} eq "")
{
$values{'classifieds'} = qq(<li>$values{'profile_name'} has no Classified Listings.</li>\n);
}

HDC
07-14-2006, 10:12 AM
After much trial and error and reading I have solved half my problem which if the folder and file doesn't exsist I get the "No.... Posted" message.

Here is the remaining problem that I just tested, if a member posts only 1 listing and then deletes it from the file the "No... Posted" doesn't show and the page is just blank.

Basically it's like the script is just recognizing if the folder has been created so it doesn't write the message.

Any ideas?



Here is the code I switched:



if($values{'classifieds'} eq "")
{
$values{'classifieds'} = qq(<li>No Classified Listings Posted</li>\n);

}

to this:


if(($values{'classifieds'} eq "") or(!$open)) {
$values{'classifieds'} = qq(<li>No Classified Listings Posted</li>\n);

}

Nedals
07-14-2006, 11:45 PM
Here's a slight simplification of your code and should do what you want.

my @total = ();
$values{'classifieds'} = '';
open(dat,"<${root}data/classifieds/$input{'login'}.dat") || $values{'classifieds'} = '<li>Your events file could not be opened.</li>';

if ($values{'classifieds'} eq '') {
while (<dat>) {
my @entry = split(/\|/, $_);
my $part = qq($entry[1]\|$entry[0]\|$entry[2]\|$entry[3]\|$entry[4]);
push(@total, $part);
}
close dat;

if ($entry[0]) { ## check for any data
@total = reverse sort @total;
for (my $j=0; 5 > $j; $j++) { ## strange syntax! usually $j<6
my @listing = split(/\|/, $total[$j]);
$values{'classifieds'} .= qq(<li><a href=pm.cgi?action=app_display&app=classifieds&app_id=$listing[1]&profile_name=$values{'profile_name'}> $listing[2] - $listing[3] <img src=present.gif border=0></a></li>\n);
}

} else {
$values{'classifieds'} = "<li>$values{'profile_name'} has no Classified Listings.</li>\n";
}
}

Please use code tags

HDC
07-16-2006, 12:48 PM
Thanks Nedals for the help but when I replaced my code with yours I get a 500 internal server error. Not sure how to troubleshoot your code...

HDC
07-18-2006, 03:52 PM
Thanks eveyone for the help but I have figured it out! A very simple space is all I needed.


From this:

if(($values{'classifieds'} eq "") or(!$open)) {


To This:

if(($values{'classifieds'} eq " ") or(!$open)) {


Thanks!