Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP Class Array

  • 22-10-2010 11:26PM
    #1
    Closed Accounts Posts: 2,828 ✭✭✭


    I am trying to figure out how to return an array from within a class, for example

    <?php
     
    class products{
     
    function get_description(){
     //mysql query here
     return $result
    }
     
    }
     
    ?>
    

    Within the get_description function will be a mysql query which will return a row which I want to be able to access outside of the class script, there is probably a much easier way to do this


Comments

  • Closed Accounts Posts: 4,001 ✭✭✭Mr. Loverman


    You could pass in the array's pointer. Google pass by reference.


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    justryan wrote: »
    I am trying to figure out how to return an array from within a class, for example

    <?php
     
    class products{
     
    function get_description(){
     //mysql query here
     return $result
    }
     
    }
     
    ?>
    

    Within the get_description function will be a mysql query which will return a row which I want to be able to access outside of the class script, there is probably a much easier way to do this

    You could create a class that represents the values in the row, then populate an instance of that class and return it. Seems like better OOP in my opinion.


  • Registered Users, Registered Users 2 Posts: 67 ✭✭The Gibmiester


    I'm no expert but is something like this what your looking for
    <?php
    //Products.php
    
    class Products {
        
        function db_connect()
          {
            $connection = mysql_pconnect('localhost', 'root', 'password');
            mysql_set_charset('utf8');
            if(!$connection)
            {
                return false;
            }
            if(!mysql_select_db('my_db'))
            {
                return false;
            }
                return  $connection;
          }
        
        function db_result_to_array($result)
        {
          $this->db_connect();
          $res_array = array();
            
            for ($count=0;  $row = mysql_fetch_array($result) ; $count++)
            {
              $res_array[$count] = $row;
            }
            
            return $res_array;
        }
    }
    
    ?>
    
    <?php
    //test.php
    
         include('Products.php');
         $my_products = new Products();
         $connection = $my_products->db_connect();
         $description = mysql_query("SELECT field1, field2 FROM TABLE", $connection);
         $rows = $my_products->db_result_to_array($description);
         foreach($rows as $row):
             echo $row['field1']." ".$row['field2'];
         endforeach; 
    ?>
    


Advertisement