Alternative to global variables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

Alternative to global variables?

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Alternative to global variables?

Post 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
 
 
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

Re: Alternative to global variables?

Post 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)
{....
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Alternative to global variables?

Post by Benjamin »

How about you post what you have so we can propose solutions. Please use the code tags.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Alternative to global variables?

Post 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;
(#10850)
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

Re: Alternative to global variables?

Post 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>";  
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Alternative to global variables?

Post by Benjamin »

When the user submits the form, you need to repopulate the arrays, They are not persistent.
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

Re: Alternative to global variables?

Post by katkat »

If I build a separate function that only populates the table without any user input, would my array information be retained?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Alternative to global variables?

Post 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>
 
 
 
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

Re: Alternative to global variables?

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Alternative to global variables?

Post by Benjamin »

I'm not sure I completely understand you.

Code: Select all

 
SELECT id, fname, lname FROM TABLE WHERE id = n
 
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

Re: Alternative to global variables?

Post by katkat »

Sorry, I meant in a form in either the option value= or select name= .
Post Reply