Jump to content

Help in foreach inside foreach.


lopes_andre

Recommended Posts

Hi, I need help for listing this in a RSS feed...

 

The feed will contain information about The blog-post and the tags associated to the post. I have the information from two tables,

One with the posts and the other with the tags.

 

TABLE "POSTS"

id_post | n_post | description

1      | aaaa  | aaa aaa aaa aaa

2      | bbbb  | bbb bbb bbb bbb

3      | cccc  | ccc ccc ccc ccc

 

TABLE "TAGS"

id_post|id_ordinal|n_tag

1      | 1        | tag1

1      | 2        | tag2

1      | 3        | tag3

2      | 1        | tag1

2      | 2        | tag2

3      | 1        | tag1

3      | 2        | tag2

3      | 3        | tag3

 

I have the idea that I must to do two foreach cicles. But I'am with doubts in the query(SQL) that I need to do...

 

<?php foreach($bloposts as $post) : ?>
<?php foreach($posttags as $tags) : ?>
	<?php echo $tags->n_tag; ?>
<?php endforeach; ?>

<?php echo $post->n_post . '-' . $post->description; ?>

<?php endforeach; ?>

 

Do tou have any ideas on how to do this?

 

 

Best Regards,

Link to comment
https://forums.phpfreaks.com/topic/190917-help-in-foreach-inside-foreach/
Share on other sites

If both tables have the same number of rows then just do a foreach loop with a counter and use the counter to extract the information from the second array at the right key. You could even just use the key from the first array on the second if they are numbered the same. This is all assuming that you are extracting the tables into two seperate arrays.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.