Jump to content

Array Push Error but I've declared Globals


emilyfrazier

Recommended Posts

Hi,

I am getting an error about array push, but I've declared my global variables. The error I get is:

[color=red]warning: array_push() [function.array-push]: First argument should be an array in /home/retnkes9/public_html/drupal/themes/retn/page.tpl.php on line 18.[/color]

Here is my code:
[code]<?php function getRandomImages($count = 1){
global $randomImages;
srand((float) microtime() * 10000000);
$ret = array();
global $ret;
$keys = array_rand($randomImages, $count);
foreach( $keys as $index ){
array_push( $ret, $randomImages[$index] );
}
return  $ret;
}

$randomImages = array();
function addRandomImage( $img = false ) { if( $img ){
global $randomImages;
array_push( $randomImages, $img );
}}
for( $i=1; $i<41; $i++ ){
addRandomImage( "../../../images/random_".sprintf("%02d", $i).".jpg" );
}

?>

<table border="0" cellpadding="0" cellspacing="0" id="header">
  <tr>
   
  <?php
$randomImages = getRandomImages(5);
$first = true;
foreach( $randomImages as $image ){
?>
<?php if( $first ){ $first=false; } else{ ?><td width="16">
<img src="../../../images/randompic_divider.gif" width="16" height="52"></td><?php } ?>
    <td width="52"><img src="<?php echo $image; ?>" width="52" height="52"></td>   
<?php } ?>
    [/code]

I would really appreciate any help on this in advance. It's really messing with me!

Thanks,
Emily
Maybe I am not understanding, but I believe I declared $randomImages both in the first and second functions. I declared the array in between the functions. ... I thought making it global would take care of the issue? Thanks for helping,

Emily
Hmm. Just tried that, but it didn't change anything. Am still getting the same errors.

Here is my code:[code] <?php
function getRandomImages($count = 1){
global $randomImages;
srand((float) microtime() * 10000000);
$ret = array();
$keys = array_rand($randomImages, $count);
foreach( $keys as $index ){
array_push( $ret, $randomImages[$index] );
}
return  $ret;
}

$randomImages = array();

function addRandomImage( $img = false ) { if( $img ){
global $randomImages;
array_push( $randomImages, $img );
}}
for( $i=1; $i<41; $i++ ){
addRandomImage( "../../../images/random_".sprintf("%02d", $i).".jpg" );
}

?>


<table border="0" cellpadding="0" cellspacing="0" id="header">
  <tr>
 
   
  <?php
$randomImages = getRandomImages(5);
$first = true;
foreach( $randomImages as $image ){
?>
<?php if( $first ){ $first=false; } else{ ?><td width="16">
<img src="../../../images/randompic_divider.gif" width="16" height="52"></td><?php } ?>
    <td width="52"><img src="<?php echo $image; ?>" width="52" height="52"></td>   
<?php } ?>[/code]

Thanks for helping!
Emily

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.