1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

PHP help, a simple coding a while loop

Discussion in 'BBS Hangout' started by what, Jul 14, 2013.

Tags:
  1. what

    what Member

    Joined:
    Dec 4, 2003
    Messages:
    14,621
    Likes Received:
    2,593
    If I wanted to echo the current category_description using these functions:

    as it stands, if I do something like this it only shows the earliest category created.
    {?php else:(categories())?}

    {?php echo category_description(); ?}

    {?php endif; ?}

    If I add a while loop, it shows all the category_descriptions. I onlyb want to show the CURRENT description of the page.

    Here are your functions:
    categories()

    Loops through every category, one by one. Returns true when there is more
    categories to be listed or false when we are at the end.
    Should be used like this:

    {?php while(categories()): ?}
    <!-- Loop through the categories. -->
    {?php endwhile; ?}

    category_count
    Returns the number of posts that are in the current category.

    category_description
    Returns the current category description.

    category_id
    Returns the current category ID.

    category_slug
    Returns the current category url slug.

    category_title
    Returns the current category title.

    category_url
    Returns the full current category url.

    total_categories
    Returns the number of categories.
     
  2. what

    what Member

    Joined:
    Dec 4, 2003
    Messages:
    14,621
    Likes Received:
    2,593
    Not sure I made it clear, but what I am trying to do is to correctly identify the category_description for each page using the functions I listed.

    just echoing the function category_description doesn't work, as it appears to only see the first category I created. Somehow I need to collect the category_ids and attach them to the category_descriptions prior to echoing the output, if that makes sense.
     
  3. Kyakko

    Kyakko Member

    Joined:
    Aug 15, 2002
    Messages:
    2,161
    Likes Received:
    39
    I think you need the id from the page GET or POST to compare the category with. categories seems to return all the categories, not just the current one. So, it's probably something like this:

    {?php

    while(categories()){

    if (category_id == _GET['id'] || category_id == _POST['id']) {
    echo category_description;
    break;
    }
    }

    ?}
     
  4. what

    what Member

    Joined:
    Dec 4, 2003
    Messages:
    14,621
    Likes Received:
    2,593
    I'm getting an unexpected '[' on the if line.
     
  5. Kyakko

    Kyakko Member

    Joined:
    Aug 15, 2002
    Messages:
    2,161
    Likes Received:
    39
    Sorry... I've been working on ruby for a while.. it's $_GET['id'] and $_POST['id'] . Note the $ for variables. Also.. id might not be the name of the id.. it could product_id or category_id . Usually you can tell on your url string. If it says http://www.xxx.com?category_id=2&widget=3 or something similar, it's category_id, i.e. $_GET['category_id']
     
  6. Kyakko

    Kyakko Member

    Joined:
    Aug 15, 2002
    Messages:
    2,161
    Likes Received:
    39

    also.. it's category_id() note the () for functions. I've been using ruby for too long :p it's got none of those syntax.

    Anothe note: in instuctions with iterators, current doesn't mean current page.. it' means the current iterator position. So:

    while(categories()){

    if (category_id() == _GET['id'] || category_id() == _POST['id']) {
    echo category_description;
    break;
    }

    }
     
  7. what

    what Member

    Joined:
    Dec 4, 2003
    Messages:
    14,621
    Likes Received:
    2,593
    yeah, i fixed it the way you said, but it can't find the array: 'id' or category_id either. When I look into the phpmyadmin, the actual database entry is anchor_categories and there is an id in the table. However, anchor doesn't actually use the actual database table name, rather it has a function system.

    {?php

    /**
    * Theme functions for categories
    */

    function total_categories() {
    if( ! $categories = Registry::get('categories')) {
    $categories = Category::get();

    $categories = new Items($categories);

    Registry::set('categories', $categories);
    }

    return $categories->length();
    }

    // loop categories
    function categories() {
    if( ! total_categories()) return false;

    $items = Registry::get('categories');

    if($result = $items->valid()) {
    // register single category
    Registry::set('category', $items->current());

    // move to next
    $items->next();
    }

    return $result;
    }

    // single categories
    function category_id() {
    return Registry::prop('category', 'id');
    }

    function category_title() {
    return Registry::prop('category', 'title');
    }

    function category_slug() {
    return Registry::prop('category', 'slug');
    }

    function category_description() {
    return Registry::prop('category', 'description');
    }

    function category_url() {
    return base_url('category/' . category_slug());
    }

    function category_count() {
    return Query::table(Base::table('posts'))
    ->where('category', '=', category_id())
    ->where('status', '=', 'published')->count();
    }
     
  8. Kyakko

    Kyakko Member

    Joined:
    Aug 15, 2002
    Messages:
    2,161
    Likes Received:
    39
    Okay... categories is doing what I expected it to do. It iterates through a static class and returns ALL of the categories one by one. So, you need to know which category the web page is currently showing. Can you put this line of code and paste the results :


    foreach ($_POST as $param_name => $param_val) {
    echo "Param: $param_name; Value: $param_val ,";
    }

    foreach ($_GET as $param_name => $param_val) {
    echo "Param: $param_name; Value: $param_val ,";
    }

    put this on the page you want to edit
     
    #8 Kyakko, Jul 14, 2013
    Last edited: Jul 14, 2013

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now