Results 1 to 2 of 2

Thread: Functions

  1. #1

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Functions

    In Visual Basic I would do the following...

    VB Code:
    1. Public Function SetName(strFirstname as string, strLastname as string) as string
    2.  
    3. SetName = strFirstname & " " & strLastname
    4.  
    5. End Function
    6.  
    7. 'Usage
    8.  
    9. Msgbox SetName("Ben", "Everard")

    The point being i would be able to set the function as a variable, so the value returned from the function can be used immediatly upon use.

    The following PHP function (my first by the way ) queries my MySQL database and recieves the last 5 entries into the news table.

    PHP Code:
    function sidebar_news($function_db, $function_limit, $function_directory_height) {

        $global_news_content = mysql_query("SELECT * FROM tblNews ORDER BY date DESC LIMIT " . $function_limit, $function_db);
                
            while ($global_news_content_row = mysql_fetch_row($global_news_content)) { 
                ?>
    <a href='<?php echo($function_directory_height); ?>/news.php'><?php echo($global_news_content_row[0]); ?></a><br>
    <?php
            
    }
    }
    Now currently it is being called and the function is simply writing the HTML code on each loop of the database query, what I need to happen is on each loop the HTML code to be added to a variable, and then after the loop has complete, return the variable to the function, just like the VB example did before.

    Cheers my lovers

    ILMV 1.0.1beta

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Functions

    You need to use an array. In the while loop add the $global_news_content_row in the while loop to an array and retur nthe array. Much as you would in VB:
    Code:
    $return = array();
    
    while ($global_news_content_row = mysql_fetch_row($global_news_content)) {
        $return[] = $global_news_content_row ; // cuases a new element to be pushed on to the end of the array
    }
    
    return $return;
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width