Hi,
I'm using an XML feed to supplement the links on my LSQL site, and have a small problem.
When the XML feed only returns a single result, my script fails offering the following error:
Not an ARRAY reference at /home/qango/public_html/cgi-local/dir/admin/Links/User/Search.pm line 197
I need it to either, display a single link from the feed without failing (something it hasn't been able to do in the past), or TRAP a single result and bypass the XML printing so the error doesn't occur.
Here's the relevant section of the Search.pm script, I wonder if anyone can help?
# Return if no results.
unless ($link_count or $cat_count) {
# CHECK FOR SPONSORED LINKS IF NO LOCAL RESULTS FOUND
# Get 20 XML results to put in the 'Sponsored Links - Not Found' search results template
my $XML_result = '';
my $query = $IN->param('query');
$query =~ s/ /\+/g;
$query =~ s/\W/+/g;
my $page = get("http://feed.feedpatrol.com/?affid=xxx&maxcount=20&search=$query&xtype=1&xformat=xml&ip=$ENV{HTTP_REFERER}");
# if we can't get the XML feed blank out the XML results and return
if (!$page) {
return { error => Links::language('SEARCH_NOLINKS'), search_banner => $banner_html, search_button => $button_html, search_sky => $sky_html, search_text => $text_html, term => $term };
}
else {
# parse the XML feed to see if there are any results
my $ref = XMLin($page);
my $link_count = scalar($ref);
# if there are no results for this query from XML source, blank the XML results and return
if (!$link_count) {
return { error => Links::language('SEARCH_NOLINKS'), search_banner => $banner_html, search_button => $button_html, search_sky => $sky_html, search_text => $text_html, term => $term };
}
else {
# Don't process the feed if there is only one XML link found (avoids ARRAY problem caused by single link only)
if ($link_count <= 1) {
return { error => Links::language('SEARCH_NOLINKS'), search_banner => $banner_html, search_button => $button_html, search_sky => $sky_html, search_text => $text_html, term => $term };
}
# parse the XML feed into usable results for the template link_xml and link_xml_spon
my ($count, $html_links);
foreach ($count = 0; $count <= 20; $count++) {
# catch if there are no more results...
if (!$ref->{LISTING}->[$count]->{TITLE}) { last; }
# get the variables all set up...
my $Description = $ref->{LISTING}->[$count]->{DESCRIPTION};
my $Title = $ref->{LISTING}->[$count]->{TITLE};
my $Sitehost = $ref->{LISTING}->[$count]->{SITEHOST};
my $Sitelink = $ref->{LISTING}->[$count]->{LINK};
my $Sitelink = &url_decode ($Sitelink);
# put the links into HTML format...
$XML_result .= Links::SiteHTML::display('link_xml', { Description => $Description, Title => $Title, Sitehost => $Sitehost, Sitelink => $Sitelink });
}
}
}
return { xml => Links::language('SEARCH_NOLINKS'), search_banner => $banner_html, search_button => $button_html, search_sky => $sky_html, search_text => $text_html, term => $term, XML_result => $XML_result };
}
The line in red is where the error is reported from, and it appears that it fails because there isn't a second [#2] element in the array?
Any help with this much appreaciated.
All the best
Shaun
I'm using an XML feed to supplement the links on my LSQL site, and have a small problem.
When the XML feed only returns a single result, my script fails offering the following error:
Not an ARRAY reference at /home/qango/public_html/cgi-local/dir/admin/Links/User/Search.pm line 197
I need it to either, display a single link from the feed without failing (something it hasn't been able to do in the past), or TRAP a single result and bypass the XML printing so the error doesn't occur.
Here's the relevant section of the Search.pm script, I wonder if anyone can help?
Code:
# Return if no results.
unless ($link_count or $cat_count) {
# CHECK FOR SPONSORED LINKS IF NO LOCAL RESULTS FOUND
# Get 20 XML results to put in the 'Sponsored Links - Not Found' search results template
my $XML_result = '';
my $query = $IN->param('query');
$query =~ s/ /\+/g;
$query =~ s/\W/+/g;
my $page = get("http://feed.feedpatrol.com/?affid=xxx&maxcount=20&search=$query&xtype=1&xformat=xml&ip=$ENV{HTTP_REFERER}");
# if we can't get the XML feed blank out the XML results and return
if (!$page) {
return { error => Links::language('SEARCH_NOLINKS'), search_banner => $banner_html, search_button => $button_html, search_sky => $sky_html, search_text => $text_html, term => $term };
}
else {
# parse the XML feed to see if there are any results
my $ref = XMLin($page);
my $link_count = scalar($ref);
# if there are no results for this query from XML source, blank the XML results and return
if (!$link_count) {
return { error => Links::language('SEARCH_NOLINKS'), search_banner => $banner_html, search_button => $button_html, search_sky => $sky_html, search_text => $text_html, term => $term };
}
else {
# Don't process the feed if there is only one XML link found (avoids ARRAY problem caused by single link only)
if ($link_count <= 1) {
return { error => Links::language('SEARCH_NOLINKS'), search_banner => $banner_html, search_button => $button_html, search_sky => $sky_html, search_text => $text_html, term => $term };
}
# parse the XML feed into usable results for the template link_xml and link_xml_spon
my ($count, $html_links);
foreach ($count = 0; $count <= 20; $count++) {
# catch if there are no more results...
if (!$ref->{LISTING}->[$count]->{TITLE}) { last; }
# get the variables all set up...
my $Description = $ref->{LISTING}->[$count]->{DESCRIPTION};
my $Title = $ref->{LISTING}->[$count]->{TITLE};
my $Sitehost = $ref->{LISTING}->[$count]->{SITEHOST};
my $Sitelink = $ref->{LISTING}->[$count]->{LINK};
my $Sitelink = &url_decode ($Sitelink);
# put the links into HTML format...
$XML_result .= Links::SiteHTML::display('link_xml', { Description => $Description, Title => $Title, Sitehost => $Sitehost, Sitelink => $Sitelink });
}
}
}
return { xml => Links::language('SEARCH_NOLINKS'), search_banner => $banner_html, search_button => $button_html, search_sky => $sky_html, search_text => $text_html, term => $term, XML_result => $XML_result };
}
The line in red is where the error is reported from, and it appears that it fails because there isn't a second [#2] element in the array?
Any help with this much appreaciated.
All the best
Shaun