PDA

View Full Version : Using Combined Template Conditionals


Jay Lee
07-23-2010, 04:18 AM
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:


$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:


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


Output:

Artist 1
Artist 2
Artist 3


<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:


<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.



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

$tracks_bit = '';
while($track = mysql_fetch_array($tracks, MYSQL_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
Just concat the HTML output in the loop.



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

$tracks_bit = '';
while($track = mysql_fetch_array($tracks, MYSQL_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
while($track = mysql_fetch_array($tracks, MYSQL_ASSOC)
{
$tracks_bit .= '<p>' . $track['trackartist'] . ' - ' . $track['tracktitle'];
}
create a template for each bit and do something like
while($track = mysql_fetch_array($tracks, MYSQL_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.