passing arrays from one function to an other

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
labert_the_old
Forum Newbie
Posts: 5
Joined: Fri Jun 13, 2003 7:22 pm

passing arrays from one function to an other

Post 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++;
     }
   
   }
}
?>
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
Post Reply