Page 1 of 1

Alternative to global variables?

Posted: Sat Mar 14, 2009 8:01 pm
by katkat
I have a function that builds a list of information (in an array) that I need in another function. I guess "global" variables are not recommended.

Does anyone have a suggestion as to how I should make the array information available in other functions?

Am a newbie so I will need details. Thanks!

Re: Alternative to global variables?

Posted: Sat Mar 14, 2009 8:04 pm
by John Cartwright
Pass the return value of the first function to the second function. I.e.,

Code: Select all

 
function foo($value) {
   return $value + 1;
}
 
function bar($value) {
   return $value + 10;
}
 
$value = 0;
$newvalue = foo($value); // $newvalue = 1
$newvalue2 = bar($newvalue); // $newvalue2 = 11
 
 

Re: Alternative to global variables?

Posted: Sat Mar 14, 2009 8:24 pm
by katkat
Am a newbie...

I am not sure what you are suggesting.

Are you suggesting that I pass the $array in the functions list like this?

function ($array)
{....
}

Re: Alternative to global variables?

Posted: Sat Mar 14, 2009 8:29 pm
by Benjamin
How about you post what you have so we can propose solutions. Please use the code tags.

Re: Alternative to global variables?

Posted: Sat Mar 14, 2009 8:29 pm
by Christopher
It does not matter what kind of data the variable contains -- you pass it the same way. So you could also pass an array:

Code: Select all

function foo($array) {
   return $array[0] + 1;
}
 
function bar($array) {
   return $array[0] + 10;
}
 
$array[0] = 0;
$newvalue = foo($array); // $newvalue[0] = 1;
$newarray2 = bar($newarray); // $newvalue2[0] = 11;

Re: Alternative to global variables?

Posted: Sat Mar 14, 2009 8:36 pm
by katkat
I am reading a file and building a drop down box with concatenated first and last names. I need to know the id, first name, and last name that the user selects. So I am trying to capture the names separately in an array so I can use them in another function. I am using the id as thearrays indexes so I can keep all three fields associated.

Code: Select all

<select name='runnerselect'><option>Select a runner</option>";
 
while ($result_row = mysql_fetch_array($result, MYSQL_ASSOC)) 
 {
    echo "<option value=" .$result_row['runnerid']. ">" . $result_row['firstname'] . " " .                $result_row['lastname'] . "</option>";
 
   $ct=$result_row['runnerid'];
   $fname[$ct]=$result_row['firstname'];
   $lname[$ct]=$result_row['lastname'];      
 }
 echo "</select>";  

Re: Alternative to global variables?

Posted: Sat Mar 14, 2009 8:57 pm
by Benjamin
When the user submits the form, you need to repopulate the arrays, They are not persistent.

Re: Alternative to global variables?

Posted: Sat Mar 14, 2009 9:10 pm
by katkat
If I build a separate function that only populates the table without any user input, would my array information be retained?

Re: Alternative to global variables?

Posted: Sat Mar 14, 2009 9:26 pm
by Benjamin
Here's a procedural approach:

Code: Select all

 
// create the array
$runners = array();
while ($row = mysql_fetch_assoc($resource)) {
    $runners[$row['id']] = array(
        'fname' = $row['fname'],
        'lname' = $row['lname'],
    );
}
 
// if the user submitted a form, process it
if (!empty($_POST['do']) && $_POST['do'] == 'process') {
    // $x = $runners[$_POST['runner']];
    // echo $x['fname'] . ' ' . $x['lname'];
}
 
// drop out of php and display the form
?>
 
<form action="#" name="foo" method="post">
  <select name="runner">
    <?php foreach ($runners as $id => $name) { ?>
      <option value="<?php echo $id; ?>"><?php echo implode(' ', $name); ?></option>
    <?php } ?>
  </select>
  <input type="hidden" name="do" value="process" />
  <input type="submit" value="Save" />
</form>
 
 
 

Re: Alternative to global variables?

Posted: Sat Mar 14, 2009 9:47 pm
by katkat
Got it.
You write beautiful code!
Thanks so much for your help.

By the way...is there any way to capture 3 name= values from a select statement?

For example, I display a dropdown box with first and last names. When the user selects the name, I capture the record id. Is there any way to capture the record it, first name, and last name from that single select statement?

Re: Alternative to global variables?

Posted: Sat Mar 14, 2009 9:49 pm
by Benjamin
I'm not sure I completely understand you.

Code: Select all

 
SELECT id, fname, lname FROM TABLE WHERE id = n
 

Re: Alternative to global variables?

Posted: Sat Mar 14, 2009 9:55 pm
by katkat
Sorry, I meant in a form in either the option value= or select name= .