Welcome Guest, Not a member yet? Register   Sign In
Array Help
#1

[eluser]Clauddiu[/eluser]
Hey guys, I have a problem with some arrays, I'm new to php and codeigniter.
I want to have a function in my model that can simulate a relation between some tables.

So I have this array
Code:
$_users = array(
array(
  'id' => 1,
  'name' => 'john',
  'pass' => 'john1'
),

array(
  'id' => 2,
  'name' => 'jack',
  'pass' => 'jack_password'
)
);


$_posts = array(
array(
  'id' => 1,
  'user_id' => '1',
  'title' => 'this is a nice title',
  'body' => 'this is a nice body'
),

array(
  'id' => 2,
  'user_id' => '1',
  'title' => 'very cool post',
  'body' => 'This is the body for this post'
)
);

$_comments = array(
array(
  'id' => 1,
  'post_id' => '2',
  'comment' => 'this is a cool comment'
),

array(
  'id' => 2,
  'user_id' => '2',
  'comment' => 'comments comeeeeeents'
)
);

$_with = array(
'posts',
'comments'
);

and I want to make it look like this:
Code:
$_users = array(
array(
  'id' => 1,
  'name' => 'john',
  'pass' => 'john1',
  'posts' => array(
   array(
    'id' => 1,
    'user_id' => '1',
    'title' => 'this is a nice title',
    'body' => 'this is a nice body'
   ),

   array(
    'id' => 2,
    'user_id' => '1',
    'title' => 'very cool post',
    'body' => 'This is the body for this post',
    'comments' => array(
     array(
      'id' => 1,
      'post_id' => '2',
      'comment' => 'this is a cool comment'
     ),

     array(
      'id' => 2,
      'user_id' => '2',
      'comment' => 'comments comeeeeeents'
     )
    )
   )
  )
),

array(
  'id' => 2,
  'name' => 'jack',
  'pass' => 'jack_password'
)
);

I can't find a way of doing this, I always get duplicates because I have to check the with array, the with array will tell me what tables will be related.

Can anyone please give me a hint how to do this, please?

Regards,
Claudiu
#2

[eluser]jmadsen[/eluser]
This is a case where I think it is best to ask, "Why are you doing this?" before answering, because it seems like using sql joins in your query or something similar would be much better.

I would answer that question before jumping on any response people might give about how to do a bunch of iterations. Php-wise, it is simple, but probably a really bad solution.

I have a feeling you might be dealing with this issue? http://www.codebyjeff.com/blog/2012/08/n...rscore-php
#3

[eluser]Clauddiu[/eluser]
yes
#4

[eluser]Clauddiu[/eluser]
Yes, I use a database to make this happen like this:

Code:
$test = db::table('admin')->relate(
   array(

    'posts'=> array(
     'where'    => array('id', '=', 2),
     'order_by' => array('id', 'desc')
    ),

    'comments'=> array(
     #'where'    => array('id', '=', 2),
     'order_by' => array('id', 'desc')
    )
   )
  )->get();

I'm using Idiorm with some custom methods, anyways all you see is based on $_with array which comes from related method, right there I get the related tables.

So what I need is how the heck to make this recursive work without having duplicates, or any a better solution, please let me know.

Regards,
Claudiu
#5

[eluser]jmadsen[/eluser]
I've never worked with Idiorm, but if it is giving you a single result set that you need reworked like you described, that blog post I put up is exactly what you want.

Unfortunately my host just went down for an hour or so of scheduled maintenance :-(

It is using Underscore.php's groupby function: http://brianhaveri.github.com/Underscore.php/ -- if you can wait an hour, you can see exactly how to set it up in CodeIgniter, or just have a go from the docs

#6

[eluser]Clauddiu[/eluser]
Sure, no problem at all. I'll wait, lemme know when I can read your article. Thank you.
#7

[eluser]jmadsen[/eluser]
actually, google was nice enough to cache it

http://webcache.googleusercontent.com/se...=firefox-a




Theme © iAndrew 2016 - Forum software by © MyBB