Welcome Guest, Not a member yet? Register   Sign In
change controller to see more than one occurance of ingredients
#1

[eluser]scoobie[/eluser]
Hello all,

I have a problem / issue whereby i am not sure how to get my select statement to show me what i know it should be and
am hoping someone can tell me how to change this to be correct ?

say i have 2 recipes (shortbread and scones)

shortbread has these ingredients =
250g butter
140gm icing sugar
300gm plain flour
75gm cornflour
5ml vanilla extract

and scones has these =
450gm self-raising flour
80g butter
250ml milk


and i want to get ingredients for 2 lots of shortbread and 1 lot of scones,


in my db i have all of the ingredients (as well a categories of food like 'cold storage',
'herbs / spices' and 'Long Life - Sugar / Baking'
and from the db i want to create a shopping list that itemises the ingreients and the
quantities into categories so i would be expecting something along the lines of


Cold Storage 580 Gram Butter
500 Millilitre Milk
Herbs / Spices 10 Millilitre Vanilla extract
Long Life - Sugar / Baking 150 Gram Flour - cornflour
600 Gram Flour - plain
450 Gram Flour - self-raising
280 Gram Icing sugar


however, what's actually returning is

69 occurred 2 times! 68 occurred 1 times!
Cold Storage 250 + 80 Gram Butter it's not adding here either but the sum give an error in codeigniter that i'll try to fix later
250 Millilitre Milk
Herbs / Spices 5 Millilitre Vanilla extract
Long Life - Sugar / Baking 75 Gram Flour - cornflour
300 Gram Flour - plain
450 Gram Flour - self-raising
140 Gram Icing sugar

so the 'select' is only seeing 2 recipes ie. it's not seeing that one recipe is chosen
twice (shortbread) and the other recipe (scones) is only chosen once

i have to put CI code in another post as it wont fit here, sorry
#2

[eluser]scoobie[/eluser]
Code:
public function preview_shopping_list ($list_name = NULL)
    {
    $data['user_list_url'] = $list_name;  
    $id=$_SESSION['id'];                  
  
        if($this->cfd->auth_login(1, TRUE) === TRUE) {
            $data['show_all'] = FALSE;
            
            $start =date('Y-m-d', strtotime($_POST['startdate']));
            $end =date('Y-m-d', strtotime($_POST['enddate']));
            $start1 = date("j F Y", strtotime($start));
            $end1 = date("j F Y", strtotime($end));
            $data['title'] = 'View all ingredients for recipes chosen for start date '.$start1. ' to end date '.$end1;
          
            $items = new List_item();    
            $items->include_join_fields()->include_related('recipe')->where_in('user_list_id', $id)->where('item_date >=', $start)->where('item_date <=', $end)->get();

            $recipe_ids = array();
            foreach($items AS $item)
            {
              $recipe_ids[] = $item->recipe_id;
            }

            if (empty($recipe_ids))
            {    
            ?&gt;
                        [removed]
                        alert("there are no recipes for those dates - please choose again ...");
                        [removed]
                        
                        &lt;?php
                        $_POST['startdate']=NULL;
                        $_POST['enddate']=NULL;
              
                        $url = '/diet_plan/view_shopping_list/'.$list_name;
                        echo '&lt;META HTTP-EQUIV=Refresh C URL='.$url.'"&gt;';  
            }
            else{
                  $array = array_count_values($recipe_ids);
                  foreach( $array as $number => $times_number_occurred) {
                      echo ' ' . $number . ' occurred ' . $times_number_occurred . ' times!';
                  }              
              }
        
        
            $items = new Recipe();
            $items->include_join_fields()->include_related('ingredient')->where_in('id', $recipe_ids)->order_by('name', 'asc')->get();

            $unit_ids = array();
            foreach($items AS $item)
            {
              $unit_ids[$item->join_unit_id] = $item->join_unit_id;  
            }
                    
            $units_data = new Unit();
            $units_data->where_in('id', $unit_ids)->get();
            $units = array();
            foreach($units_data AS $unit)
            {
               if ($unit->name=='Unit')
                  {
                  $unit->name='';
                  }
                $units[$unit->id] = $unit->name;
            }
            
            $category_ids = array();
            foreach($items AS $item)
            {
                $category_ids[] = $item->ingredient_item_category_id;  
            }
            
            $categories = new Item_category();
            $categories->where_in('id', $category_ids)->order_by('name', 'asc')->get();
            
            if($items->exists()) {
                 $data['item_count_item'] = $items->result_count();
                 $data['items'] = $items;    

                $data['categories'] = array();
                foreach($categories AS $category)
                {
                  $data['categories'][$category->id] = array('name' => $category->name, 'ingredients' => array());
                }
                
                foreach($items AS $item)
                {
                
                  if(!isset($data['categories'][$item->ingredient_item_category_id]['ingredients'][$item->ingredient_id]))  
                  {
                    $data['categories'][$item->ingredient_item_category_id]['ingredients'][$item->ingredient_id] = array(
                      'name' => $item->ingredient_name,
                      'amount' => round($item->join_amount,2),
                      'unit' => $units[$item->join_unit_id],
                      'ingredient_id' => $item->ingredient_id,
                      'recipe_id' => $item->id
                    );
                    }
                  else
                  {
                    $data['categories'][$item->ingredient_item_category_id]['ingredients'][$item->ingredient_id]['amount'] .= ' + ' .round($item->join_amount,2);
                    $data['categories'][$item->ingredient_item_category_id]['ingredients'][$item->ingredient_id]['unit'] .= ' / '.$units[$item->join_unit_id];                      
                      }
                }
              }  
            $this->cfd->build('diet/preview_shopping_list', $data);
        }
    }



Any ideas of things to try for changes to codeigniter controller much appreciated.
I think i've been looking at it for too long ! It shouldn't be so hard but i cant see
alternatives to try.

thank you to all




Theme © iAndrew 2016 - Forum software by © MyBB