#!/usr/bin/perl -w # recipe.pl #**************************************************************** # This perl script is a html interface to a SQL cookbook database. # As it is written it will connect to a MySQL database but it # should work with most other sql servers with slight changes, # albeit it is untested. # This perl script was designed to run using mod perl in apache2 # It has also been tested and is known to work as a standard CGI script. #***************************************************************** # recipe.pl was written by # Joe Kamphaus # Boise, Idaho USA # Copyright 10-1-2006 use DBI; use CGI; use strict; ##################################################### sub start () { # Initialize variables my %var = (); $var{'path'} = "recipe.pl"; # May neet to edit with filename $var{'q'} = new CGI; $var{'category'} = $var{'q'}->param('category'); $var{'searchStr'} = $var{'q'}->param('SearchStr'); $var{'recipe'} = $var{'q'}->param('recipe'); $var{'newrecipe'} = $var{'q'}->param('new'); $var{'edit'} = $var{'q'}->param('edit'); $var{'create'} = $var{'q'}->param('create'); $var{'update'} = $var{'q'}->param('update'); $var{'instr'} = $var{'q'}->param('instructions'); $var{'title'} = $var{'q'}->param('title'); $var{'instr'} =~ s/\r//g; $var{'instr'} =~ s/\n/
/g; #connect to database $var{dbh} = DBI->connect('DBI:mysql:cookbook', 'root', '' ) || print "Could not connect to database: $DBI::errstr"; #get categories from database and put them into some hashes and array $var{cathashref} = $var{dbh}->selectall_hashref('SELECT * FROM categories', 'name'); $var{catidref} = $var{dbh}->selectall_hashref('SELECT * FROM categories', 'id'); my @mykeys = sort keys %{$var{cathashref}}; $var{catkeys} = \@mykeys; # Start printing the html print "Content-type: text/html\n\n", ' Joe and Wilma at home Recipe Database

Wilma and Joes Recipes

Home

'; # decide what action to take if ($var{edit} eq "1") { &EditRecipe(%var); } elsif ($var{create} eq "1") { Create(%var); } elsif ($var{newrecipe} eq "1") { NewRecipe(%var); } elsif ((length($var{category})==0) and (length($var{searchStr})==0) and (length($var{recipe})==0)) { SearchForm(%var); } elsif (length($var{recipe})>0) { GetRecipe(%var); } else { Search(%var); } #close the html and disconnect from the database Bye(%var); } ##################################################### sub SearchForm () { my %var = @_; foreach ('A'..'Z') { print "$_ | "; } print '

Category:'; print $var{q}->popup_menu(-name => 'category', -values => $var{catkeys}, -default => "Uncategorized"); print '
Search For:
'; return 0; } ##################################################### sub Search () { my %var = @_; my $query = ""; my $recipeHashRef = ""; if (($var{category} eq "Uncategorized") && (length($var{searchStr})==0)) { &SearchForm(%var); return; } elsif (($var{category} eq "Uncategorized") && (length($var{searchStr})==1)) { $query = "SELECT * FROM recipes WHERE title LIKE \'" . $var{searchStr} . '%\'ORDER BY title'; } elsif (($var{category} eq "Uncategorized") && (length($var{searchStr})>1)) { $query = "SELECT * FROM recipes WHERE title LIKE \'%" . $var{searchStr} . '%\'ORDER BY title'; } elsif (($var{category} ne "Uncategorized") && (length($var{searchStr})==0)) { $query = "SELECT * FROM recipes WHERE category_id = " . $var{cathashref}->{$var{category}}->{id} . " AND title LIKE \'" . $var{searchStr} . '%\'ORDER BY title'; } elsif (($var{category} ne "Uncategorized") && (length($var{searchStr})==1)) { $query = "SELECT * FROM recipes WHERE category_id = " . $var{cathashref}->{$var{category}}->{id} . " AND title LIKE \'" . $var{searchStr} . '%\'ORDER BY title'; } elsif (($var{category} ne "Uncategorized") && (length($var{searchStr})>1)) { $query = "SELECT * FROM recipes WHERE category_id = " . $var{cathashref}->{$var{category}}->{id} . " AND title LIKE \'%" . $var{searchStr} . '%\'ORDER BY title'; } $recipeHashRef = $var{dbh}->selectall_hashref($query, 'id'); print "

"; foreach my $id (sort keys %$recipeHashRef){ print ""; print ""; print ""; print ""; } print "
$recipeHashRef->{$id}->{title}$var{catidref}->{$recipeHashRef->{$id}->{category_id}}->{name}$recipeHashRef->{$id}->{date}
"; return 0; } ##################################################### sub GetRecipe () { my %var = @_; my $query = "SELECT * FROM recipes WHERE id = $var{recipe}"; my $recipeHashRef = $var{dbh}->selectall_hashref($query, 'id'); #uncomment to debug #print $query, "
", $DBI::errstr, "
"; print "

$recipeHashRef->{$var{recipe}}->{title}

"; print '"; print "
', $recipeHashRef->{$var{recipe}}->{instructions}, "
"; print '
Edit this recipe '; return 0; } ##################################################### sub NewRecipe () { my %var = @_; print $var{q}->start_form(-method=>"post", -action=>$var{path}); print $var{q}->hidden("create", "1"); print "Title:
"; print $var{q}->textfield('title','',30,80); print "

Category:
"; print $var{q}->popup_menu(-name => 'category', -values => $var{catkeys}, -default => "Uncategorized"); print "

Instructions:
"; print $var{q}->textarea('instructions','',30,70); print "

"; print $var{q}->submit('Submit','Create'); print $var{q}->end_form(); print ""; return 0; } ##################################################### sub EditRecipe () { my %var = @_; if (length($var{q}->param('instructions'))>0) { Update(%var); return 0; } my $query = "SELECT * FROM recipes WHERE id = $var{recipe}"; my $recipeHashRef = $var{dbh}->selectall_hashref($query, 'id'); #uncomment to debug #print $query, "
", $DBI::errstr, "
"; print $var{q}->start_form(-method=>"post", -action=>$var{path}); print $var{q}->hidden("edit", "1"); print $var{q}->hidden("recipe", $var{recipe}); print "Title:
"; print $var{q}->textfield('title',$recipeHashRef->{$var{recipe}}->{title},30,80); print "

Category:
"; print $var{q}->popup_menu(-name => 'category', -values => $var{catkeys}, -default => $var{catidref}->{$recipeHashRef->{$var{recipe}}->{category_id}}->{name}); print "

Instructions:
"; my $instr = $recipeHashRef->{$var{recipe}}->{instructions}; $instr =~ s/
|

/\n/g; print $var{q}->textarea('instructions',$instr,30,70); print "

"; print $var{q}->submit('Submit','Update'); print $var{q}->end_form(); print "

"; return 0; } ##################################################### sub Create () { my %var = @_; my @d = localtime; my $date = $d[5]+1900 . "-" . $d[4] . "-" . $d[3]; my $query = "INSERT INTO recipes (title, description, date, instructions, category_id) VALUES (\'$var{title}\', \'$var{title}\', \'$date\', \'$var{instr}\', $var{cathashref}->{$var{category}}->{id})"; $var{dbh}->do($query); #uncomment to debug #print $query, "
", $DBI::errstr, "
"; &GetRecipe(%var); return 0; } ##################################################### sub Update () { my %var = @_; my @d = localtime; my $mo = $d[4] + 1; my $yr = $d[5] + 1900; my $day = $d[3]; my $date = "$yr-$mo-$day"; my $query = "UPDATE recipes SET instructions = \'$var{instr}\', date = \'$date\', category_id = $var{cathashref}->{$var{category}}->{id} WHERE id = $var{recipe}"; $var{dbh}->do($query); #uncomment to debug #print $query, "
", $DBI::errstr, "
"; &GetRecipe(%var); return 0; } ##################################################### sub Bye () { my %var = @_; #Print out the tail print '
Search for recipies Create a new recipe
'; $var{dbh}->disconnect(); return 0; } # Start the program # start();