Link to home
Start Free TrialLog in
Avatar of Adam Mason
Adam MasonFlag for United States of America

asked on

Unable to return an array from a function

Sorry to post a large block of code, but I'm new to php (started last week) and am totally stumped.  What I have is a CSV file that needs to be scanned for a value stored in the second column, then use the data in the other columns to make a table, with the total size depending on the value in first column.  The variable "id" is passed in the URL and generated by the linking page.  the function works, and finds what its supposed to, but the return doesn't seem to work.  I think $data2 is empty, since it is using "case 0" in the switch block.

For example: a line from book2.csv (spaced added for ease of reading):
3 , il10 , IL-10 , oIMR0086 , GTG , oIMR0087 , GCC , endogenous IL-10 , oIMR0088 , CCT , 2 , mutant IL-10

I know I should probably use mySQL (or comparable), but I have 2 whole databases of maybe 20 lines each...

Below is the code I'm using right now; a couple of notes about this:
HTML has been stripped off for space
There are some lines that are strictly for debugging, and commented as such
I am using the install of Apache that comes with OS 10.2, with php enabled
I apologize for my mess.

<?php

$id1 = $_GET["id"] ;  //var "id" passed in URL

function scanarray ($file, $arg) {
echo "function called <hr>" ; //debug
  $file0 = fopen($file, 'r') ;
  while (($data0 = fgetcsv($file0, 2000)) != FALSE) {
    if ($data0[1] == $arg) {
      echo "$data0[2] primers found<hr>" ; //debug
    }
  }
  fclose($file0) ;
  return $data0;
}

$data2= scanarray('book2.csv',$id1) ;
switch ($data2[0]) {
  case 0: //no primers used
    echo "<p align=center> There are no primers available</p>" ;
    break;
  case 1: //multiple, start yanking primer sets
    for ($i ; $i<=$data2[0] ; $i++) {
      echo "<hr>" ;
    }
    break;
  case 2: //normal
    echo "<table border=\"5\" cellpadding=\"10\" align=center> \n <tr>" ; //establish table
    echo "<td> $data2[3] </td> <td> <code> $data2[4] </td> <td> with $data2[5] amplifies $data2[7] </tr> \n" ; //write first primer name, seq, and amp
    echo "<td> $data2[5] </td> <td> <code> $data2[6] </td> <td> with $data2[3] amplifies $data2[7] </tr> \n" ; //write 2nd name, seq, amp
    echo "</table> <br> <hr>" ; //end table
    break;
  case 3: // wt and null
    echo "<table border=\"5\" cellpadding=\"10\" align=center> \n <tr>" ; //establish table
    echo "<td> $data2[3] </td> <td> <code> $data2[4] </td> <td> with $data2[5] amplifies $data2[7] </tr> \n" ; //write first primer name, seq, and amp
    echo "<td> $data2[5] </td> <td> <code> $data2[6] </td> <td> with $data2[3] amplifies $data2[7] </tr> \n" ; //write 2nd name, seq, amp
    echo "<tr> <td> $data2[8] </td> <td> <code> $data2[9] </code> </td>" ; //write name, seq
    $tempval = $data2[10]*2+1 ; //reference
    echo "<td> with $data2[$tempval] amplifies $data2[11] </td> </tr> \n"; //write which and amp
    echo "</table> <br> <hr>" ; //end table
    break;
  default:
    echo "<b>Invalid argument</b>" ;
  } //end switch
?>
Avatar of andreif
andreif
Flag of Canada image

$data0 is local variable within while() {} structure, so it's empty after while ended

if you want to return $data0 as soon as you found it, try

 while (($data0 = fgetcsv($file0, 2000)) != FALSE) {
    if ($data0[1] == $arg) {
      echo "$data0[2] primers found<hr>" ; //debug
      fclose($file0) ;
      return $data0;
    }
  }
Avatar of Adam Mason

ASKER

Huh.... don't remember seeing that anywhere in the documentation.  Need new glasses I guess.

What I originally had was a search for all instances of $arg in the "database" since some things had more than 1 row to be displayed, but I've changed that now, so yours should work.

Follow-up question:

could I declare $data0 empty, eg:
   $data0 = array() ;
at the beginning of the function and let it be changed by the function and then returned at the end of the search?

So on all the loops (eg "for") variables declared inside them are local? Or is it only when you declare them as part of the loop as I did w/ $data0.
And as a further note... the only programming I've done before this was High School writing Pascal... so I probably write code funny.
Loop variables are local and function variables are local. So

function Blah () {

}
ASKER CERTIFIED SOLUTION
Avatar of andreif
andreif
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've been tinkering with it and I think I've got it now.
In the above example you called Blah, but that looks like a procedure call (with no arguments) to me, not a "true" function... Am I right?
well, in Pascal it used to be some difference between function and procedure - I remember that :)
But not in PHP.
You can have function with or without arguments, it may return value and may not.
Even when it returns something, you can ignore that and call it as procedure, you just miss the returned value then
Great... thanks for the quick help.  I'll probably need more later, but that pretty much sums it up for me.