vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Insert custom thread field into the database (https://vborg.vbsupport.ru/showthread.php?t=307730)

josh_krz 01-30-2014 07:01 PM

Insert custom thread field into the database
 
Hi there,

I have a good understanding of the vB templating system and PHP knowledge but I am having trouble working out how I could add a custom field on my thread pages to a new column in the "thread" table.

I know how I could add info to the table using PHP prepared statements but apparently it is better to use vBulletins own database classes however I can't find information on it anywhere!

I also don't know how I would get the generated thread ID to use as a reference when inserting this info or what my custom field would be called, I can't just use $POST['field']. These variables seem to be a mystery.

Advanced vBulletin documentation seems to be pretty scarce am I supposed to guess these variables and class names :S.

Thank you.

kh99 01-30-2014 08:25 PM

1 Attachment(s)
I don't know of any documentation, but I threw together a bare bones example of adding a field to the thread table, and I've attached the product xml (you'd only want to import this to a test forum). Two things this example doesn't do: create the database field and add the input field to the newthread template. I called my new field "test" and made it a string, so to use this example product you'd need to add a field to your thread table called "test", and add an input control to the form in the newthread template, and set name="test". Alternatively you could edit the code and change the field name and type to something else.

If you have any questions please ask, but if you don't already have the vb source code set up so you can search it, you should do that. If you search for the hook names you can see a little about what's going on at the point where each plugin in executed.

josh_krz 01-30-2014 09:30 PM

Thank you kh99, so all I need to do is create the database column and add the field within the template for this to work?

Looking at that XML these things seems pretty straight forward, if there were some definite way to know what classes and functions to use :(

How do people learn how to code plugins, there must be some cache of resources somewhere outlining these things?

Thanks once again!

ozzy47 01-30-2014 09:36 PM

No there is no tutorial, basically you have to know, php, html, css, and have a understanding of how vBulletin works. Then take it and put it all together.

PHP, basic guide: PHP 5 Tutorial

HTML basic guide: HTML Tutorial - (HTML5 Compliant)

CSS basic guide: CSS Tutorial

vBulletin basic mod guide: Creating a Product

kh99 01-30-2014 10:07 PM

Quote:

Originally Posted by josh_krz (Post 2478077)
Thank you kh99, so all I need to do is create the database column and add the field within the template for this to work?

Right. Also, it should automatically be read in so in most places where you have a $threaddata array, your field value should be there. So for example if you wanted to display the value on the showthread page, you shouldn't need to do anything extra to read it from the database.

BTW, another thing this example doesn't handle is editing the thread.


Quote:

Looking at that XML these things seems pretty straight forward, if there were some definite way to know what classes and functions to use :(

How do people learn how to code plugins, there must be some cache of resources somewhere outlining these things?
Yeah, I don't know how most people learned, but I just started reading the code. For example if I wanted to add a field to a thread, I'd see that when I'm creating a new thread it goes to newthread.php, so I'd look at that to see what was going on (yeah, it's easy for me to say but I know it takes a lot of time).

josh_krz 01-31-2014 05:15 PM

Quote:

Originally Posted by ozzy47 (Post 2478081)
No there is no tutorial, basically you have to know, php, html, css, and have a understanding of how vBulletin works. Then take it and put it all together.

Thanks for your help but im already pretty comprehensive in PHP 5, HTML 5 and CSS 3. It's just the vBulletin part I thought would have been documented. That link is helpful though!

Quote:

Originally Posted by kh99 (Post 2478089)
Right. Also, it should automatically be read in so in most places where you have a $threaddata array, your field value should be there. So for example if you wanted to display the value on the showthread page, you shouldn't need to do anything extra to read it from the database.

BTW, another thing this example doesn't handle is editing the thread.

I see so I would need to add a plugin that also reads the current info from the database and adds it in when editing?

Quote:

Originally Posted by kh99 (Post 2478089)
Yeah, I don't know how most people learned, but I just started reading the code. For example if I wanted to add a field to a thread, I'd see that when I'm creating a new thread it goes to newthread.php, so I'd look at that to see what was going on (yeah, it's easy for me to say but I know it takes a lot of time).

I was initially going to do this but I don't have a test vBulletin site to do this on just my production one. I was worried about messing my site up.

I will take a look at some of the internal code and see what I can make of it. Thank you very much!

grey_goose 04-04-2016 06:53 PM

This seems fairly straightforward but I guess I'm messing something up.

1. I've added 'expenditure' to the 'post' table.
2. I'd like to be able to to add/update this field when replies to a thread are made.

newpost_process
PHP Code:

if ($type == 'thread')
{
    
$dataman->setr('expenditure'$post['expenditure']);


newreply_post_start
PHP Code:

$vbulletin->input->clean_gpc('p''expenditure'TYPE_STR);
$newpost['expenditure'] = $vbulletin->GPC['expenditure']; 

newreply_start
PHP Code:

$this->validfields['expenditure'] = array(TYPE_STR,  REQ_NO); 

newreply template
PHP Code:

<input type="text" name="expenditure" /> 

When I look at my post table, there's no values being added to expenditures. I also tried:

PHP Code:

$expenditure $vbulletin->input->clean_gpc('p'"expenditure"TYPE_STR);
$vbulletin->post['expenditure'] += $expenditure

Where am I going wrong?


All times are GMT. The time now is 05:23 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.01052 seconds
  • Memory Usage 1,746KB
  • 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
  • (5)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)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