Welcome Guest, Not a member yet? Register   Sign In
Loop through multiple array concat and sort by value
#1

(This post was last modified: 01-06-2016, 02:51 PM by sebastianvirlan.)

I have a array that describe a database table and it's relations.

Assume we have a products table with following fields:
  • product_id
    column1
    column2
    column_three

and based on the table I create a object with the details.

PHP Code:
Array
(
 [
pk] => product_id
 
[Fields] => Array
    (
        [
column1] => stdClass Object
            
(
                [
Position] => 1
                
[Label] => Column1
            
)

        [
column2] => stdClass Object
            
(
                [
Position] => 3
                
[Label] => Column2
            
)

        [
column_three] => stdClass Object
            
(
                [
Position] => 4
                
[Label] => Column Three
            
)
 
//and here are the relations
 
[Relations] => Array
    (
        
//the field category_name from category model
        
[category_name] => stdClass Object
            
(
                [
Position] => 2
                
[Label] => Category Name
                
[Model] => category
                
[Relation] => belongs_to
                
[PK] => id_categorie
            
)

        [
image_name] => stdClass Object
            
(
                [
Position] => 5
                
[Label] => Image Name
                
[Model] => image
                
[Relation] => has_one
                
[PK] => image_id
                
[Through] => product_id
            
)

    ) 


How can I loop this array in view so I can get the labels to create a table with the following thead ordered by position?

Column1 | Category Name | Column2 | Column Three | Image Name

When merging the arrays must check if a field from main table has same name with from relation table and if yes add a suffix.
Reply
#2

I would suggest that you build your page array in a controller or library and only pass the data to your page that your view actually requires and in a format fit for that view. This will help you to avoid complex array manipulation within a view.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB