Two parameters - each()

I need to pass two parameters in each() method. How can i do that ?

The problem is this: I have a query that returns username, id, local, offer, and so on. But i need another function because i need to get the tags for each offer. The main problem is each offer can get many tags, so for each offer i need data from two php functions.

script

first function

   function get_posts($db, $start, $number_of_posts) {
           //code
            	return json_encode($posts);
            }

output:

  string '[{"username":"Altitude software","foto_oferta":"thumb\\/miniaturas\\/oferta\\/default_offer.jpg","actividades":"Some activities","id_oferta":77,"oferta":"Programador web" ...

second function

    function get_posts1($db, $start, $number_of_posts) {
           //code
            	return json_encode($posts1);
            }

output:

    string '[{"id_offer":77,"tags":["c++","JAVA"]},{"id_offer":76,"tags":["ajax","php"]},{"id_offer":75,"tags":["PHP","JAVA"]}]'

js

 var postHandler = function(postsJSON, postsJSON1) {
    				$.each(postsJSON, postsJSON1, function(i, post, post1) {//problem here- this didn't works. Only works something like $.each(postsJSON, function(i, post) {
    					var id = 'post-' + post.id_oferta;

    					$('<div></div>').addClass('post').attr('id',id)
    					.html('<div class="box_offers"><div class="rating_offer"></div><div class="post-title">'
    							+ post.oferta + '</div>  <div class="post-box"> <a class="oferta_val bold_username">'
    							+ post1.tags+ '</a></div><a id='+id+'hide class="post-more" >Tags</a><div class="logo_offer">')
    							.appendTo($('#posts'));

    					$('#'+id+'hide').click(function() {
    						$('.'+id+'hidden').slideToggle('fast');
    					});
    				});	
    			};

    postHandler(<?php echo get_posts($db, 0, $_SESSION['posts_start']); ?>, <?php echo get_posts1($db, 0, $_SESSION['posts_start']); ?>);

Why not simply get all the tags and merge them in your main posts array

That way you might end with something like:


[	{
		"username":"Altitude software",
		"foto_oferta":"thumb\\/miniaturas\\/oferta\\/default_offer.jpg",
		"actividades":"Some activities",
		"id_oferta":77,
		"oferta":"Programador web",
		"tags" : [ "tag 1", "tag 2", "etc." ]
	}
]

Would probably be a lot simpler :slight_smile:

hi

thanks. But how can i do that ?

A simply

$posts = array_merge($posts_, $post_tags); 

will not works.

In your get_posts() function, you can probably write the query differently to select the tags as well as the posts.

Alternatively you could run through a foreach to do the merge. Something like*:


// example array of posts
$posts = array(
    array( 'id' => 1, 'title' => 'Hello world' ),
    array( 'id' => 2, 'title' => 'Second post' ),
    array( 'id' => 4, 'title' => 'Fourth post' )
);

// assign indexes using the post ID
$tags = array(
    1 => array( 'tag 1.1', 'tag 1.2' ),
    4 => array( 'tag 4.1', 'tag 4.2' )
);

$i = 0;

$merged = array();

foreach ($posts as $post) {
    
    //check if there is something in the tags array with this post id
    if ( !empty( $tags[$post['id']] ) ) {
        
        // add a 'tags' item to the current post containing all the tags
        $post['tags'] = $tags[$post['id']];

    }

    //push the modified post onto a merged array
    array_push($merged, $post);
    
}

 printf("<pre>%s</pre>",print_r($merged,1));

(* disclaimer, I’m not a PHP guru /SIZE