Jump to content

Passing Arraya IN and Out of Functions


venturemc

Recommended Posts

Another head banger for this newb. (Seems that everything is a head banger with this language!)

 

I've created a function that creates an array. WHen I try to pass the array back to the calling script I get every result but an array that contains the array in the function. I've tried passing in a variable as a reference and tried not passing a variable in at all, letting the function return the variable. I've tried messing with serializing/unserializing and even breifly nessed with using a $_SESSION variable. Nothing seems to give me results.

 

Here's the parent php file as it is right now:

 

<?php
include_once("mc_lib.php");

$db = "sampledb";
$tbl = "sample";
//$rda = array('index', 'fldname','fldtype','flag','length','inval','outval');

$rda = array_values(unserialize(rd_bld_array($db, $tbl, $rda)));

foreach($rda as $rec => $r){
echo 'x' . $r['index'] . ' ' . $r['fldname'] . ' ' . $r['fldtype'] . ' ' . $r['flag'] . ' ' . $r['length'] . '<br>';
}
$html = rd_bld_form($rda, $html);

echo $html;

?>

Here's the function in question: 
<?php
function rd_bld_array($db, $tbl){
// $db = database name, $tbl = table name, $rda = recdata array

// init variables
// flag to indicate that the loop has past the index indicator
$gdata = false;
$ctr = 1;
$li = 'login';
$pw = 'password';
$dbloc = 'xxx.xxx.x.xxx';

$dbc = mysql_connect($dbloc, $li, $pw);
mysql_select_db($db,$dbc);

$q = 'SELECT * FROM ' . $tbl;

$result = mysql_query($q, $dbc) or die('Error' . mysql_error($dbc));

$fields = mysql_num_fields($result);

$rd = array('index', 'fldname','fldtype','flag','length','inval','outval');

             for ($ctr = 1; $ctr < $fields; $ctr++){
	//if ($gdata == true) {
	$rd['index'] = $ctr;
   		$rd['fldname'] = mysql_field_name($result, $ctr);  		
	$rd['fldtype'] = mysql_field_type($result, $ctr);
	$rd['flag'] = mysql_field_flags($result, $ctr);
	$rd['length'] = mysql_field_len($result, $ctr);
            $rd['inval'] = '';
	$rd['outval'] = '';	
	//}	     

                          //if (mysql_field_name($result, $ctr) == 'rk') {
    		//$gdata = true;
	//	    }
echo $ctr . ' ' . $rd['index'] . ' ' . $rd['fldname'] . ' ' . $rd['fldtype'] . ' ' . $rd['flag'] . ' ' . $rd['length'] . '<br>';		
}
mysql_free_result($result);
mysql_close($dbc);
$rd = serialize($rd);
return $rd;
}

Here's the results back from the debug echoing (the 'x' lines are after the array has been passed back to the parent page)

?>


1 1 name string not_null binary 50
2 2 occupation string not_null binary 50
3 3 age int not_null 20
x 
xa a a a a
xi i i i i
xn n n n n
x 
x 
x 

 

The first three lines are what I expect. You can see that not only am I getting giberish back, I'm getting abunch of extra cells. I expect to be seeing:

x 1 name string not_null binary 50

x 2 occupation string not_null binary 50

x 3 age int not_null 20

 

after the return of the array.

 

Can somebody point this idiot in the right direction?

 

 

Link to comment
Share on other sites

$rd = array('index', 'fldname','fldtype','flag','length','inval','outval'); creates an array that looks like this -

 

Array
(
    [0] => index
    [1] => fldname
    [2] => fldtype
    [3] => flag
    [4] => length
    [5] => inval
    [6] => outval
)

 

And when your code adds values like - $rd['index'] = $ctr; ...

 

You end up with an array that looks like this -

 

Array
(
    [0] => index
    [1] => fldname
    [2] => fldtype
    [3] => flag
    [4] => length
    [5] => inval
    [6] => outval
    [index] => 1
    [fldname] => 
    [fldtype] => 
    [flag] => 
    [length] => 
    [inval] => 
    [outval] => 
)

 

Your for() loop also just keeps setting the same elements in $rd over and over but with each succeeding set of values in each iteration through the loop.

 

 

Link to comment
Share on other sites

try

<?php
function rd_bld_array($db, $tbl){
// $db = database name, $tbl = table name, $rda = recdata array

// init variables
// flag to indicate that the loop has past the index indicator
$gdata = false;
$ctr = 1;
$li = 'login';
$pw = 'password';
$dbloc = 'xxx.xxx.x.xxx';
$out = array();
$dbc = mysql_connect($dbloc, $li, $pw);
mysql_select_db($db,$dbc);

$q = 'SELECT * FROM ' . $tbl;

$result = mysql_query($q, $dbc) or die('Error' . mysql_error($dbc));

$fields = mysql_num_fields($result);

$rd = array('index', 'fldname','fldtype','flag','length','inval','outval');

             for ($ctr = 1; $ctr < $fields; $ctr++){
	//if ($gdata == true) {
	$rd['index'] = $ctr;
   		$rd['fldname'] = mysql_field_name($result, $ctr);  		
	$rd['fldtype'] = mysql_field_type($result, $ctr);
	$rd['flag'] = mysql_field_flags($result, $ctr);
	$rd['length'] = mysql_field_len($result, $ctr);
            $rd['inval'] = '';
	$rd['outval'] = '';	
	//}	     

                          //if (mysql_field_name($result, $ctr) == 'rk') {
    		//$gdata = true;
	//	    }
echo $ctr . ' ' . $rd['index'] . ' ' . $rd['fldname'] . ' ' . $rd['fldtype'] . ' ' . $rd['flag'] . ' ' . $rd['length'] . '<br>';	
           $out[] = $rd;	
}
mysql_free_result($result);
mysql_close($dbc);
//$rd = serialize($rd);
           $out = serialize($out);
return $out;
}

Link to comment
Share on other sites

That depends. Are you trying to make an array of strings or an array of arrays?

 

I'm building a multi-dimensional array which is where I went wrong with getting the array set up properly.

 

In the spirit of sharing learning experiences....

I tricked myself into believeing that the array was populating as I expected when I did my echo statement inside the loop. Once it was pointed out that I was only repopulating the same array over and over (which explained one of the function's returns I was getting), I built a loop after the collection loop to echo my array before passing it back to the parent script and saw exactly what PF told me. Seeing that, I banged my head a few times before realizing what I was doing wrong. Once I built it as a multi dimensional array, I got the passing of the array figured out. I initially tried to pass in an array and have it modified by the function, but I couldn't get that working, but I was able to get the array back out of the function. The corrected code is below.

 

parent document calling function


<?php
//.....

$rda = array(array('index', 'fldname','fldtype','flag','length','inval','outval'));

$rda = array_values(rd_bld_array($db, $tbl));

//foreach($rda as $rec => $r){
//echo 'x' . $r['index'] . ' ' . $r['fldname'] . ' ' . $r['fldtype'] . ' ' . $r['flag'] . ' ' . $r['length'] . '<br>';
//}

 

 

function to build array....

 

	
<?php 
//..........

             $result = mysql_query($q, $dbc) or die('Error' . mysql_error($dbc));

$fields = mysql_num_fields($result);

//$rd = array('index', 'fldname','fldtype','flag','length','inval','outval');
for ($ctr = 1; $ctr < $fields; $ctr++){
	//if ($gdata == true) {
	$rd[$ctr]['index'] = $ctr;
   		$rd[$ctr]['fldname'] = mysql_field_name($result, $ctr);  		
	$rd[$ctr]['fldtype'] = mysql_field_type($result, $ctr);
	$rd[$ctr]['flag'] = mysql_field_flags($result, $ctr);
	$rd[$ctr]['length'] = mysql_field_len($result, $ctr);
                	$rd[$ctr]['inval'] = '';
		$rd[$ctr]['outval'] = '';	
	//}	     
	if (mysql_field_name($result, $ctr) == 'rk') {
    			$gdata = true;
		    }
//vvvvvv this is the debug echo that tricked me   vvvvvvvvv

//echo $ctr . ' ' . $rd[$ctr]['index'] . ' ' . $rd[$ctr]['fldname'] . ' ' . $rd[$ctr]['fldtype'] . ' ' . $rd[$ctr]['flag'] . ' ' . $rd['length'] . '<br>';		
}


//vvvvvvvvv this is the revised debug echo vvvvvvvvvvvvvvvv

//foreach($rd as $rec => $r){
//echo 'v' . $r['index'] . ' ' . $r['fldname'] . ' ' . $r['fldtype'] . ' ' . $r['flag'] . ' ' . $r['length'] . '<br>';
//}
mysql_free_result($result);
mysql_close($dbc);
$rd = array_values($rd);
return $rd;

 

Thanks for the prod......

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.