配方查找器,PHP和MySQL基于成分

时间:2012-03-28 09:59:49

标签: php mysql

我正在开发一个烹饪食谱网站,我想根据使用的难题创建一个食谱查找器。

我目前的发现者只能使用3种成分。

Finder应该根据使用过的难度返回正确的食谱(应该使用1-n *)

我的表格:

CREATE TABLE IF NOT EXISTS `INGREDIENTS` (
  `ingredients_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY (`ingredients_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;

CREATE TABLE IF NOT EXISTS `INGREDIENTS_POS` (
  `ingredients_pos_id` int(11) NOT NULL AUTO_INCREMENT,
  `ingredients_id` int(11) NOT NULL,
  `ingredients_unit` varchar(20) NOT NULL,
  PRIMARY KEY (`ingredients_pos_id`),
  KEY `ingredients_detail_fk` (`ingredients_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;

CREATE TABLE IF NOT EXISTS `RECIPES` (
  `recipes_id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(50) COLLATE utf8_bin NOT NULL,
  `text` varchar(2000) COLLATE utf8_bin NOT NULL,
  `count_persons` int(11) NOT NULL,
  `duration` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `date` datetime NOT NULL,
  `accepted` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`recipes_id`),
  KEY `recipes_user_fk` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=88 ;

CREATE TABLE IF NOT EXISTS `RECIPES_POS` (
  `recipes_pos_id` int(11) NOT NULL AUTO_INCREMENT,
  `recipes_id` int(11) NOT NULL,
  `ingredients_id` int(11) NOT NULL,
  `ingredients_value` int(11) NOT NULL,
  PRIMARY KEY (`recipes_pos_id`),
  KEY `recipe_pos_rec_id` (`recipes_id`),
  KEY `recipes_pos_ingredient_fk` (`ingredients_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=58 ;

我的错误解决方案(不支持1-n计数):

<?php
include 'db_connect.php';

$q = urldecode(mysql_real_escape_string($_GET['q']));
$parameter = explode ('$',$q);
$var = 0;
//print_r($parameter);

foreach($parameter as $ing)
{
    //echo $ing;
    $sql = "SELECT ingredients_id FROM  INGREDIENTS WHERE name='".$ing."'";
    $result = mysql_query($sql,$db) or exit('{"Data":null,"Message":null,"Code":500}');
    $row = mysql_fetch_array($result);
    $arr_id[$var] = $row['ingredients_id'];
    $var++;

}
//print_r($arr_id);

$sql = "SELECT r.recipes_id FROM RECIPES r, RECIPES_POS rp WHERE r.recipes_id = rp.recipes_id ";

foreach($arr_id as $id)
{
$sql .= "AND rp.ingredients_id =".$id . " ";
}
//echo $sql;
$result = mysql_query($sql,$db) or exit('{"Data":null,"Message":null,"Code":500}');
mysql_close($db);
$rec;
while($row = mysql_fetch_array($result))
             {
                 //echo "test";
                 $_GET['id'] = $row['recipes_id'];
                 $rec= include('get_recipe_byID.php');

             }

//print_r(mysql_fetch_array($result));
if (count($arr_id) == 0)
    {
        echo '{"Data":null,"Message":null,"Code":404}';
        die();
    }

?>

我需要一个更好的解决方案。 也许SQL本身会帮助我找到正确的食谱

THX

1 个答案:

答案 0 :(得分:0)

这个问题对我有很大帮助:

   select r.recipes_id
    from RECIPES r
    inner join RECIPES_POS rp on r.recipes_id = rp.recipes_id
    where rp.ingredients_id in (4, 6)
    group by r.recipes_id
    having count(distinct rp.ingredients_id) = 2