mwl707 Posted August 23, 2009 Share Posted August 23, 2009 I have a function called equipment picker which in turn calls another function called screens date range . At the end of the screens date range function I end up with a variable called max_screens which has a value (I included an echo so i can see this to be true). When the function returns to the original function (equiment picker) the variable has cleared. I declared it as a global which I was I thought was all that was needed ?. Can anyone help me please ?? function equipment_picker($st,$ed) { // call screens date range function (Returns the maximum number of screens available for the given dates ) // creates drop down box with the max number of screens available screens_date_range("i100s",$st,$ed) ; echo "<h4> ep - ms/ $max_screen </h4> " ; $result = mysql_query("SELECT * FROM stock") ; while($row = mysql_fetch_array($result)) { // for each list of stock $item = $row['item'] ; $disc = $row['description'] ; $result1 = mysql_query("SELECT * FROM $item" ); $num_items = mysql_num_rows($result1); echo "<label><b> $disc </b>" ; echo "<select name='$item" . "s' " . " id='i100s'' >" ; for ( $i=0; $i<=$max_screen ; $i++) { echo "<option>$i</option>" ; } ; // end of for echo " </select> </label>" ; } ; // end of while } ; // end of func ================================================= function screens_date_range($ss,$st,$ed) { // date range function (Returns the maximum number of screens available for the given dates ) // use as follows // screens_date_range('screensize' , 'start date' , 'end date') // // result is in the global variable $max_screen // // assign globals global $totaldays ; global $available_screens ; global $max_screen ; // Make a MySQL Connection include '../database_sql/dbconnect.php' ; // conditions sent to this func $screen_size = $ss ; // Total Number of specific screen in the table // i100 and i100s issue IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII // get the total of all i100 $result = mysql_query("SELECT * FROM i100") ; $total_kit = mysql_num_rows($result); $st_year = substr("$st", 0, 4); // alocate the start day,month,year $st_month = substr("$st", 5, 2); $st_day = substr("$st", 8, 2); $end_year = substr("$ed", 0, 4); // alocate the start day,month,year $end_month = substr("$ed", 5, 2); $end_day = substr("$ed", 8, 2); $startDate = date("D-d-M-Y", mktime(0, 0, 0, $st_month, $st_day, $st_year)); $endDate = date("D-d-M-Y", mktime(0, 0, 0, $end_month, $end_day, $end_year)); // Date loop ++++++++++++++++++++++++++++++++++++++++++++ $i=0; while ($temp <> $endDate) { $totaldays ++ ; $year = date("Y", mktime(0, 0, 0, $st_month, $st_day+$i, $st_year)); $month = date("m", mktime(0, 0, 0, $st_month, $st_day+$i, $st_year)); $day= date("d", mktime(0, 0, 0, $st_month, $st_day+$i, $st_year)); $temp = date("D-d-M-Y", mktime(0, 0, 0, $st_month, $st_day+$i, $st_year)); $i++; $sysdate = "$year-$month-$day" ; $condition = "jobstatus = 'confirmed' AND startdate <= '$sysdate' AND '$sysdate' <= enddate "; // Number of screens for date $result = mysql_query("SELECT SUM($screen_size) FROM event WHERE $condition") ; $row = mysql_fetch_array($result) ; // Should only be one result !! $num_screens_today = $row["SUM($screen_size)"] ; $available_screens_today = $total_kit - $num_screens_today ; $vari = $vari . $available_screens_today . "," ; }; // ========== END WHILE ==================== $vari = $vari . $total_kit ; $max_screen = min(explode ("," , $vari )) ; echo "<h4> sdr - ms/ $max_screen </h4> " ; // close the dbase 'goodbye' include "../database_sql/dbclose.php" ; // end of function } ; Link to comment https://forums.phpfreaks.com/topic/171510-global-variable/ Share on other sites More sharing options...
DavidAM Posted August 23, 2009 Share Posted August 23, 2009 You need to declare it as global in equipment_picker as well. Link to comment https://forums.phpfreaks.com/topic/171510-global-variable/#findComment-904434 Share on other sites More sharing options...
PFMaBiSmAd Posted August 23, 2009 Share Posted August 23, 2009 You need to pass all the values into functions as parameters in the function call and return values from functions to avoid this type of problem. The only way that the global keyword works is if the variable(s) is(are) defined in the main program. You cannot use it to make a variable that is defined in one function available inside of another function. If you are now going to go and define those variables in your main program just to get them to be available between your two functions, ask yourself this, am I making unreadable spaghetti code that will be hard to reuse in the future because it is all tied to having and using specific variable names in the main program and in two or more function definitions? Please use functions as they were intended and don't use the global keyword to being values into functions. Link to comment https://forums.phpfreaks.com/topic/171510-global-variable/#findComment-904443 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.