SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

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

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

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

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


    js

    Code:
     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']); ?>);

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Why not simply get all the tags and merge them in your main posts array

    That way you might end with something like:
    Code javascript:
    [	{
    		"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
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    thanks. But how can i do that ?

    A simply
    Code:
    $posts = array_merge($posts_, $post_tags);
    will not works.

  4. #4
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    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*:

    Code PHP:
    // 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 )
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •