I hope someone can help me with this I have been banging my head on it
for a week now.

In my view I have a form. Part of that form is a list of inputs that
are in a table. These inputs are all related to an ingredient and its
attributes. The inputs and rows are created from an element one row at
a time. Underneath the table I want to place a button that when
clicked will add a new row, essentially the output of the element.

So the page renders the first time with one empty row of inputs that
is derived from the element: ingredient_row.ctp

When the user clicks the Add Another Ingredient button an AJAX method
should be called that gets the content of ingredient_row.ctp and then
updates the table (ingredient_table) with this new data without
clearing the other inputs.

Every avenue I have tried has failed impressively.

I tried building the rows in pure javascript leaving CakePHP out of
the loop but I cant fill my select lists from the CakePHP vars for
ingredient_types and measurement_types.

I tried getting the output of the element in a javascript function but
the multi line strings messes it up.

I am really stuck on this. Any help would be appreciated. If this is
the completely wrong approach would someone tell me? If someone knows
how to do this, or even part of this please tell me.

Thanks in advance. Code posted below.

ADD RECIPE VIEW

<h1>Add Recipe</h1>
<?php
echo $form->create('Recipe');
echo $form->input('Recipe.recipe');
echo $form->input('Recipe.servings', array('maxlength' => 5, 'size' =>
5));
echo $form->label('Recipe.servings', 'Recipe Type');
echo $form->select('Recipe.recipe_type_id', $recipe_types);
?>
<div>
<h2>Ingredient List</h2>
<table id="ingredient_list">
<tr>
        <th>Amount</th>
        <th>Measurement Type</th>
        <th>Description</th>
        <th>Ingredient</th>
</tr>
<?php
for($index = 0; $index < $ingredient_count; $index ++)
{
        echo $this->element('ingredient_row', array('index' => $index,
'delete' => false));
}
?>
</table>
<?php
$action = 'add/' . ($index + 1);
echo $html->link('Add', array('controller'=>'recipes', 'action'=>
$action));
?>
<h2>Instructions and Description</h2>
<?php
echo $form->input('Recipe.description', array('rows' => '3'));
echo $form->input('Recipe.instructions', array('rows' => '3'));
echo $form->end('Save Recipe');
?>



INGREDIENT ROW ELEMENT

        <tr id="ingredient_<?php echo $index; ?>">
                <td>
                        <?php
                        echo $form->input('IngredientList.'. $index .'.amount', 
array
('label' => false));
                        echo $form->input('IngredientList.'.$index.'.id', array
('type'=>'hidden'));
                        ?>
                </td>
                <td>
                        <?php
                        echo $form->select('IngredientList.'.
$index .'.measurement_type_id', $measurement_types);
                        ?>
                </td>
                <td>
                        <?php echo $form->input('IngredientList.'. $index 
.'.description',
array('label' => false)); ?>
                </td>
                <td>
                        <?php
                        echo $form->select('IngredientList.'. $index 
.'.ingredient_id',
$ingredients);
                        ?>
                </td>
                <?php
                if($delete == true)
                {
                ?>
                <td>
                        <?php echo $html->link('delete', 
'ingredient_list/delete');     ?>
                </td>
                <?php
                }
                ?>
        </tr>


RECIPE CONTROLLER

<?php
class RecipesController extends AppController
{
        /*
        Make sure that if you add another controller that you put the current
controller
        in the array as well or it wont be found.
        */

        var $uses = array('Recipe', 'RecipeType', 'Ingredient',
'MeasurementType', 'IngredientList');

        function index()
        {
                $this->set('recipes', $this->Recipe->findAll());
                $this->pageTitle = 'Recipes';
                $this->layout = 'default';
        }

        function view($id = null)
        {
                $this->Recipe->id = $id;
                $this->set('recipe', $this->Recipe->read());
        }

        function add($ingredient_count = null)
        {
                $this->set_recipe_types();
                $this->set_ingredients();
                $this->set_measurement_types();

                if($ingredient_count == null)
                {
                        $ingredient_count = 1;
                }

                $this->set('ingredient_count', $ingredient_count);

                if (!empty($this->data))
                {
                        if ($this->Recipe->saveAll($this->data))
                        {
                                $this->flash('Your recipe has been saved.', 
'/recipes');
                        }
                }
        }

        function edit($id = null)
        {
                $this->Recipe->id = $id;
                $this->set_recipe_types();
                $this->set_ingredients();
                $this->set_measurement_types();


                $list = $this->IngredientList->find('all', array ('recipe_id' =>
$id));

                //debug($list, true);

                foreach($list as $item)
                {
                        //debug($item['IngredientList']['recipe_id'], true);
                        if($item['IngredientList']['recipe_id'] == $id)
                        {
                                $ing_list[] = $item;
                        }
                }

                $this->set('ingredient_list', $ing_list);

                if (empty($this->data))
                {
                        $this->data = $this->Recipe->read();
                }
                else
                {
                        if ($this->Recipe->saveAll($this->data))
                        {
                                $this->flash('Your recipe has been 
updated.','/recipes');
                        }
                }
        }

        function delete_ingredient($id)
        {
                $this->IngredientList->del($id);
                $this->flash('The recipe with id: '.$id.' has been deleted.', '/
recipes');
        }

        private function set_recipe_types()
        {
                $this->set('recipe_types', $this->RecipeType->find('list', array
('fields'=>'RecipeType.recipe_type',
'order'=>'RecipeType.recipe_type')));
        }

        private function set_ingredients()
        {
                $this->set('ingredients', $this->Ingredient->find('list', array
('fields'=>'ingredient', 'order'=>'ingredient')));
        }

        private function set_measurement_types()
        {
                $this->set('measurement_types', 
$this->MeasurementType->find('list',
array('fields'=>'measurement_type', 'order'=>'measurement_type')));
        }
}

?>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to