WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] For each loop takes only one variable of array and forgets the rest (9 posts)

  1. design_dolphin
    Member
    Posted 4 years ago #

    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?

  2. MichaelH
    Member
    Posted 4 years ago #

    Can you explain what you are attempting to accomplish?

  3. kymac
    Member
    Posted 4 years ago #

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

  4. design_dolphin
    Member
    Posted 4 years ago #

    @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.

  5. kymac
    Member
    Posted 4 years ago #

    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.

  6. design_dolphin
    Member
    Posted 4 years ago #

    @kymac

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

    Works like a charm.

    Thank you, and thank you as well MichealH for taking the time. :-)

  7. kymac
    Member
    Posted 4 years ago #

    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.

  8. design_dolphin
    Member
    Posted 4 years ago #

    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.

  9. kymac
    Member
    Posted 4 years ago #

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

    Looks like your approach is good.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.