vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Using Combined Template Conditionals (https://vborg.vbsupport.ru/showthread.php?t=247113)

Jay Lee 07-23-2010 04:18 AM

Using Combined Template Conditionals
 
I'm trying to understand how to use conditionals in templates, using the following thread as an initial guide:

https://vborg.vbsupport.ru/showthread.php?t=217570

So basically what I'm trying to do is to show a track listing. Let's say the following is what's in the database:

Field "trackartist": Artist 1, Artist 2, Artist 3
Field "tracktitle": Title 1, Title 2, Title 3

So I need to figure out how to convert the following php code so that it's compatible with vBulletin:

Code:

$doquery = mysql_query("SELECT trackartist, tracktitle FROM `tracklisting`");
$numberoftracks = mysql_num_rows($doquery);
$count = 0;

for ($i = 0; $i < $numberoftracks; $i++)
{
$trackartist[$i] = mysql_result($doquery, $count, "trackartist");
$tracktitle[$i] = mysql_result($doquery, $count, "tracktitle");
echo $trackartist . " - " . $tracktitle . "<br>";
}

The output should be simple:

Artist 1 - Title 1
Artist 2 - Title 2
Artist 3 - Title 3

The problem is, I don't know how to combine template conditionals, meaning I was only able to show each variable separately:

Code:

<vb:each from="trackartist" key="character1" value="trackartist">
<li> {vb:var trackartist} </li>
</vb:each>

Output:

Artist 1
Artist 2
Artist 3

Code:

<vb:each from="tracktitle" key="character2" value="tracktitle">
<li> {vb:var tracktitle} </li>
</vb:each>

Output:

Title 1
Title 2
Title 3

The closest I was able to get to the correct result was the following:

Code:

<vb:each from="trackartist" key="character1" value="trackartist">
<vb:each from="tracktitle" key="character2" value="tracktitle">
<li> {vb:var trackartist} - {vb:var tracktitle} </li>
</vb:each>
</vb:each>

Output:

Artist 1 - Title 1
Artist 1 - Title 2
Artist 1 - Title 3

So how do I get the correct result? Any help would be greatly appreciated!

Guest190829 07-23-2010 04:08 PM

Just concat the HTML output in the loop.

PHP Code:


$tracks 
mysql_query("SELECT trackartist, tracktitle FROM tracklisting");

$tracks_bit '';
while(
$track mysql_fetch_array($tracksMYSQL_ASSOC)
{
    
$tracks_bit .= '<p>' $track['trackartist'] . ' -  ' $track['tracktitle'];


Then register the variable, and you should have the correct output.

Jay Lee 07-24-2010 04:12 AM

Quote:

Originally Posted by Danny.VBT (Post 2073424)
Just concat the HTML output in the loop.

PHP Code:


$tracks 
mysql_query("SELECT trackartist, tracktitle FROM tracklisting");

$tracks_bit '';
while(
$track mysql_fetch_array($tracksMYSQL_ASSOC)
{
    
$tracks_bit .= '<p>' $track['trackartist'] . ' -  ' $track['tracktitle'];


Then register the variable, and you should have the correct output.

I knew I was missing something simple! Your code works! Thank you so much!

Retal 07-29-2010 12:31 PM

It would be even better if you didn't use HTML in the code.
Instead of
PHP Code:

while($track mysql_fetch_array($tracksMYSQL_ASSOC

    
$tracks_bit .= '<p>' $track['trackartist'] . ' -  ' $track['tracktitle']; 


create a template for each bit and do something like
PHP Code:

while($track mysql_fetch_array($tracksMYSQL_ASSOC

$templater vB_Template::create('tracks_bit');
    
$templater->register('track'$track);
$tracks_bit .= $templater->render(); 



Jay Lee 08-01-2010 08:58 PM

Thanks for the additional tip Retal. I'll use your code and if I encounter any problems I'll post questions.


All times are GMT. The time now is 11:21 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.01062 seconds
  • Memory Usage 1,738KB
  • 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
  • (4)bbcode_code_printable
  • (4)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)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