Page 1 of 1

passing arrays from one function to an other

Posted: Thu Jul 03, 2003 5:52 pm
by labert_the_old
Hi There,

I was curious if you can pass an array as a global? It would make things allot easier for me. Ive had no trouble passing the $count variable as a global, but I just cant seem to figure out how to pass these arrays.

Sincerely,

Lambert the old

Code: Select all

<?php
function show_booked()
{
		
		
 $query = "SELECT reservation_date FROM  unit_availability WHERE  unit_id = 240";
 $result = mysql_query($query);
 if (!$result)
 {
  error_message(sql_error());
 } 
 $count = 0;	
 while ($row = mysql_fetch_array($result))
 {
 $booked = $row['reservation_date'];
 
 //get dates year month and day
		
 $a1[$count] = intval(substr($booked,0,4));
 $m1[$count]  = intval(substr($booked,5,3));
 $d1[$count] = intval(substr($booked,8,3));
 $month_length = strlen($m1[$count]);
 $day_length = strlen($d1[$count]);

  if($month_length==1)
  {
   
    $m1[$count] = "0".$m1[$count];
		
  }

  if($day_length==1)
  {

    $d1[$count] = "0".$d1[$count];

  }
		
 $count++;
		
 }		
		
	

}
	
class MyCalendar extends Calendar
{
    
  function getDateLink($day, $month, $year)
    {
       
   global $count,$a1,$d1,$m1;
    echo "*$a1*";
    $link = "";
    $i=0;

    while($i<=$count)
     {
        if ($day == $d1[$count])
         {
          	  $link = "view_units.php";
         }
         return $link;
      $count++;
      $i++;
     }
   
   }
}
?>

Posted: Thu Jul 03, 2003 7:27 pm
by m3rajk
you can a: explicitly declare the variable as global
or b: do something much safer and pass it in the function call. once you make something global it removes some degree of secutiry

Posted: Thu Jul 03, 2003 8:50 pm
by patrikG
regarding your date-parsing, have a look at thestrtotime()-function. It might save you about 20 lines of code :)

I would avoid to unnecessarily setting globals - they can easily make things much more complicated and create problems.

Why don't you call your function show_booked() from function getDateLink($day, $month, $year)? That would be much cleaner and keep all variables local.