vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 3 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=187)
-   -   Useful function when you are coding addons (https://vborg.vbsupport.ru/showthread.php?t=41598)

hamoe 07-26-2002 10:00 PM

Useful function when you are coding addons
 
1 Attachment(s)
Last night I coded the main page for http://www.fexboards.com, and before that I coded the tutorial database for that same site (organized very similar to this sites hack section, although I believe coded quite differently). I knew I was going to have to deal with a lot of repeating-element templates. By repeating-element templates, I mean templates such as the infamous postbit template, something which when its finally used, will be echoed out many times in a row in accordance with the results of a database query. The function I wrote has you supply a comma separated list of variables as they would appear in the database query, your master template name, your subtemplate name, the table(s) present in the from clause of the query, and the conditions under which the selection will take place (the where clause, you can also include order by and group by stuff here).

An example of its use is as follows:
PHP Code:

$mainpage[top10posters] = loop_template('userid,username,posts',
                                          
'mainpage','top10posters','user',
                                          
'order by posts desc limit 10'); 

That one function call right there will store all the iterations of the mainpage_top10posters template, filled with the top 10 posters' information, into $mainpage[top10posters]. That means after you run this you can just include your final template that just echoes out $mainpage[top10posters] where its needed.

The main limitation is that you cannot do any processing on the variables. If you wanted to loop through posts and you wanted to parse the pagetext with bbcodeparse(), you would have to hack up the function or just forget about using it. Nonetheless, I have found it pretty useful.

Comments/criticism/suggestions appreciated. The function is attached.

hamoe 07-29-2002 02:50 AM

I just realized this would probably do better in the Hacking Hints and Tips forum. If some bored mod feels like moving it for me it would be appreciated :)

Admin 07-29-2002 07:14 AM

Umm, this could be written a lot shorter and faster...

PHP Code:

function loop_template($vars$template$subtemplate$table$conditions '') {
    global 
$DB_site;
    
$dblink $DB_site->query("SELECT $vars FROM $table $conditions");
    while (
$db_array $DB_site->fetch_array($dblink)) {
        foreach (
$db_array as $field => $value) {    // DO NOT use while here
            
$array["{$subtemplate}_{$field}"] = $value;
            unset(
$array["$field"]);
        }
        eval(
'$return .= "'.gettemplate($template.'_'.$subtemplate).'";');
    }
    
$DB_site->free_result($dblink);
    return 
$return;



hamoe 07-29-2002 07:38 AM

Thanks a lot, firefly. I feel really dumb about not realizing that I could have gotten fieldnames from the mysql_fetch_array() results. I haven't gotten used to the functions in vb quite yet, so I can almost justify my unneeded usage of output buffering. And nice call on defaulting the last argument to ''.

I still think this function is pretty useful, but use firefly's revised version, does the same thing but with less code and less load :)

DrkFusion 07-30-2002 10:31 PM

How would I apply this?
I want to change
Code:

  $reviewb = $DB_site->query("SELECT * FROM reviews");
  while($ra = $DB_site->fetch_array($reviewb)) {


$listings = "<a href=reviews.php?s=$session[sessionhash]&action=view&revid=$ra[id]>$ra[review_name]</a>-$ra[review_by]<br>";
eval("dooutput(\"".gettemplate("reviews_list")."\");");       

  }
}

to that.

Thanks
Drk

DrkFusion 07-31-2002 12:12 AM

bump

Drk

Chris M 07-31-2002 12:18 AM

Arunan -

Shouldnt it be?

Code:

  $reviewb = $DB_site->query("SELECT * FROM reviews");
  while($ra = $DB_site->fetch_array($reviewb)) {


$listings = "<a href=reviews.php?s=$session[sessionhash]&action=view&revid=$ra[revid]>$ra[review_name]</a>-$ra[review_by]<br>";
eval("dooutput(\"".gettemplate("reviews_list")."\");");       

  }
}

???

Satan

DrkFusion 07-31-2002 12:20 AM

It is, but its not repeating the link in reviews.php

Drk

Chris M 07-31-2002 12:25 AM

Ok...But at least it calls the right variable;)

Its a start...

Now we just need help:)

Satan

DrkFusion 07-31-2002 12:35 AM

Chen? Hamoe? come on guys :)

Drk

hamoe 07-31-2002 12:54 AM

Hey I'm here. What I am wondering is why you don't just have that link in your template file....

PHP Code:

  $reviewb $DB_site->query("SELECT * FROM reviews");
  while(
$ra $DB_site->fetch_array($reviewb)) {


   eval(
"dooutput(\"".gettemplate("reviews_list")."\");");    

  } 


Can you not just have that link that I took out in your template? Here is how the function call would look:

loop_template("*", "reviews","list", "reviews");

In your template the variables will be available to you like this:

$reviews[list_fieldname]

All other variables you want to use that arent taken from the mysql query are just available how you would expect them.

DrkFusion 07-31-2002 12:57 AM

Thanks man, you are the coolest person around, can you help me further on aim, if you have it :)

my friend sat ScapeStudio, I am at his house :)

Thanks

Chris M 07-31-2002 01:14 AM

Cool...:)

Satan

SaintDog 07-31-2002 03:24 AM

Quote:

Originally posted by hamoe
I just realized this would probably do better in the Hacking Hints and Tips forum. If some bored mod feels like moving it for me it would be appreciated :)
Bored....Moved.... ;)


- SaintDog

hamoe 08-01-2002 05:36 AM

lol, thanks saintdog, i knew someone would get bored eventually :)


All times are GMT. The time now is 09:35 AM.

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.01208 seconds
  • Memory Usage 1,758KB
  • 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
  • (2)bbcode_code_printable
  • (3)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (15)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
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete