Alternative to global variables?
Moderator: General Moderators
Alternative to global variables?
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!
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!
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Alternative to global variables?
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?
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)
{....
}
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?
How about you post what you have so we can propose solutions. Please use the code tags.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Alternative to global variables?
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;(#10850)
Re: Alternative to global variables?
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?
When the user submits the form, you need to repopulate the arrays, They are not persistent.
Re: Alternative to global variables?
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?
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?
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?
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?
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?
Sorry, I meant in a form in either the option value= or select name= .