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

Searching a personal database?

Status
Not open for further replies.

Xerxes333

Technical User
Mar 11, 2001
4
US
Im creating a web page for a fun and Ive never created my own script. I have C++, C, java, Visual C++ background but I dont know what i need to know to write a cgi script. Basically what i want to do is create a database of items and give the option to someone viewing my web page to search for them I have all the files created for the database and saved as .txt files. For example if they want to search for a word like aardvark, the program would search for the A.txt file then open in and search through it until it found the given word, then return the information that comes with it (like the defenition and so forth) If anyone could help or point me in the direction of some good tutorials i would greatly appreciate it.



Jeremy Draxler
Try? Try not. Do or do not, there is no try. -Yoda-
dubiedubiedoo@hotmail.com Jeremy Draxler
"do or do not, there is no try" -Yoda-
 
If you want to play with some Perl, I can help. If you can write C, you could pick up Perl quickly. There is a FAQ tab in this forum that might be worth looking at.

HTH


keep the rudder amid ship and beware the odd typo
 
I would love to learn some perl, lately i have been looking into perl programming and it seems to follow pretty much the same pattern as C, so I dont think it would be to hard for me to "Convert" and thanx for the FAQ sggestion ill be sure to check it out. Jeremy Draxler
"do or do not, there is no try" -Yoda-
 
I've done C and perl both, and I'd rather write perl any day!!! perl is easier to work with, is more flexible, has better string handling capability, has better regular expression capability, doesn't force you into strong typing, doesn't force you to constantly deal with pointers, and about a zillion other advantages. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I know this if off topic, but Tracy, you wrote:

"...has better regular expression capability" in reference to the Perl better than C argument. I am just wondering, does C have any regular expression ability? I know that you have to deal with pointers or array's for strings, and you can edit them by each index, but I was not aware of any RegEx ability in C.

Thanks,
-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
I think there are some regex library functions for C, but I wouldn't bet my next paycheck on it. That's one of the things I don't like about C. It takes very little time to learn the language, but much longer to learn all the library functions it takes to do anything useful with it. And most of them require passing pointers around. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Yup, there are very few languages in which the syntax is hard to learn. Thats my biggest gripe with languages like PHP, there are over 2000 built in functions. No one can ever memorize all of those.

Thats the beaty of Perl, it has just enough built in functions to get the job done well, and leaves difficult stuff to all of the modules out there.

Thanks for the input though.
-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
Even with perl I keep a printed copy of the perlfunc man page on my desk for reference, but I don't have to refer to it very often. You can do most things in perl with a relatively small set of functions.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Here' s a sloppily slapped together start. It only uses one data file, but, maybe it will give you something to chew on.

Given a data file that looks like,
german shepherd
chihuahua
monkey
red fish
greyhound
mutt
kangaroo
mouse
parrot

and is named, cgi_cb.txt.

and the following code,

Code:
#!/usr/local/bin/perl 
use CGI;
use strict;
my $page = new CGI;
my $self_url = $page->self_url;
my $searchTerm = $page->param('searchTerm');
my @results;
my $record;
my $hits;
print $page->header,
    $page->start_html(-title=>'text db stuff'),
    '<center>';
# if no search term, generate input page
if (!($searchTerm))
    {
    print '<a name=&quot;TOP&quot;></a><center>',
        $page->h3('Search a simple text db'), '<hr>',
        $page->start_form(-method=>'POST', -action=>&quot;$self_url&quot;),
        &quot;Search Term or Phrase: &quot;,'<BR>',
        $page->textfield(-name=>'searchTerm',-size=>'25',
        -maxlength=>'80'),'<BR>',$page->submit(-name=>'Submit',
        -value=>'search'),$page->endform,'<hr>';
    }
# if exists(searchTerm), perform query
if ($searchTerm)
    {
    print &quot;<h4 align='center'>Search Results</h4><hr>\n&quot;;
    open(DATA,&quot;<cgi_db.txt&quot;) or ooooops(&quot;Failed to open cgi_db.txt, $!&quot;);
    while ($record = <DATA>)
        {
        if ($record =~ /\b$searchTerm\b/i) { push @results, $record; }
        }
    close DATA;
    foreach  my $record (@results) 
        {
        $hits++;
        print &quot;$record<br>\n&quot;;
        }
    unless ($hits) { print &quot;Sorry, no hits for $searchTerm.\n<br>\n&quot;; }
    }
print &quot;</center>&quot;,$page->end_html;

That will present an input page if a search term does not exist and
if a search term does exists it opens, it opens/read/searches the data file
for results and print them to the browser.

HTH


keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top