vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Function info to a template? (https://vborg.vbsupport.ru/showthread.php?t=176718)

WarriorDL 04-20-2008 07:47 PM

Function info to a template?
 
I probably have this written incorrectly, but... how do I get this info, namely the $category name at the end, to be accessible via a template variable?

Like how $forumbits is pulled from a function into the forum templates.
PHP Code:

function get_categories($cat_id 0
{
    global 
$vbulletin$stylevar$vbphrase$show$atlas_config$site_template;
    global 
$cat_cache$cat_parent_cache$new_image_cache$subcat_ids;

    
$cattable_width ceil((intval($atlas_config['cat_table_width'])) / $atlas_config['cat_cells']);
    if ((
substr($atlas_config['cat_table_width'],-1)) == "%"
    {
        
$cattable_width .= "%";
    }

    if (!isset(
$cat_parent_cache[$cat_id])) 
    {
        return 
"";
    }

    
$visible_cat_cache = array();
    if (empty(
$visible_cat_cache)) 
    {
        return 
"";
    }

    
$total sizeof($visible_cat_cache);
    
$table_columns = (intval($atlas_config['cat_cells'])) ? intval($atlas_config['cat_cells']) : 2;
    if (
$total <= $table_columns
    {
        
$table_rows 1;
    }
    else 
    {
        
$table_rows $total $table_columns;
        if (
$total >= $table_columns AND !is_integer($table_rows)) 
        {
            
$table_rows intval($table_rows) + 1;
        }
    }

    
$categories '<table width="'.$atlas_config['cat_table_width'].'" border='0' cellpadding='0' cellspacing='0'><br /><tr><br /><td valign='top' width="'.$cattable_width.'" class='catbgcolor'><br />';
    
$categories .= '<table border='0' cellpadding="'.$atlas_config['cat_table_cellpadding'].'" cellspacing="'.$atlas_config['cat_table_cellspacing'].'"><br />';
    
$count 0;
    
$count2 0;
    foreach (
$visible_cat_cache AS $key => $category_id
    {
        
$categories .= '<tr><br /><td valign='top'><br />';

        
$is_new = (isset($new_image_cache[$category_id]) AND $new_image_cache[$category_id] > 0) ? 0;
        
$num_images = (isset($cat_cache[$category_id]['num_images'])) ? $cat_cache[$category_id]['num_images'] : 0;

        
$subcat_ids = array();
        
get_subcat_ids($category_id$category_id$cat_parent_cache);

        if (isset(
$subcat_ids[$category_id])) 
        {
            foreach (
$subcat_ids[$category_id] AS $val
            {
                if (isset(
$new_image_cache[$val]) AND $new_image_cache[$val] > 0
                {
                    
$is_new 1;
                }
                if (isset(
$cat_cache[$val]['num_images'])) 
                {
                    
$num_images += $cat_cache[$val]['num_images'];
                }
            }
        }

        if (
defined("SHOW_RANDOM_IMAGE") AND SHOW_RANDOM_IMAGE == OR defined("SHOW_RANDOM_CAT_IMAGE") AND SHOW_RANDOM_CAT_IMAGE == 0
        {
            
$random_cat_image_file "";
        }
        else 
        {
            
$random_cat_image_file get_random_image($category_id01);
        }
        
        
$this->registry = array (
        
'cat_id' => $category_id,
        
'cat_name' => format_text($cat_cache[$category_id]['cat_name'], 2),
        
'cat_description' => format_text($cat_cache[$category_id]['cat_description'], 1),
        
'cat_hits' => $cat_cache[$category_id]['cat_hits'],
        
'cat_is_new' => $is_new,
        
'lang_new' => $vbphrase['new'],
        
'sub_cats' => get_subcategories($category_id),
        
'cat_url' => $vbulletin->session->url(ATLAS_ROOT_PATH."categories.php?".URL_CAT_ID."=".$category_id),
        
'random_cat_image_file' => $random_cat_image_file,
        
'num_images' => $num_images);
        eval(
'$categories .= "' fetch_template('atlas_category_bits') . '";');
        
$count++;
        
$count2++;
        
$categories .= '</td><br /></tr><br />';

        if (
$count == $table_rows AND $count2 sizeof($visible_cat_cache)) 
        {
            
$categories .= '</table><br /></td><br />';
            
$categories .= '<td valign='top' width="'.$cattable_width.'" class='catbgcolor'><br />';
            
$categories .= '<table border='0' cellpadding="'.$atlas_config['cat_table_cellpadding'].'" cellspacing="'.$atlas_config['cat_table_cellspacing'].'"><br />';

            
$total $total $count2;
            
$table_columns $table_columns 1;
                
/*if ($total <= $table_columns AND $table_columns > 1) 
                {
                    $table_rows = 1;
                }
                else 
                {
                    $table_rows = $total / $table_columns;
                if ($total >= $table_columns AND !is_integer($table_rows)) {
                  $table_rows = intval($table_rows) + 1;
                    }
                }*/
            
$count 0;
        }
    }

    
$categories .= '</table><br /></td><br /></tr><br /></table><br />';
    return 
$categories;



Opserty 04-20-2008 08:16 PM

Probably won't aid your actual problem but I recommend you take a read of vBulletin Code Standards (specifically the bits related to code layout), it will make it easier for others to read/understand your code and also make it easier for you to spot your own problems.

WarriorDL 04-20-2008 10:16 PM

Edited the indent spacing (I think that's what you meant).

Opserty 04-21-2008 02:35 PM

Where is this code being executed/the function being called. You've just written a function at the moment, it won't do anything until you execute it. You could put it in a Plugin then do something like:

PHP Code:

$cats get_categories(); 

Then use [minicode]$cats[/minicode] in your template.

WarriorDL 04-21-2008 02:54 PM

Ok. I have it written in an external functions page that I named "atlas_functions.php" and call it through my external index page.

On my external index page, I do have this:

PHP Code:

$categories get_categories(0);
if (!
$categories)  
{
    
$categories $vbphrase['atlas_no_categories'];
}
unset(
$categories); 

And in the index template I created for my external page, I use $categories in the template.

--------------- Added [DATE]1208793301[/DATE] at [TIME]1208793301[/TIME] ---------------

What's weird is that the counters thing I posted about in another thread DOES work when called from the same functions page.

Opserty 04-21-2008 02:58 PM

Why have you got this line:
PHP Code:

unset($categories); 

?

WarriorDL 04-21-2008 03:08 PM

It was in the original php. Removing it has made no difference.

Opserty 04-21-2008 03:11 PM

Look up what unset() does on php.net. :p

Hmm its might be a problem with the function itself, have you checked to see if it is even returning anything? (Run a [minicode]var_dump()[/minicode] on it to see if it contains anything).

WarriorDL 04-21-2008 03:33 PM

...ok. Please humor me :D

Where do I put that var_dump()??

--------------- Added [DATE]1208796733[/DATE] at [TIME]1208796733[/TIME] ---------------

Ok. I did this (hope it was right)

PHP Code:

$categories get_categories(0);
var_dump($categories get_categories);
if (!
$categories)  
{
    
$categories $vbphrase['atlas_no_categories'];


Now when I reload the page, this is at the top of it:

string(14) "get_categories"

And in the area where the categories are to show up it says:

get_categories

...and without the "get_categories" in there, I get this at the top:

string(0) ""

And in the page area, nothing.

--------------- Added [DATE]1208802729[/DATE] at [TIME]1208802729[/TIME] ---------------

Ok. I'm gonna start this again from scratch. Gonna have to re-write all.

Thanks for the help though! :)

Opserty 04-22-2008 01:27 PM

I recommend you also refresh yourself on the basics of PHP also you can access documentation and examples of most functions by a quick browse on the PHP site by using: http://www.php.net/function_name where function_name is the the function your looking up. So for example var_dumo() would be http://php.net/var_dump e.t.c.


All times are GMT. The time now is 01:06 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01175 seconds
  • Memory Usage 1,798KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete