Welcome Guest, Not a member yet? Register   Sign In
dynamically store data in multidimensional arrays with keys
#11

[eluser]crumpet[/eluser]
whats wrong with $array = $query->result_array()
#12

[eluser]Frank Berger[/eluser]
of course you get undefined index: name

Code:
class Recipe extends Model {

    
    
    function get_recipe_data() {
        
        
        $query = $this->db->query("SELECT id, user_id, name, prep_time, cook_time, directions, photo, submitted_time FROM recipes");
        $data = array();
        $data['recipes']=array();

//----- here you define $data['recipes']
        
        foreach($query->result() as $row) {
        
            array_push($data['recipes'],array('id' => $row->id, 'name' => $row->name, 'prep_time'=> $row->prep_time));
        }

        return $data;
    }
}

Controller search.php
Code:
<?php

class Search extends Controller {

    function Search()
    {
        parent::Controller();
        $this->load->model('recipe');
        
    }
    
    function index()
    {
        $data['title'] = "RecipeMatcher - Serch: ";
        $this->form_validation->set_rules('search',     'Search',     '');
        
        
        if ($this->form_validation->run() == TRUE) {
            
            $data['recipes'] = $this->recipe->get_recipe_data();

// here you load your the return of get_recipe_data, which is at least $data['recipes'] into an array which is $data['recipes']\
// your array looks now like this:
// $data['recipes']=>array('recipes'=>array(0=>array('id'=>1,'name=>'foo')))

            $this->load->view('header', $data);
            $this->load->view('search_bar');
            $this->load->view('search',     $data);
            $this->load->view('footer', $data);
        }

View search.php
Code:
<?php
                foreach($recipes as $recipe)
// the content of $recipe here is
// $recipe=array('recipes'=>array(0=>...
// instead of what you expected                
                    
                    ?>
                        <div class="recipe_box">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td width="100" align="left" valign="top"><img src="&lt;?=base_url()?&gt;system/application/assets/images/recipe/noPhotoSmall.gif" /></td>
                                <td valign="top">
                                    <span style="font-size:16px; color:#000000;"><b>&lt;?=$recipe['name']?&gt;</b></span><br />
                                </td>
                              </tr>
                          </table>
                        </div>
                    
                    &lt;?php
                }
            ?&gt;

easy solution, write your model like this:

Code:
class Recipe extends Model {

    
    
    function get_recipe_data() {
        
        
        $query = $this->db->query("SELECT id, user_id, name, prep_time, cook_time, directions, photo, submitted_time FROM recipes");
        $data = array();
        
        foreach($query->result() as $row) {
        
            $data[]=array('id' => $row->id, 'name' => $row->name, 'prep_time'=> $row->prep_time);
        }

        return $data;
    }
}

now it should work like you expected it. your 'mistake' was that your model cared how the view works with the data. Let the controller care how the view receives data, thats the job of the controller.

Frank
#13

[eluser]smickiedoo[/eluser]
Awesome!

Simple solution indeed.

Thank you.




Theme © iAndrew 2016 - Forum software by © MyBB