Page 1 of 1

[Solved] Dynamically populating array

Posted: Wed Oct 22, 2008 11:40 am
by Addos
I need to dynamically add content to an array I’m using so for example I’m using the following:

Code: Select all

$review_text = array("I", "Ate", "My", "Dinner" );
But I want to dynamically populate this from fields in my database and I think I need to run some kind of loop.

For example how can I pass this which returns i Ate My Dinner

Code: Select all

do {
 
$text = $row_GetRandomReview['random_details_field'];
 
} while ($row_GetRandomReview1 = mysql_fetch_assoc($GetRandomReview1));
Into my array e.g. similar to something like this:

Code: Select all

$review_text = array($text );
Thanks for any help.

Re: Dynimically populating array

Posted: Wed Oct 22, 2008 11:44 am
by watson516
Appending the new stuff to the end of the array

Code: Select all

 
$someArray=array();
$someArray[]="New Item";
 
Just put that in your do loop, minus the initialization of the array.

Re: Dynimically populating array

Posted: Wed Oct 22, 2008 11:44 am
by dude81
Change your code as below


Code: Select all

 
$text = array()
do {
 
$text[] = $row_GetRandomReview['random_details_field'];
 
} while ($row_GetRandomReview1 = mysql_fetch_assoc($GetRandomReview1));
Into my array e.g. similar to something like this:

Code: Select all

$review_text = $text

Re: Dynimically populating array

Posted: Thu Oct 23, 2008 6:23 am
by Addos
Thank you both I got this to work a treat!
:wink: