Referencing a global array from inside of a function.

Discussion in 'PHP' started by Submerged, Jul 21, 2009.

  1. #1
    I know this isn't difficult, but I having the hardest time figuring out why it isn't working. It must one of the "devil in the details" kind of things.

    The summary is that I'm trying to refer to the array $adsad inside of the create_adlist function. When I take this script apart and only keep the necessary details (the content of the array, artificially inserted, as while as the call to the function and the print_r to the array inside the function) it works fine. I even built it all the way back up, piece by piece, and it kind of worked (it was hard to tell because of how it's set up -- at the very least, it didn't fail the same way.

    Here's the very pertinent section of code, taking out unrelated parts:
    
    print_r($adsad); // prints Array ( [1] => 1 [2] => 2 )  
    create_adlist(1,1); // calls function
    
    function create_adlist($setup,$location)
    {
    global $adsad;
    	
    print_r($adsad);
    }
    Code (markup):
    This code works perfectly by itself (I manually added $adsad[1] = 1; $adsad[2] = 2; to the top to give it something to print).

    Yet the larger script does not work because it does not print anything when it's called to print_r in the function code. Honestly, I'm lost as to how. It's a Wordpress project, so you'll see some of that language in here, but here's the whole code: (keep in mind that it does print out the array perfectly before calling the function at the bottom of the script -- the function is at the top of the script):
    <?
    // Create ad list
    function create_adlist($setup,$location)
    {
    global $adsad;
    	
    print_r($adsad);
    	
    echo "<select name=\"adset{$setup}adloc{$location}\"><option value=\"blank\">Please select an ad &nbsp;</option>
    
    <option value=\"blank\">Ad Units . . .</option>";
    
    	for ($round=1;$round<=count($adsad);$round++)
    	{
    	echo "<option value=$adsad[$round]>$adsad[$round]</option>";
    	}
    	
    
    echo "</select>";
    }
    
    // Organize current ads available
    $totalcodes = get_option("ao_totalcodes");
    
    $adsadcount = 0;
    $adslinkcount = 0;
    $adssearchcount = 0;
    
    	for ($nround=1;$nround<=$totalcodes;$nround++)
    	{
    	$temparray = "ads".get_option("ao_type$nround");
    	${"$temparray"."count"}++;
    	${$temparray}[${$temparray."count"}] = $nround;
    	}
    
    
    print_r($adsad);
    echo "<br>";
    print_r($adslink);
    echo "<br>";
    print_r($adssearch);
    echo "<br>";
    
    
    $totalrandoms = @get_option("ao_totalrandoms");
    
    if ($totalrandoms < 1){$totalrandoms = 1;}
    ?>
    
    
    <form method="post" action="options.php" name="setadlocations2">
    <?php wp_nonce_field('update-options'); ?>
    <input type="hidden" name="page_options" value="ao_totalrandoms,ao_staticul,ao_staticuc,ao_staticur,ao_staticml,ao_staticmc,ao_staticmr,ao_staticbl,ao_staticbc,ao_staticbr" />
    <input type="hidden" name="action" value="update" />
    
    <input type="hidden" name="ao_totalrandoms" value="<? echo $totalrandoms ?>">
    
    
    <script language="javascript">
    originaltotalrandoms = <? echo $totalrandoms; ?>;
    
    function toggleTab(tab)
    {
    currenttab = tab;
    
    	if (tab > document.setadlocations2.ao_totalrandoms.value)
    	{
    	document.setadlocations2.ao_totalrandoms.value = currenttab;
    	}
    
    document.getElementById("tab" + currenttab).style.display = 'block';
    
    
    	for (round=1;round<=originaltotalrandoms+1;round++)
    	{
    		if (round != currenttab)
    		{
    		document.getElementById("tab" + round).style.display = 'none';
    		
    		}
    	}
    
    }
    
    //toggletab(1);
    
    </script>
    
    
    <?
    $cellwidth = 100/($totalrandoms+1);
    
    echo "totalrandoms is $totalrandoms";
    
    for ($round=1;$round <= $totalrandoms+1;$round++)
    {
    echo "<div id=\"tab$round\"";
    	
    	if ($round != 1)
    	{
    	echo " style=\"display : none;\"";
    	}
    
    echo ">";
    
    
     
    echo "<table bgcolor=#cccccc width=100% border=2><tr>";
    
    	for ($mround=1;$mround<=$totalrandoms+1;$mround++)
    	{
    	echo "<td align=center width=\"$cellwidth%\">";
    	
    		if ($mround != $round)
    		{
    		echo "<a href=\"javascript:toggleTab('$mround');\" style=\"text-decoration : none\">Ad Set-up #$mround </a>";
    		}
    		
    		else
    		{
    		echo "Ad Set-up # $mround (Selected)";
    		}
    		
    		if ($mround == $totalrandoms+1)
    		{
    		echo " (Create New)";
    		}
    		
    	echo "</td>";
    	}
    
    echo "</tr></table><br>";
    
    print_r($adsad);
    echo "<br>";
    create_adlist(1,1);
    
    
    echo "</div>";
    }
    Code (markup):

     
    Submerged, Jul 21, 2009 IP