TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Returning an array from a function (http://www.talkphp.com/absolute-beginners/4736-returning-array-function.html)

captainmerton 07-14-2009 07:42 PM

Returning an array from a function
 
I want to create a function that goes away and collects data from a database that will be used in a form drop down. I assume the best way to do this is an array as the number of rows of data returned will vary. Essentially i want to build an array as i read th rows then return the array from the function. How do I go about doing this and how do I handle the data when calling the function. the code I have inside the function (PS i havent included all the function code just the main part of it):

PHP Code:

    function getList($data1,$data2) {

        
$query = ("SELECT blahdata from blahtable
        WHERE blahcolumn == "
$data1");
          
        
$result = mysql_query($query) OR die("Cannot perform getList query!");

        if (mysql_num_rows(
$result) == 0) {
            return 0;
        } else {
            
$arr=array();
            
$counter=0;
            while(
$row = mysql_fetch_array($result))
            {
                
$arr[$counter]=$row['blahdata'];
                
$counter=$counter+1;
            }
            return 
$arr;            
        }
            
    } 

I find if I call the function using a print statement I get "0" when there is no rows as expected but "array" when there ares rows. Do i need to explode the array?

Village Idiot 07-14-2009 08:08 PM

Edit: It looks like I completely misunderstood your post. Arrays can not be directly printed, you have to grab the data and individually print it. Look into foreach.

captainmerton 07-15-2009 04:57 PM

That looks like what i need. many thanks.

rguy84 07-15-2009 06:37 PM

why can't you do:
PHP Code:

<select style="margin-right:0.25em" name="cmbModPCProg">
        <?php
        
echo "<option>Select a Program</option>\n".
        
"\t\t<option>----------</option>\n";
        
$qGetProg "SELECT progID, progName FROM tblProg;";
        if (
$rGetProg $mysqli->query($qGetProg)){
            while(
$row $rGetProg->fetch_assoc()){
                
$rGetPID $row['progID'];
                
$rGetPN $row['progName'];
                echo 
"\t\t<option value=\"".$rGetPID."\">".$rGetPN."</option>\n";
                }
        
$rGetProg->close();
        }
        
?>
    </select>

(I made this years ago when I was learning php, so it may not be the cleanest)

captainmerton 07-15-2009 08:45 PM

My intention was to create the function to return an array which may or may not be used to produce a form drop down list. I've created another function to accept in the array and reformat it as a form drop down list. If that makes sense. But thanks for the help.


All times are GMT. The time now is 05:52 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0