#!/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", '
'; # 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 '
| $recipeHashRef->{$id}->{title} | "; print "$var{catidref}->{$recipeHashRef->{$id}->{category_id}}->{name} | "; print "$recipeHashRef->{$id}->{date} | "; print "
| ', $recipeHashRef->{$var{recipe}}->{instructions}, " |
| 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: Instructions: "; print $var{q}->submit('Submit','Create'); print $var{q}->end_form(); print "
|