array - 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: array (/showthread.php?tid=22525) |
array - El Forum - 09-12-2009 [eluser]georgerobbo[/eluser] Is there a better way to structure this array? Code: <div id="panel_wrapper"> array - El Forum - 09-12-2009 [eluser]pistolPete[/eluser] The array you generated is this: Code: Array What exactly are you trying to achieve? array - El Forum - 09-12-2009 [eluser]georgerobbo[/eluser] I'm trying to achieve an array containing the quote and the author with multiple quotes and then generating a random quote using the rand() function. I have a structure, Code: <?php $quote[] = array ( 'author' => 'Doug Larson', 'quote' => 'Life expectancy would grow by leaps and bounds if green vegetables smelled as good as bacon.'); ?> Is there a better way of structuring this array? array - El Forum - 09-12-2009 [eluser]NateL[/eluser] I think what you want is something like Code: $item = array(); now, do a var_dump on $item, and you should get: Code: Array array - El Forum - 09-12-2009 [eluser]bretticus[/eluser] I think perhaps you need an array or arrays. In other words, you can put each author and quote in it's own associative array and then encapsulate each one of those in one indexed array. After that, you can generate a random index. See below to demonstrate: Code: $quotes = array(); Now if I were you, I'd generate an author and quote variable in your controller and then send it to your view (to keep that logic out of the view.) array - El Forum - 09-12-2009 [eluser]Aken[/eluser] Are these quotes stared in a database? If so, you can use a RAND() function directly in the DB call to return only one random quote, instead of a whole array. Will make things faster and easier to manage. Normal query: Code: SELECT * FROM table ORDER BY rand() LIMIT 1 CI Active Record: Code: $this->db->order_by('id', 'random'); |