Trouble with array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Trouble with array

Post by seodevhead »

Hey guys... For some reason with the code below... the array $arrayThreadTimes is not being created correctly and populated correctly. I know it is something simple I am missing here. Perhaps you guys could take a look and tell me if I have something wrong with this code. Thanks a bunch!

Code: Select all

$start = 1152691200 + 18000;  ## 07/12/06 @ 8 am
$end = 1152813600 + 180000;    ## 07/13/06 @ 6 pm

$query = "SELECT COUNT(threadid) FROM thread";
$result = mysql_query($query);
$set = mysql_fetch_row($result);

$numThreads = $set[0];

$arrayThreadTimes = array();

for ($i = 0; $i < $numThreads; $i++)
{
	$randTime = rand($start, $end);  ## Add 1800 for US Eastern Time.
	
	while ($randTime > (1152747000+18000) && $randTime < (1152777600+18000))
	{
		$randTime = rand($start, $end);
	}
	
	$arrayThreadTimes['$i'] = $randTime;
		
}

sort($arrayThreadTimes, SORT_NUMERIC);
print_r($arrayThreadTimes);
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Most likely $numThreads is 0. Might want to muck with the query a little bit...

Something like this might work..

Code: Select all

$query = "SELECT COUNT(threadid) as `total` FROM thread";
$result = mysql_query($query);
$set = mysql_fetch_assoc($result);

$numThreads = $set['total'];
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

// perhaps
$arrayThreadTimes['$i']
// should be 
$arrayThreadTimes[$i]
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Oooh good catch!
Post Reply