TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-14-2009, 07:42 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default 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?
captainmerton is offline  
Reply With Quote
Old 07-14-2009, 08:08 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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.
__________________

Village Idiot is offline  
Reply With Quote
Old 07-15-2009, 04:57 PM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

That looks like what i need. many thanks.
captainmerton is offline  
Reply With Quote
Old 07-15-2009, 06:37 PM   #4 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

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)
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 07-15-2009, 08:45 PM   #5 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

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.
captainmerton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
feedback on my class please frostyboy33 Advanced PHP Programming 7 10-22-2012 09:12 AM
How to delete array element in function benton General 4 04-02-2009 01:12 AM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 04:26 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design