Skip to main content
Participant
September 29, 2013
Question

Relate, join, add row to table or what?

  • September 29, 2013
  • 1 reply
  • 1008 views

Using Dreamweaver cs3, php and mysql

I'm trying to make a simple recipe program. I'm new to all this.  The index.php has the following links and options:

 

I've a table with 2 rows and 5 columns. Top row, first column says "cookbook names," second column is "add cookbook." Second row is a repeat region, first column lists the cookbook names, 2nd column has a link to "view chapters", 3rd column is change cookbook name and last column deletes cookbook and all chapters/recipes within.

 

The goal is to list cookbooks like:

 

Brians cookbook

Mary's cookbook

Our cookbook

...etc. When I click on view chapter next to a cookbook, I want to list chapters associated with the cookbook. e.g. Brian's cookbook has the folowing chapters within:

 

Sauces

Pasta dishes

Sea food

pork

...etc. Then click on a chapter, say sauces, and that would display a list of sauces, like mornay, bechemel, tomato, and finally, clicking on mornay sauce pulls up the recipe.

 

Think of it this way. I open a real, hard cover/paper cookbook, the index lists chapters, and when I go to a chapter, I see recipes.

 

MAN I hate typing!

 

How do I make it so that if I create a new cookbook, I can create chapters that will be in that cookbook and no others? Some kind of relation, a join command?

 

 

-- phpMyAdmin SQL Dump

-- version 3.3.5

-- http://www.phpmyadmin.net

--

-- Host: 127.0.0.1

-- Generation Time: Sep 29, 2013 at 03:25 AM

-- Server version: 5.1.36

-- PHP Version: 5.3.26

 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

 

 

/*!40101 SET @9272211_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;

/*!40101 SET @9272211_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;

/*!40101 SET @9272211_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;

/*!40101 SET NAMES utf8 */;

 

--

-- Database: `Gourmet_Entertainment`

--

 

-- --------------------------------------------------------

 

--

-- Table structure for table `chapter`

--

 

CREATE TABLE IF NOT EXISTS `chapter` (

`chapter_id` int(11) NOT NULL AUTO_INCREMENT,

`Chapter_Name` varchar(100) NOT NULL,

PRIMARY KEY (`chapter_id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

 

--

-- Dumping data for table `chapter`

--

 

INSERT INTO `chapter` (`chapter_id`, `Chapter_Name`) VALUES

(1, 'Pasta'),

(2, 'Sauces'),

(4, 'Soups'),

(5, 'booze');

 

-- --------------------------------------------------------

 

--

-- Table structure for table `cookbook`

--

 

CREATE TABLE IF NOT EXISTS `cookbook` (

`cookbook_id` int(11) NOT NULL AUTO_INCREMENT,

`cookbook_name` varchar(100) NOT NULL,

PRIMARY KEY (`cookbook_id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;

 

--

-- Dumping data for table `cookbook`

--

 

INSERT INTO `cookbook` (`cookbook_id`, `cookbook_name`) VALUES

(13, 'Brian''s Cookbook'),

(18, 'Mary''s'),

(20, '');

 

-- --------------------------------------------------------

 

--

-- Table structure for table `menu`

--

 

CREATE TABLE IF NOT EXISTS `menu` (

`menu_id` int(11) NOT NULL AUTO_INCREMENT,

`recipe_id` int(11) DEFAULT '0',

`name` varchar(100) DEFAULT NULL,

`prep_date` date DEFAULT NULL,

`cook_date` date DEFAULT NULL,

PRIMARY KEY (`menu_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

--

-- Dumping data for table `menu`

--

 

 

-- --------------------------------------------------------

 

--

-- Table structure for table `recipe`

--

 

CREATE TABLE IF NOT EXISTS `recipe` (

`recipe_id` int(11) NOT NULL AUTO_INCREMENT,

`cookbook_id` int(11) DEFAULT '0',

`chapter` int(11) DEFAULT '0',

`page` int(11) DEFAULT '0',

`recipe_name` varchar(100) DEFAULT NULL,

`recipe` text,

PRIMARY KEY (`recipe_id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

 

--

-- Dumping data for table `recipe`

--

 

INSERT INTO `recipe` (`recipe_id`, `cookbook_id`, `chapter`, `page`, `recipe_name`, `recipe`) VALUES

(1, 2, 2, 0, 'Drunken shrimp', 'cook with scotch'),

(2, 2, 2, 0, 'Alfredo Sauce', 'Lots of cream.');

 

-- --------------------------------------------------------

 

--

-- Table structure for table `recipe_incredents`

--

 

CREATE TABLE IF NOT EXISTS `recipe_incredents` (

`recipe_incredents_id` int(11) NOT NULL AUTO_INCREMENT,

`recipe_id` int(11) DEFAULT '0',

`incredents_id` int(11) DEFAULT '0',

`name` varchar(100) DEFAULT NULL,

`quantity` float DEFAULT '0',

`measurements` varchar(20) NOT NULL,

PRIMARY KEY (`recipe_incredents_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

--

This topic has been closed for replies.

1 reply

Participating Frequently
October 2, 2013

>How do I make it so that if I create a new cookbook, I can create chapters that

> will be in that cookbook and no others? Some kind of relation, a join command? '

I don't understand your problem. The way your data is structured, chapters do not belong to cookbooks, they belong to recipes.  So a cookbook WILL only have chapters of the related recipes.

If your question is "How do I only show chapters related to a particular cookbook, then yes, the answer is a join.

SELECT DISTINCT cookbook.cookbook_name, chapter.Chapter_Name

FROM cookbook, chapter, recipe

WHERE

recipe.cookbook_id = cookbook.cookbook_id AND

recipe.chapter = chapter.chapter_id

One thing you must do before continuing is standardizing on your naming conventions. In one case you use all lower case (cookbook_name) while using mixed case in another (Chapter_Name). You also add the _id suffix to some columns but not other id columns.