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

Separate arrays and insert in html placeholders 2

Status
Not open for further replies.

artisst

Programmer
Apr 24, 2004
18
US
I have read the Q&A section pertaining to arrays, however this example is slightly beyond the scope of the topics discussed.

I need to search 2 flat text files and return the search results in 2 separate columns. 1 column for international, and the other local listings. This search needs to open both files, read them, and return results. The HTML template I am using pulls search_line into a single column via %%searchresults%% . I have added a second include %%premiumlistings%% column adjacent to it. I know I need to assign each file to an array and then use the split / / function, but I seem to run into a problem using the while statement. The script is as follow:

Earlier in script:

<CODE>

$main_template =~ s/%%keywords%%/$fields{'keywords'}/g;
$main_template =~ s/%%searcresults%%/$pitem/g;

</CODE>

returns results in left column via %%searchresults%%

<CODE>

$main_template =~ s/%%keywords%%/$fields{'keywords'}/g;
$main_template =~ s/%%premiumlistings%%/$premiumitem/g;

</CODE>

returns SAME results in right column via %%premiumresults%%


This is where the problem starts:

<CODE>

sub normal_search
{

### PERFORM SEARCH

$search_line = $fields{'keywords'};
$icnt = 0;
$toadk = "";

(@skeyw) = split(/ /,$search_line);
$nrkeywords = 0;
foreach $item (@skeyw){$nrkeywords++;}

open (SIDX, "$data_dir/search.idx");

#################
### added below to open 2nd file ###
#################

open (SIDX2, "$data_dir/search2.idx");

###

if ($file_locking ne "No"){flock (SIDX, LOCK_SH) or die "Can't set lock for file: $data_dir/search.idx, $data_dir/search2.idx $!\n";}

## here's 1st problem (only opening and reading 1st file ##
while (defined($line=<SIDX>))
{
$sline = $line;

foreach $kwr (@skeyw)
{

if (($sline =~ /$kwr/i) and ($kwr ne ""))
{
$toadk = "true";
}

}

if ($toadk eq "true")
{
$resultline[$icnt] = $line;
$toadk = false;
$icnt++;
}

}
#if ($file_locking ne "No"){flock (CIT, LOCK_UN);}
close (SIDX);

}

## I need this to open both files and return the results of search.idx in the right column and search2.idx in the right.

## this is the latter area of importance:

###############

$resultline[$rcc] = $row[0] . "\t" .
$row[1] . "\t" .
$row[2] . "\t" .
$row[3] . "\t" .
$row[4] . "\t" .
$row[5] . "\t" .
$row[6] . "\t" .
$row[7] . "\t" .
$row[8] . "\t" .
$kinclude . "\n";


$rcc++;
}

$sth->finish;
$dbh->disconnect;

}

## and finally:

sub get_search_ready
{
my ($search_line) = @_;

$reline = $search_line;

$reline =~ s/\+//g;
$reline =~ s/\[//g;
$reline =~ s/\]//g;
$reline =~ s/\)//g;
$reline =~ s/\(//g;
$reline =~ s/\*//g;
$reline =~ s/\^//g;
#$reline =~ s/\.//g;
$reline =~ s/\$//g;
$reline =~ s/\?//g;
$reline =~ s/\\//g;
$reline =~ s/\~//g;
$reline =~ s/<//g;
$reline =~ s/>//g;
$reline =~ s/;//g;

return ($reline);
}

</CODE>
 
"I seem to run into a problem using the while statement." - and that problem is?

would help me to see (brief) samples from m the 2 input and desired output
 
I am using HTML::Template and Perl DBD::DBI Modules from CPAN. The data is text file which is tab delimited and the script is written totally in perl. Data files are created and stored in search.idx. I already have a solution to store user input data into search2.idx.

Sample Source:

Code:
20645	CJ Sports	Sportswear and equipment retailer specialising in rugby, hockey, netball and cricket.  Full product details and ordering available online.				Apparel_and_Accessories-Athletic_Clothing	[URL unfurl="true"]www.cj-sports.co.uk/[/URL]		
20660	Gekko Gear	Selection of winter sports apparel.				Apparel_and_Accessories-Athletic_Clothing	[URL unfurl="true"]www.gekkogear.com/[/URL]		
20661	GH Sports	Retailer of running, cycling, fitness, hiking, triathlon, and equestrian gear.				Apparel_and_Accessories-Athletic_Clothing	[URL unfurl="true"]www.ghsports.com/[/URL]		
20662	Girl Sports Brand	Offering sports apparel, discount golf apparel, outwear, sports bras, poloshirts, tank tops, soccer apparel, t-shirts, logo tees.				Apparel_and_Accessories-Athletic_Clothing	[URL unfurl="true"]www.girlsportsbrand.com/[/URL]

HTML Template:

Code:
<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<meta name="generator" content="Adobe GoLive 4">
		<title>Search</title>
		<!-- Hide %%metadescription%% // End Hide -->
	</head>

	<body bgcolor="white">
		<div align="left">
			%%incl_mallheader%%
			<table border="0" cellspacing="0" cellpadding="1" width="805" height="174">
				<tr align="left">
					<td width="23" valign="top"></td>
					<td valign="top" width="778">
						<div align="left">
							<table border="0" cellpadding="5" cellspacing="1" width="795">
								<tr>
									<td width="556" valign="top"><font face="Verdana" size="2"><strong>Search Results</strong></font><font face="Verdana" size="2" color="#990000"><strong> </strong></font><font face="Verdana" size="2">found </font><font face="Verdana" size="2" color="#990000"><strong>%%link_matches%%</strong></font><font face="Verdana" size="2"> matches searching for <strong>'</strong></font><font face="Verdana" size="2" color="#990000"><strong>%%keywords%%</strong></font><font face="Verdana" size="2"><strong>'</strong></font><font face="Verdana" size="2"><br>
										</font></td>
									<td valign="top" width="216" rowspan="4"><font face="Verdana" size="2">%%premiumlistings%%<br>
										</font>
										<p><font face="Verdana" size="2"><br>
										</font></td>
								</tr>
								<tr height="34">
									<td width="556" height="34" valign="top"><font face="Verdana" size="2"><br>
										%%searchresults%%<br>
										&nbsp;&nbsp;&nbsp;&nbsp; </font></td>
								</tr>
								<tr>
									<td width="556" valign="top">
										<center>
											<font face="Verdana" size="1">%%pages%%</font></center>
									</td>
								</tr>
								<tr>
									<td width="556" valign="top"></td>
								</tr>
							</table>
						</div>
					</td>
				</tr>
			</table>
			<table border="0" cellpadding="0" cellspacing="0" background="[URL unfurl="true"]http://www.mdosn.com/mall/images/bar-bk.gif"[/URL] width="741">
				<tr height="29" bgcolor="#e5ecf9">
					<td height="29" width="177"></td>
					<td height="29">
						<form method="POST" action="[URL unfurl="true"]http://www.mdosn.com/cgi-bin/directory/tsearch.cgi"[/URL] enctype="x-[URL unfurl="true"]www-form-urlencoded">[/URL]
					</td>
					<td height="29"></td>
					<td height="29"><input type="hidden" name="livebuilt" value="live"></td>
					<td height="29" width="138"><input type="text" name="keywords" size="21"></td>
					<td height="29" width="52"><font color="#003366" size="1"><span class="lefttext"><input type="submit" value=" Search " name="sbutton"></span></font></td>
					<td height="29">
						</form>
					</td>
					<td height="29"><font color="#ff9900">
						<form method="get" action="[URL unfurl="true"]http://www.mdosn.com/cgi-bin/directory/malljump.cgi"[/URL] onsubmit="location.href=jb.options[jb.selectedIndex].value; return false;">
						</font></td>
					<td height="29"></td>
					cellspacing="0">
				<tr>
					<td bgcolor="#3366cc" width="743"><img width="1" height="1" src="(Empty Reference!)"></td>
				</tr>
			</table>
			<br>
			<table border="0" cellspacing="0" width="804" cellpadding="0">
				<tr height="16">
					<td width="804" valign="top" bgcolor="white" height="16">
						<center>
							<font face="Verdana" size="1">%%incl_mallfooter%%</font></center>
					</td>
				</tr>
			</table>
			<p>&nbsp;</div>
	</body>

</html>

Required is as follows

Code:
use Fcntl qw(:DEFAULT :flock);
use CGI;
use CGI::Carp qw(fatalsToBrowser);

The script is as follow:

Code:
	$main_template =~ s/%%keywords%%/$fields{'keywords'}/g;
	$main_template =~ s/%%searcresults%%/$pitem/g;
returns results in left column via %%searchresults%%

Code:
	$main_template =~ s/%%keywords%%/$fields{'keywords'}/g;
	$main_template =~ s/%%premiumlistings%%/$premiumitem/g;
unfortunatly returns same results in right column via %%premiumresults%%.


This is where the problem starts:
Code:
sub normal_search
{

### PERFORM SEARCH

$search_line = $fields{'keywords'};
$icnt = 0;
$toadk = "";

(@skeyw) = split(/ /,$search_line);
$nrkeywords = 0;
foreach $item (@skeyw){$nrkeywords++;}

open (SIDX, "$data_dir/search.idx");


added code below to open 2nd file


open (SIDX2, "$data_dir/search2.idx");



 if ($file_locking ne "No"){flock (SIDX, LOCK_SH) or die "Can't set 
lock for file: $data_dir/search.idx, $data_dir/search2.idx $!\n";}
here's 1st problem (only opening and reading 1st file
Code:
	while (defined($line=<SIDX>))
	{
	$sline = $line;
	
	foreach $kwr (@skeyw)
		{

		if (($sline =~ /$kwr/i) and ($kwr ne ""))
			{
			$toadk = "true";
			}
			
		}

		if ($toadk eq "true")
			{
			$resultline[$icnt] = $line;
			$toadk = false;
			$icnt++;
			}

	}
 #if ($file_locking ne "No"){flock (CIT, LOCK_UN);}
close (SIDX);

}

I need this to open both files and return the results of search.idx in the right column and search2.idx in the right. This is the latter area of importance:



Code:
	$resultline[$rcc] = 					$row[0] . "\t" .
								$row[1] . "\t" .
								$row[2] . "\t" .
								$row[3] . "\t" .
								$row[4] . "\t" .
								$row[5] . "\t" .
								$row[6] . "\t" . 
								$row[7] . "\t" . 
								$row[8] . "\t" . 
								$kinclude . "\n";
								
					
	$rcc++;
	}

$sth->finish;
$dbh->disconnect;

}
and finally:
Code:
sub get_search_ready
{
my ($search_line) = @_;

$reline = $search_line;

$reline =~ s/\+//g;
$reline =~ s/\[//g;
$reline =~ s/\]//g;
$reline =~ s/\)//g;
$reline =~ s/\(//g;
$reline =~ s/\*//g;
$reline =~ s/\^//g;
#$reline =~ s/\.//g;
$reline =~ s/\$//g;
$reline =~ s/\?//g;
$reline =~ s/\\//g;
$reline =~ s/\~//g;
$reline =~ s/<//g;
$reline =~ s/>//g;
$reline =~ s/;//g;

return ($reline);
}

[/CODE]
 
hi artisst

this will do the same as your last block of code:-

Code:
$reline =~ s/[+\[\]()*^.\$?\\~<>;]//g;


Kind Regards
Duncan
 
I'm trying to create a site where the search results from one file go into 1 column and the results from a 2nd file into another column like Google.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top