CodeIgniter Forums
array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: array (/thread-22525.html)



array - El Forum - 09-12-2009

[eluser]georgerobbo[/eluser]
Is there a better way to structure this array?

Code:
<div id="panel_wrapper">
                
                    <div id="panel_healthy">
                    </div>
                
                    <div id="panel_information">
                        <div class="container">
                        &lt;?php $quote[] = array ( 'author' => 'Doug Larson', 'quote' => 'Life expectancy would grow by leaps and bounds if green vegetables smelled as good as bacon.'); ?&gt;
                        &lt;?php foreach($quote as $item):?&gt;
                            <div class="quote"><p>&lt;?php echo $item['quote']; ?&gt;</p></div>
                            <div class="author"><p>&lt;?php echo $item['author']; ?&gt;</p></div>
                        &lt;?php endforeach; ?&gt;
                        </div>
                    </div>
                
                    <div id="panel_unhealthy">
                    </div>
                </div>



array - El Forum - 09-12-2009

[eluser]pistolPete[/eluser]
The array you generated is this:
Code:
Array
(
    [0] => Doug Larson
    [1] => Life expectancy would grow by leaps and bounds if green vegetables smelled as good as bacon.
)

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:
&lt;?php $quote[] = array ( 'author' => 'Doug Larson', 'quote' => 'Life expectancy would grow by leaps and bounds if green vegetables smelled as good as bacon.'); ?&gt;

&lt;?php $quote[] = array ( 'author' => 'G.K. Chesterton', 'quote' => 'The trouble with always trying to preserve the health of the body is that it is so difficult to do without destroying the health of the mind.'); ?&gt;

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();
$item['author'] = "Doug Larson";
$item['quote'] = "Life expectancy...";

now, do a var_dump on $item, and you should get:

Code:
Array
(
  [author] = "Doug Larson"
  [quote]  = "Life expectancy..."
)



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();
$quotes[] = array ( 'author' => 'Doug Larson', 'quote' => 'Life expectancy would grow by leaps and bounds if green vegetables smelled as good as bacon.');
$quotes[] = array ( 'author' => 'G.K. Chesterton', 'quote' => 'The trouble with always trying to preserve the health of the body is that it is so difficult to do without destroying the health of the mind.');

$index = rand(0,count($quotes)-1);

echo $quotes[$index]['author'];
echo $quotes[$index]['quote'];

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');
$this->db->limit(1);
$this->db->get('table');

// Method Chaining - PHP 5 only.
$this->db->order_by('id', 'random')->limit(1)->get('table');