Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Class Array

Options
  • 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 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 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