dynamically store data in multidimensional arrays with keys - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: dynamically store data in multidimensional arrays with keys (/showthread.php?tid=13453) Pages:
1
2
|
dynamically store data in multidimensional arrays with keys - El Forum - 11-23-2008 [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( 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) { I'm sure the solution will be obvious when I see it, but I need help. Thanks, dynamically store data in multidimensional arrays with keys - El Forum - 11-23-2008 [eluser]ray73864[/eluser] dunno if this will work, but probably something similar to this: Code: $data['something'] = array(); dynamically store data in multidimensional arrays with keys - El Forum - 11-23-2008 [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... dynamically store data in multidimensional arrays with keys - El Forum - 11-23-2008 [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(); It throws the same error as above... dynamically store data in multidimensional arrays with keys - El Forum - 11-23-2008 [eluser]kgill[/eluser] look at your code after 'title' you're missing the > after the = sign. dynamically store data in multidimensional arrays with keys - El Forum - 11-23-2008 [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 ) dynamically store data in multidimensional arrays with keys - El Forum - 11-23-2008 [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'); and in my view: Code: foreach($something as $item) { echo($item['id']); } It outputs only: 1 dynamically store data in multidimensional arrays with keys - El Forum - 11-23-2008 [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... dynamically store data in multidimensional arrays with keys - El Forum - 11-23-2008 [eluser]Frank Berger[/eluser] First of all, you would be overwriting your inital array, better do it like this: Code: $data = array(); next, are you sure that your query really really returns more than one row here: Code: foreach($query->result() as $row) 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']); } Frank dynamically store data in multidimensional arrays with keys - El Forum - 11-23-2008 [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 { Controller search.php Code: <?php View search.php Code: <?php 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. |