Current location: Hot Scripts Forums » Programming Languages » PHP » Array passing between functions


Array passing between functions

Reply
  #1 (permalink)  
Old 08-24-03, 10:57 PM
alfreds's Avatar
alfreds alfreds is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Wink Variable passing between functions

Hi,

I have a little trouble with passing an array variable (say $file_names) from one function to another. Lets look in more details:

I have following funcitons:
- get_thumbs - to get the list of images in specific directory
- display_thumbs - to display them in ordered way
- display_large - to display larger image selected by user

During get_thumbs function goes through a specified directory and writes all filenames of images in an array.

The end of the get_thumbs states : return($file_names)
And the definition of function display_thumbs is following:

function display_thumbs ($file_names) {

Then, this functions displays all of the thumbnails, generate a link of a larger image for each of it, by awarding an id that corresponds to its place in array.

Afterwards, when user clicks on image, the display_large function is called, and the same array ($file_names) is passed along. BUT, the display_large function does not recieve it! At the same time, the id variable is passed!

Here is the way, the functions are called, basing on URL action variable:

PHP Code:

switch($_GET['action']) {

    
    case 
'thumbs':
        
get_thumbs();
        
display_thumbs($file_names);
    break;
    
    case 
'large':
       
display_large($file_names$_GET['id']);
    break;
    
     default:
       
select_gallery($db);

As i guess, than there might be something wrong with the break operations after each case.

I hope that my explanation is clear enough to get the idea. I would really appreciate your help, as i do not need any code sample, but rather an idea of what might be wrong!

Kaspars

Last edited by alfreds; 08-25-03 at 04:18 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-25-03, 06:29 AM
ermau's Avatar
ermau ermau is offline
Wannabe Coder
 
Join Date: Aug 2003
Location: Florida, USA
Posts: 240
Thanks: 0
Thanked 0 Times in 0 Posts
You're doing it wrong. Using a definition of function display_thumbs ($file_names) { will NOT pass the variable $file_names along, it will simply want a variable there, and then call it $file_names in your function. The best way to do what you want is by using globals.

example:

PHP Code:



function get_thumbs()
{
    global 
$file_names;

   
$file_names 'blah';
}

function 
display_thumbs()
{
   global 
$file_names;

   echo 
$file_names;
}

etc.. 
If you define a variable in a function and use global on it, it will become a global variable, so it can be used outside of functions anywhere. It can also be used in other functions, as long as global is called on it again.

HTH, Eric.
__________________
PHP / mySQL Developer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional Array Sorting Kage PHP 6 12-12-05 05:19 PM
move array element up or down Perry JavaScript 4 09-27-03 05:23 PM
passing array to hidden fields in form on next page? seala ASP 2 09-04-03 03:40 PM
asp: values in array not in order?? seala ASP 0 08-16-03 01:06 PM
call, array and display? irfaan PHP 5 08-09-03 06:41 PM


All times are GMT -5. The time now is 07:10 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.