Forum Moderators: coopster

Message Too Old, No Replies

PHP loop list issue

         

doodlebee

3:16 pm on Aug 6, 2009 (gmt 0)

10+ Year Member



I'm trying to write bit of code that queries a database and creates a list of posts and categories. I have this thing like 99.8% complete - I'm just having one single issue that I've been banging my head on since 4pm yesterday. I'd really appreciate it if someone could take a look and maybe see what I'm missing, because I'm about to have a concussion. :)

This is the code:

function list_page_children() {
global $post, $wp_query;
$cat = $wp_query->get_queried_object();

if(is_category()) {
$id = $cat->term_id;
$current_name = $cat->slug;
$term = $id;
} else if(is_single()) {
$category = get_the_category();
$parent = $category[0]->cat_name;
$postcatid = $category[0]->term_id;
$postcatcount = $category[0]->count;
$term = $postcatid;
}

if($term > '0') {
$names = get_category_parents($term, FALSE, ',', FALSE);
$getparentnames = explode(',',$names);
$parent = $getparentnames[0];

$slugs = get_category_parents($term, FALSE, ',', TRUE);
$getparentslugs = explode(',',$slugs);
$slug = $getparentslugs[0];
 
$getparentid = get_category_by_slug($slug);
$parentid = $getparentid->term_id;
}
 
$getsubs = get_categories('child_of=' . $parentid);
if($getsubs) {
$categories = $getsubs;
} else {
$categories = get_the_category();
}
 
// current category stuff
$thiscatid = get_query_var('cat');
$thiscat = &get_category($thiscatid);
$thiscatname = $thiscat->slug;
$thiscatid = $thiscat->term_id;
$thiscatparent = $thiscat->parent;
$thispost = $post->ID;

if($categories) {
foreach($categories as $cat) {
$postcount = $cat->count;
$catname = $cat->cat_name;
$catid = $cat->term_id;
$catparent = $cat->parent;
$numposts = -1;
 
// category classes
if(is_category()) {
$current_category = $thiscatid;
$current_parent = $thiscatparent;
 
$class = 'cat-item cat-item-'.$cat->term_id;
if (isset($current_category) && $current_category && ($catid == $current_category)) {
$class .= ' current-cat';
} else if(isset($current_category) && $current_category && ($catid == $current_parent)) {
$class .= ' current-cat-parent';
}
} else if(is_single()) {
$current_category = $postcatid;
$current_parent = $catid;
 
$class = 'cat-item cat-item-'.$cat->term_id;
if (isset($current_category) && $current_category && ($catid == $current_category)) {
$class .= ' current-post-cat';
}
}
 
$thelist .= '<li class="' . $class . '"><a href="' . get_category_link( $catid) . '">' . $catname . '</a>' . "\n";

$posts = get_posts('cat=' . $catid . '&showposts=' . $numposts);
if($posts) {
$thelist .= '<ul>' . "\n";
if($postcount != '0') {
foreach($posts as $post) {
setup_postdata($post);
if(is_single()) {
$postlink = get_the_ID();
if($postlink == $thispost) {
$linkclass = ' current-post';
} else {
$linkclass = '';
}
}
    
$thelist .= '<li class="' . $class . $linkclass . '"><a href="' . get_permalink($post->ID) . '" rel="bookmark" title="Permanent Link to ' . get_the_title() . '">' . get_the_title() . '</a></li>' . "\n";
} // end foreach
$thelist .= '</ul></li>' . "\n\n";
} // end "if postcount"

// $thelist .= '</li>' . "\n\n";
} // end "if posts"
} // end foreach
} // end "if categories"

$empty = 'No categories';
$find = strpos($thelist, $empty);
if($find != '') {
$thelist = '';
} ?>

<h3><?php echo $parent; ?></h3>
<ul id="children">
<?php echo $thelist; ?>
</ul>
 
<?php
}

The problem is in the section where I start putting in UL tags. The code above works perfectly *except* that it's not closing out a grouping in the proper place. The above code will output the following example:

(this is cleaned up for easier reading - just a visual to see what happens clearly)


<h3>Toplevel Category Name</h3>
<ul id="children">
<li>Category 1
<ul>
<li>Subcategory 1
<ul>
<li>post title in Subcategory 1</li>
<li>post title in Subcategory 1</li>
</ul></li>

<li>Subcategory 2
<ul>
<li>post title in Subcategory 2</li>
<li>post title in Subcategory 2</li>
<li>post title in Subcategory 2</li>
</ul></li>

<!-- there SHOULD be a closing LI tag here -->

<li>Category 2
<ul>
<li>post title in Category 2</li>
<li>post title in Category 2</li>
<li>post title in Category 2</li>
<li>post title in Category 2</li>
</ul></li>

</ul>

I've moved things around a million times, and I can't get a

</li>
tag where it's supposed to be - it either goes in the wrong place, or it adds one to every list item. Can anyone see what I'm missing? I'd appreciate and extra pair of eyes.

Thank you!

Frank_Rizzo

4:33 pm on Aug 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One quick and dirty technique for this kind of stuff (like when it's late and you just can't work out the logic) is to put a few checkpoints in there.

// $thelist .= '</li>' . "X1\n\n";
$thelist .= 'X1';
} // end "if posts"
$thelist .= 'X2';
} // end foreach
$thelist .= 'X3';
} // end "if categories"
$thelist .= 'X4';

Run that and see if any of the X's mark the spot.

doodlebee

6:20 pm on Aug 6, 2009 (gmt 0)

10+ Year Member



Yep, I did that already Frank - thanks :) However, I'm coming back to post the solution - because I figured it out. Basically, I had the script "maually" count how many posts were in a category, and then I got the actual count listed in the database. If the counts matched, then it put in the necessary closing tag in the correct position. Yay! so now it appears to be working - so far everything I've done to test it doesn't show I've messed up anywhere... so I'm good to go.

in case anyone else needs it, here's the finished code:


function list_page_children() {
global $post, $wp_query;
$cat = $wp_query->get_queried_object();

if(is_category()) {
$id = $cat->term_id;
$current_name = $cat->slug;
$term = $id;
} else if(is_single()) {
$category = get_the_category();
$parent = $category[0]->cat_name;
$postcatid = $category[0]->term_id;
$postcatcount = $category[0]->count;
$term = $postcatid;
}

if($term > '0') {
$names = get_category_parents($term, FALSE, ',', FALSE);
$getparentnames = explode(',',$names);
$parent = $getparentnames[0];
 
$slugs = get_category_parents($term, FALSE, ',', TRUE);
$getparentslugs = explode(',',$slugs);
$slug = $getparentslugs[0];

$getparentid = get_category_by_slug($slug);
$parentid = $getparentid->term_id;
}
 
$getsubs = get_categories('child_of=' . $parentid);
if($getsubs) {
$categories = $getsubs;
} else {
$categories = get_the_category();
}
 
// current category stuff
$thiscatid = get_query_var('cat');
$thiscat = &get_category($thiscatid);
$thiscatname = $thiscat->slug;
$thiscatid = $thiscat->term_id;
$thiscatparent = $thiscat->parent;
$thispost = $post->ID;

if($categories) {
foreach($categories as $cat) {
$postcount = $cat->count;
$catname = $cat->cat_name;
$catid = $cat->term_id;
$catparent = $cat->parent;
$numposts = -1;
 
// category classes
if(is_category()) {
$current_category = $thiscatid;
$current_parent = $thiscatparent;

$class = 'cat-item cat-item-'.$cat->term_id;
if (isset($current_category) && $current_category && ($catid == $current_category)) {
$class .= ' current-cat';
} else if(isset($current_category) && $current_category && ($catid == $current_parent)) {
$class .= ' current-cat-parent';
}
} else if(is_single()) {
$current_category = $postcatid;
$current_parent = $catid;
 
$class = 'cat-item cat-item-'.$cat->term_id;
if (isset($current_category) && $current_category && ($catid == $current_category)) {
$class .= ' current-post-cat';
}
}

$term = get_categories('pad_counts=1');
foreach ($term as $term) {
$termid = $term->term_id;
$termcount = $term->count;
}
 
$thelist .= '<li class="' . $class . '"><a href="' . get_category_link( $catid) . '">' . $catname . '</a>' . "\n";
 
$posts = get_posts('cat=' . $catid . '&showposts=' . $numposts);
if($posts) {
$thelist .= '<ul>' . "\n";
 
$catpostcount = count($posts); // counts all the posts in the category

if($postcount != '0') {
foreach($posts as $post) {
setup_postdata($post);
if(is_single()) {
$postlink = get_the_ID();
if($postlink == $thispost) {
$linkclass = ' current-post';
} else {
$linkclass = '';
}
}
 
$count = $postcount;
    
$thelist .= '<li class="' . $class . $linkclass . '"><a href="' . get_permalink($post->ID) . '" rel="bookmark" title="Permanent Link to ' . get_the_title() . '">' . get_the_title() . '</a></li>' . "\n";
} // end foreach
$thelist .= '</ul></li>' . "\n\n";
} // end "if postcount"
if($postcount == $termcount) {
$thelist .= '</ul></li>' . "\n";
}
} // end "if posts"
} // end foreach
} // end "if categories"

$empty = 'No categories';
$find = strpos($thelist, $empty);
if($find != '') {
$thelist = '';
} ?>

<h3><?php echo $parent; ?></h3>
<ul id="children">
<?php echo $thelist; ?>
</ul>

<?php
}