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

[eluser]smickiedoo[/eluser]
Hello,

I'm looking for a way to store data from a database into a multidimensional constructed like the following:
Code:
$data = array(
    'something' => array(
        array('id' => 1, 'name' => 'title 1'),
        array('id' => 2, 'name' => 'title 2'),
        array('id' => 3, 'name' => 'title 3'),
        array('id' => 4, 'name' => 'title 4'),
        array('id' => 5, 'name' => 'title 5')
        )
    );

How would such a thing be done?

The goal is to be able to reference data in my views like this:

Code:
foreach($something as $thing) {
    
    echo($thing['name']);
}

I'm sure the solution will be obvious when I see it, but I need help.

Thanks,
#2

[eluser]ray73864[/eluser]
dunno if this will work, but probably something similar to this:

Code:
$data['something'] = array();

foreach($query->result() as $row)
{
  $data['something'][] = array('id'=>$row->id,'name'=>$row->name,'title'=$row->title
}
#3

[eluser]smickiedoo[/eluser]
Looks like it would work but it throws an error


Parse error: syntax error, unexpected '=', expecting ')'

This should be simple to do...
#4

[eluser]smickiedoo[/eluser]
Found the PHP function array_push(), which is used to append data to the end of an existing but I can't get it to work.

I tried:

Code:
$data['something'] = array();
foreach($query->result() as $row)
        {
          array_push(
                  
                $data['something'],  array('id'=>$row->id, 'name'=>$row->name, 'title'=$row->title )
            );
        }

It throws the same error as above...
#5

[eluser]kgill[/eluser]
look at your code after 'title' you're missing the > after the = sign.
#6

[eluser]ray73864[/eluser]
yeah, excuse my typos, i have a cold at present (seriously, how can one get a cold during an aussie spring/summer Sad)
#7

[eluser]smickiedoo[/eluser]
So it no longer throws an error but the data is still not being appended.

I tried:

Code:
$data['something'] = array('id' => 1, 'name' => 'title 1', 'more_data'=>'hello world');
foreach($query->result() as $row)
        {
          array_push(
                  
                $data['something'],  array('id'=>$row->id, 'name'=>$row->name, 'more_data'=>$row->more_data )
            );
        }

and in my view:

Code:
foreach($something as $item) { echo($item['id']); }

It outputs only:

1
#8

[eluser]ray73864[/eluser]
when i originally gave the example i assumed you had the database stuff all worked out, could you please post the relevant code (from the function that this code is in) so that we can see if it is a database query problem, etc...
#9

[eluser]Frank Berger[/eluser]
First of all, you would be overwriting your inital array, better do it like this:
Code:
$data = array();
$data['something']=array();
array_push($data['something'],array('id' => 1, 'name' => 'title 1', 'more_data'=>'hello world'));
this way, your inital value becomes index 0 of the indexed array $data['something'], before it was an associative array.

next, are you sure that your query really really returns more than one row here:
Code:
foreach($query->result() as $row)
and that the one returning row's id is not coincidentally 1 as well?

because your foreach in the controller as it is wouldn't return 1 with your initial array,
Code:
foreach($something as $item) { echo($item['id']); }
it would return null or a warning, as you expected an indexed array (as it is produced in your db-routine) and not an associative array as you initially created.

Frank
#10

[eluser]smickiedoo[/eluser]
gives the error:

Message: Undefined index: name

Filename: views/search.php

OK, this is getting to me now. All I'm trying to do is display search results on a page.

My code is as follows:


Model recipe.php
Code:
class Recipe extends Model {

    function Recipe(){
    
        parent::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();
        
        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();
            
            $this->load->view('header', $data);
            $this->load->view('search_bar');
            $this->load->view('search',     $data);
            $this->load->view('footer', $data);
        }
        else {
            $data['search_success'] = FALSE;
        
            $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) {
                
                    
                    ?>
                        <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;

If there is a very easy and simple way to achieve this, that's what I want. If array_push is not necessary I'd rather not have the overhead of the function.

big thanks for any help with this.




Theme © iAndrew 2016 - Forum software by © MyBB