WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] For each loop takes only one variable of array and forgets the rest

[Resolved] For each loop takes only one variable of array and forgets the rest

  • design_dolphin

    @design_dolphin

    I want to pass the id of the parent category to a for each loop. When I pass the result manually and fill this in, then the for each loop works. However when I pass the variable $qi to the for each loop, then I just get a result for the first ID? The relevant part of the code is marked with a comment.

    I still have to the combine double ids’, but I would like the for each loop to work first.

    <?php 
    
         $categories = get_categories(); 
    
                 foreach ($categories as $category) {
    
                      $tests = array($category->category_parent);
    
                 foreach ($tests as $trys) {
    
                      $groups[] = $trys;
    
    }
         }
    
     $qi = implode(', ', $groups);
     $qi = str_replace('0,','',$qi); // remove the 0 id
     // result parent category ids' as 1,1,2,2,2 . Tested with echo $qi and the result is just that.
    
        ?>
    
    <?php 
    
         $display_categories = array($qi);$i = 1; // This is the problem area. It accepts 1,2,2,1,1 manually, but with the variable $qi it just picks the first number?
    
                  foreach ($display_categories as $category) { ?>
    
    <div id="fr_cat-<?php echo $i; ?>" class="fr_cat_nav">
    
    <?php query_posts("showposts=1&cat=$category")?>
    
    // Do stuff e.g.  <?php echo get_category_link($category);?>
    
    </div>
    
    <?php $i++; ?>
    <?php } ?>

    Anybody have a take on what could be going wrong?

Viewing 8 replies - 1 through 8 (of 8 total)
  • MichaelH

    @michaelh

    Can you explain what you are attempting to accomplish?

    I think you might need to explode($qi).

    design_dolphin

    @design_dolphin

    @michealh:

    My homepage has two categories listed in colored boxes at the top. I am trying to get these to be created automatically instead of typing the category numbers in manually.

    @kymac

    If I explode I get an array again? I’m lost.

    Edit: added the homepage url.

    I haven’t tested it, but I think the array declaration is taking $qi as a string and making $display_categories a single-value array.

    If this is the case, using $display_categories = explode(‘,’,$qi); will create the full array.

    Sorry if I am leading you down the wrong path.

    design_dolphin

    @design_dolphin

    @kymac

    $display_categories = explode(',',$qi);

    Works like a charm.

    Thank you, and thank you as well MichealH for taking the time. 🙂

    One other note:

    If the 0 id is always first, you could use

    $display_categories = array_shift($groups);

    and eliminate $qi. If it isn’t always first, and happens to be last, then your str_replace will fail because there is no trailing comma.

    design_dolphin

    @design_dolphin

    Thanx, I tried it, but unfortunately it errored out. Your point about the ‘0’ value is a valid one though, and I was planning to look for a way to do this as well.

    This might be a better way:

    $categories = get_categories(); 
    
            foreach ($categories as $category1) {
    
                 $tests = array($category1->category_parent);
    
            foreach ($tests as $trys) {
    
                      if ($trys > 0) {
                      $groups[] = $trys;
                      }
    }
         } 
    
           $qi = implode(', ', $groups);
    
           $display_categories = explode(',',$qi);$i = 1;
    ?>

    This should solve the problem before it starts. Stopping any 0 value from getting into the array into the first place.

    My mistake – array_shift returns the value shifted off, not the remaining array.

    Looks like your approach is good.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] For each loop takes only one variable of array and forgets the rest’ is closed to new replies.
Skip to toolbar