• Hi, working on a plugin which has a shortcode feature, got a problem with the output though. This is my function:

    function the_output($atts){
     extract(shortcode_atts(array(
        "id" => '1'
        ), $atts));
    
     $poll_sel_data = get_poll_objects($id);
    
    	foreach($poll_sel_data as $poll_sel_item){
    	    $poll_sel_output = "<ul>";
    	    $poll_sel_output .= "<li>";
    	    $poll_sel_output .= $poll_sel_item->object_title;
    	    $poll_sel_output .= "</li>";
    	    $poll_sel_output .= "<ul>";
    	    $poll_sel_output .= "<li>";
    	    $poll_sel_output .= $poll_sel_item->secondary_text;
    	    $poll_sel_output .= "</li>";
    	    $poll_sel_output .= "</ul>";
    	    $poll_sel_output .= "</ul>";
    	}
    
    	return $poll_sel_output;
    
    }

    And the get_poll_objects function:

    function get_poll_objects($current_poll_id){
          global $wpdb;
    	$table_name = $wpdb->prefix . "poll_objects";
    	$poll_sel = $wpdb->get_results(
    	    "SELECT * FROM {$table_name} WHERE poll_id={$current_poll_id}"
    	);
    
    	return $poll_sel;
    }

    When I use the shortcode it should output all the objects which has the current poll_id. However, it only gives me the last added item with that poll_id.

    Anyone who can spot the problem?

    Regards.

Viewing 5 replies - 1 through 5 (of 5 total)
  • edited

    Your problem should be here:

    $poll_sel_output = “

      “;

    change = to

    .=

    otherwise you’ll be overwriting your previous data with each loop.

    Grrr… that didn’t paste correctly.

    foreach($poll_sel_data as $poll_sel_item){
    $poll_sel_output = ”

      “;

    <— change this from = to .=

    and you should initialize your variable with $poll_sel_output = ”; before entering the foreach loop.

    WTH? Ok, since I can’t get it to paste correctly, just make sure all your assignments in the foreach loop use .= instead of =

    Thread Starter smileX

    (@smilex)

    Haha, thank you so much!

    I was just wondering why my topic was so popular!

    Case Closed

    Thanks again!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘What′s wrong with this shortcode function?’ is closed to new replies.