![]() |
Returning A Result (Problem With Arrays I Believe) - 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: Returning A Result (Problem With Arrays I Believe) (/showthread.php?tid=2253) |
Returning A Result (Problem With Arrays I Believe) - El Forum - 07-25-2007 [eluser]Chris.Campbell[/eluser] Ok so I am trying to return a result from a database. I think it's being put into a multidimensional array... I am not 100% sure. Anyways look below. This is my model. As you can see in the get_header() function it is pulling the result from the table "header". Code: <?php This my controller. As you can see in the header_view() function, it's taking the result and putting it in an array. And then the index is taking the "header_view()" and putting it in another array. Code: <?php Now I am trying to call it... I cannot figure out how! Code: <title><?php echo $header[2]['title']; ?></title> Any help is great please! Returning A Result (Problem With Arrays I Believe) - El Forum - 07-25-2007 [eluser]Imkow@CN[/eluser] Your search result usually is a 3-dimentional array. use print_r($array) to show how it structured. By following the code in your controller, code in your view should be like: <title><?php echo $title[0]["header"]; ?></title> Here $title is the varible you assigned in controller, [0] is the first set(row) of your query result, Returning A Result (Problem With Arrays I Believe) - El Forum - 07-25-2007 [eluser]座頭市[/eluser] Basically, you need to return something from your function get_header(), right now all it ![]() Code: function header_view() In the view, you have an array of objects, not a multidimensional array, so you should be accessing it like this: Code: <title><?=$header[0]->title;?></title> If you want to return a multidimensional array, use result_array() rather than result() in the model. Returning A Result (Problem With Arrays I Believe) - El Forum - 07-25-2007 [eluser]Chris.Campbell[/eluser] [quote author="座頭市" date="1185427349"]Basically, you need to return something from your function get_header(), right now all it ![]() Code: function header_view() In the view, you have an array of objects, not a multidimensional array, so you should be accessing it like this: Code: <title><?=$header[0]->title;?></title> If you want to return a multidimensional array, use result_array() rather than result() in the model.[/quote] I used the suggestions you outlined such as using row() and return, I am confused about how I would output this in the header now. Returning A Result (Problem With Arrays I Believe) - El Forum - 07-25-2007 [eluser]座頭市[/eluser] If you're using row() rather than result() in your model function, just call Code: <?=$header->title;?> Returning A Result (Problem With Arrays I Believe) - El Forum - 07-25-2007 [eluser]Chris.Campbell[/eluser] [quote author="座頭市" date="1185428790"]If you're using row() rather than result() in your model function, just call Code: <?=$header->title;?> Hmmm now I get an error: Message: Trying to get property of non-object Returning A Result (Problem With Arrays I Believe) - El Forum - 07-25-2007 [eluser]座頭市[/eluser] Please post your updated code, or edit your original post to reflect the changes. Returning A Result (Problem With Arrays I Believe) - El Forum - 07-25-2007 [eluser]Chris.Campbell[/eluser] Model. Code: <?php Controller. Code: <?php View. Code: <title><?php echo $header->title; ?></title> Returning A Result (Problem With Arrays I Believe) - El Forum - 07-25-2007 [eluser]座頭市[/eluser] Odd that - it all looks correct. I guess the most obvious thing would be to make sure the db table has a column called title? Returning A Result (Problem With Arrays I Believe) - El Forum - 07-25-2007 [eluser]Chris.Campbell[/eluser] Crap... the column is called "title" ... hmm I am not sure what is wrong. |