PDA

View Full Version : Help show more than one result.


Preech
04-07-2012, 04:20 AM
So I'm working on creating a mod for music base sites. I am trying to show the output for all of the latest mixtapes that was posted. I can get the one to show. But the rest doesn't show. Maybe I messed up some where in the code. Here is what I got.

// ###### YOUR CUSTOM CODE GOES HERE #####
$musicalbum = $db->query_read("
SELECT al.alid, al.alname, al.arid, al.added, al.cvrsaved, ar.arname FROM " . TABLE_PREFIX. " album al
LEFT JOIN artist ar ON ar.arid=al.arid
ORDER BY al.alid DESC LIMIT 5
");


while ($musical =$db->fetch_array($musicalbum))
{
$malbumid = $musical['alid'];
$malbumname = $musical['alname'];
$malbumarname = $musical['arname'];
$malbumarid = $musical['arid'];
$mcvrsaved = $musical['cvrsaved'];
}



$musicstat = $db->query_read("SELECT nr_artists, nr_albums,nr_tracks FROM " . TABLE_PREFIX ." stats");
while ($music_stat = $db->fetch_array($musicstat))
{
$statar = $music_stat['nr_artists'];
$statal = $music_stat['nr_albums'];
$statt = $music_stat['nr_tracks'];
}




// ###### NOW YOUR TEMPLATE IS BEING RENDERED ######

$templater = vB_Template::create('vbmusic');
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('malbumid', $malbumid);
$templater->register('malbumname', $malbumname);
$templater->register('malbumarname', $malbumarname);
$templater->register('malbumarid', $malbumarid);
$templater->register('cvrsaved', $cvrsaved);
$templater->register('statar', $statar);
$templater->register('statal', $statal);
$templater->register('statt', $statt);
$templater->register('mar1', $mar1);
$templater->register('vbmusic_latest', $vbmusic_latest);
print_output($templater->render());

this is my template.
<div class="audio_body2">
<span class="column">
<div class="box">
<center>
<vb:if condition="$my_variable == yes">
<img src="./audio/covers/{vb:raw cvrsaved}/jpg" width="130px" border="0" ">
<vb:else />
<img src="./audio/covers/no_cover.jpg" width="130px" border="0" ">
</vb:if>
</center>
</div>
<hr style="height: 1;
border-style: dashed;
border-width: 1px 0 0 0;
border-color:rgb(255, 255, 255) url(images/gradients/grey-up.png) repeat-x left bottom;">
<div class="box">Album:&nbsp <a href="./vbmusic_album.php?id=".$row1['alid']."\">{vb:raw malbumname}</a></div>
<div class="box">Artist:&nbsp<a href="./artist.php?id={vb:raw malbumarid}">{vb:raw malbumarname}</a></div>
</span>
</div>

and this is the code I'm using to pull this template into my main template.
{vb:raw vbmusic_latest}

Please point me in the right direction.

Pandemikk
04-07-2012, 04:42 AM
You need to register your template for use in another template: https://vborg.vbsupport.ru/showthread.php?t=228078

Save your template into a variable then preregister that variable into the main template. The variable will contain the HTML and parsed template syntax ready for use in the main template.

Btw: You should wrap: if ($db->num_rows($result))
{
// while here
}
Prevents errors on empty results.

Preech
04-07-2012, 04:44 AM
Am I putting this before my while statment, or would it be smarter to try using the foreach. I'm guessing my issue is all in my php page.

Pandemikk
04-07-2012, 04:55 AM
I think I've mistaken what you're trying to do.

You want: {vb:raw vbmusic_latest}

To contain the contents of the HTML you posted in your first post? Assuming it's a template,

$templater = vB_Template::create('YOURTEMPLATE');
$templater->register('malbumid', $malbumid);
$templater->register('malbumname', $malbumname);
$templater->register('malbumarname', $malbumarname);
$templater->register('malbumarid', $malbumarid);
$vbmusic_latest = $templater->render();

Preech
04-07-2012, 05:03 AM
Yes, {vb:raw vbmusic_latest} contains all of the html.
Negative on showing up though. I'm guessing I have to do a foreach statement.

Pandemikk
04-07-2012, 05:18 AM
Lol no.

Are you sure $vbmusic_latest contains the proper HTML?

var_dump($vbmusic_latest); die;
Put this right above where your template gets rendered. What do you see?

Preech
04-07-2012, 05:35 AM
after pasting this. I see NULL. No I know some where I'm messing up with registering the template to show.

Pandemikk
04-07-2012, 05:46 AM
Your code doesn't show $vbmusic_latest being registered at all. ;)

Preech
04-07-2012, 05:54 AM
$musicalbum = $db->query_read("
SELECT al.alid, al.alname, al.arid, al.added, al.cvrsaved, ar.arname FROM " . TABLE_PREFIX. " album al
LEFT JOIN artist ar ON ar.arid=al.arid
ORDER BY al.alid DESC LIMIT 5
");
if ($db->num_rows($musicalbum))
{
while ($musical =$db->fetch_array($musicalbum))
{
$malbumid = $musical['alid'];
$malbumname = $musical['alname'];
$malbumarname = $musical['arname'];
$malbumarid = $musical['arid'];
$mcvrsaved = $musical['cvrsaved'];
}





}

$musicstat = $db->query_read("SELECT nr_artists, nr_albums,nr_tracks FROM " . TABLE_PREFIX ." stats");
while ($music_stat = $db->fetch_array($musicstat))
{
$statar = $music_stat['nr_artists'];
$statal = $music_stat['nr_albums'];
$statt = $music_stat['nr_tracks'];
}




// ###### NOW YOUR TEMPLATE IS BEING RENDERED ######

$templater = vB_Template::create('vbmusic');
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('malbumid', $malbumid);
$templater->register('malbumname', $malbumname);
$templater->register('malbumarname', $malbumarname);
$templater->register('malbumarid', $malbumarid);
$templater->register('vbmusic_latest', $vbmusic_latest);
$templater->register('cvrsaved', $cvrsaved);
$templater->register('statar', $statar);
$templater->register('statal', $statal);
$templater->register('statt', $statt);
$vbmusic_latest = $templater->render();
print_output($templater->render());

First off I appreciate all of the help. This is the php page.
1. The vbmusic_latest template still doesn't show.
2. For each album id, I want to display that album. I've looked through the original vb files. The closest would be foreach, but I'm lost now.

Pandemikk
04-07-2012, 07:00 AM
1) Let's work on getting you to actually show your template first. :)

You really should look at that article I posted, because you're still doing it all wrong.

$templater = vB_Template::create('vbmusic');
$templater->register('malbumid', $malbumid);
$templater->register('malbumname', $malbumname);
$templater->register('malbumarname', $malbumarname);
$templater->register('malbumarid', $malbumarid);
$vbmusic_latest = $templater->render();

$templater = vB_Template::create('GENERIC_SHELL');
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('vbmusic_latest', $vbmusic_latest);
$templater->register('cvrsaved', $cvrsaved);
$templater->register('statar', $statar);
$templater->register('statal', $statal);
$templater->register('statt', $statt);
print_output($templater->render());

Preech
04-07-2012, 07:10 AM
Damn, that was simple. I see where I went wrong.
I never put this in the code at all.
$templater = vB_Template::create('vbmusic');

I still one get the one album to show. I will send you a pm of the link of what I"m trying to do.

--------------- Added 1333786475 at 1333786475 ---------------

I read the article, I guess I just wasn't pay attention to it.
I really do appreciate it. I've sent you a link to see what I'm trying to accomplish over-all.
Just by seeing how it's done, I learned alot faster on how to accomplish this. Now I'm just trying to figure out the foreach statement.

I figured I could use the vb:each statement in the template, but that didn't work.

Pandemikk
04-07-2012, 07:27 AM
I'm going to focus on your second problem now:

while ($musical =$db->fetch_array($musicalbum))
{
$malbumid = $musical['alid'];
$malbumname = $musical['alname'];
$malbumarname = $musical['arname'];
$malbumarid = $musical['arid'];
$mcvrsaved = $musical['cvrsaved'];
}You're only going to get one value doing that because you're overwriting the variable each time.

$musicals = array();
if ($db->num_rows($musicalbum))
{
while ($musical = $db->fetch_array($musicalbum))
{
$musicals[] = $musical;
}
}
$db->free_result($musicalbum);Now we're cooking. ;)

I've replace your unnecessary variables with an array and, furthermore, I've made sure each row gets it own place in the array.

$musicstat = $db->query_read("SELECT nr_artists, nr_albums,nr_tracks FROM " . TABLE_PREFIX ." stats");
while ($music_stat = $db->fetch_array($musicstat))
{
$statar = $music_stat['nr_artists'];
$statal = $music_stat['nr_albums'];
$statt = $music_stat['nr_tracks'];
}
Think you can figure out how to fix this?

Now replace all that ugliness in your first template render with:
$templater->register('musicals', $musicals);Now you have an array of all the albums ready for display.

I'd recommend using {vb:each [, ]} to loop through that array and do that.

Read my article, it should help you get the basics down: https://vborg.vbsupport.ru/showthread.php?t=280880

Preech
04-07-2012, 07:34 AM
Appreciate it so much. Just some free time during this deployment. I will read this article one more.

--------------- Added 1333821880 at 1333821880 ---------------

I've been trying for hours. I've search on google, and read through your article. I'm assuming that I am making a mistake as to where I'm suppose to put the php code foreach.

Preech
04-08-2012, 05:17 PM
Yo, I am seriously lost.
This is my code in the template
<vb:each from="musicals" value="value">
{vb:raw vbmusic_latest}
</vb:each>

This is my php code.
$musicals = array('alid','alname','arid','cvrsaved','arname');

if ($db->num_rows($musicalbum))
{
while ($musical = $db->fetch_array($musicalbum))
{

$musicals[] = $musical;
$malbumid = $musical['alid'];
$malbumname = $musical['alname'];
//$malbumarname = $musical['arname'];
//$malbumarid = $musical['arid'];
//$mcvrsaved = $musical['cvrsaved'];
}
foreach ($musicals AS $value)
{
$value = $vbmusic_latest;
}
}

I am trying to get the template to show for each album, but I can not get it to show. I can get it to show without being in the foreach.

Pandemikk
04-08-2012, 05:29 PM
What are you doing? The code I gave you is what you should be using! What you're doing won't work. vb:each replicates the foreach function, so you shouldn't be using an actual foreach in your php code!

<vb:each from="musicals" key="key" value="musical">
{vb:raw musical.nr_artists}
{vb:raw musical.nr_albums}
etc.,.
</vb:each>

Preech
04-08-2012, 05:56 PM
Okay, so I put the php code just like you shown, and I've put the vb:each in just like you just posted. Nothing shows.
I've been reading alot of articles.

Pandemikk
04-08-2012, 07:18 PM
Post your complete PHP code and template code.

Preech
04-09-2012, 03:36 AM
Php Code
<?php

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################

define('THIS_SCRIPT', 'vbmusic');
define('CSRF_PROTECTION', true);
// change this depending on your filename

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array('vbmusic','vbmusic_latest'
);

// pre-cache templates used by specific actions
$actiontemplates = array();

// ######################### REQUIRE BACK-END ############################
// if your page is outside of your normal vb forums directory, you should change directories by uncommenting the next line
// chdir ('/path/to/your/forums');
require_once('./global.php');

// ################################################## #####################
// ######################## START MAIN SCRIPT ############################
// ################################################## #####################

$navbits = construct_navbits(array('' => 'Music'));
$navbar = render_navbar_template($navbits);



// ###### YOUR CUSTOM CODE GOES HERE #####



$musicalbum = $db->query_read("
SELECT al.alid, al.alname, al.arid, al.added, al.cvrsaved, ar.arname FROM " . TABLE_PREFIX. " album al
LEFT JOIN artist ar ON ar.arid=al.arid
ORDER BY al.alid DESC LIMIT 5
");

$musicals = array();

if ($db->num_rows($musicalbum))
{
while ($musical = $db->fetch_array($musicalbum))
{
$musicals[] = $musical;

}


}
$db->free_result($musicalbum);





$templater = vB_Template::create('vbmusic_latest');
$templater->register('musicals', $musicals);
// $templater->register('malbumid', $malbumid);
// $templater->register('malbumname', $malbumname);
//$templater->register('malbumarname', $malbumarname);
// $templater->register('malbumarid', $malbumarid);
$vbmusic_latest = $templater->render();


//$malbumid = $musical['alid'];
//$malbumname = $musical['alname'];
//$malbumarname = $musical['arname'];
//$malbumarid = $musical['arid'];
//$mcvrsaved = $musical['cvrsaved'];



$musicstat = $db->query_read("SELECT nr_artists, nr_albums,nr_tracks FROM " . TABLE_PREFIX ." stats");
while ($music_stat = $db->fetch_array($musicstat))
{
$statar = $music_stat['nr_artists'];
$statal = $music_stat['nr_albums'];
$statt = $music_stat['nr_tracks'];
}




// ###### NOW YOUR TEMPLATE IS BEING RENDERED ######

$templater = vB_Template::create('vbmusic');
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('vbmusic_latest', $vbmusic_latest);
$templater->register('cvrsaved', $cvrsaved);
$templater->register('statar', $statar);
$templater->register('statal', $statal);
$templater->register('statt', $statt);
print_output($templater->render());



?>

{vb:stylevar htmldoctype}
<html xmlns="http://www.w3.org/1999/xhtml" dir="{vb:stylevar textdirection}" lang="{vb:stylevar languagecode}" id="vbulletin_html">
<head>
<title>{vb:raw vboptions.bbtitle} - {vb:raw pagetitle}</title>
{vb:raw headinclude}
{vb:raw headinclude_bottom}
</head>
<body>

{vb:raw header}

{vb:raw navbar}


<br /><br /><br />

<div class="colmask leftmenu">
<div class="colleft">
<div class="col1">
<!-- Column 1 start -->



<div class="audio_head">Newest Mixtapes</div>
<div class="audio_body">

<vb:each from="musicals" value="musical">

{vb:raw musical.alid}
{vb:raw musical.alname}

</vb:each>
<!--{vb:raw vbmusic_latest}-->
</div>


<!-- Column 1 end -->
</div>
<div class="col2">
<!-- Column 2 start -->
<div class="audio_head">Menu</div>
<div class="audio_body">
<table align="center" cellspacing="0" cellpadding="0" width="100%" >
<tr>
<td valign="top" align="center">
<table align="center" cellspacing="5" cellpadding="0" width="100%">
<tr>
<td align="left"><a href="vbmusic.php" title="" alt="">- Music Home</a></td>
</tr>
<tr>
<td align=\"left\"><a href=\"browse.php?typ=artist\" title=\"\" alt=\"\">- Browse Artist</a></td>
</tr>
<tr>
<td align=\"left\"><a href=\"browse.php?typ=album\" title=\"\" alt=\"\">- Browse Albums</a></td>
</tr>
<tr>
<td align=\"left\"><a href=\"browse.php?typ=genre\" title=\"\" alt=\"\">- Browse Genre</a></td>
</tr>

<tr>
<td align=\"left\"><a href=\"import.php\" title=\"\" alt=\"\">- Import Albums</a></td>
</tr>
<tr>
<td align=\"left\"><a href=\"config.php\" title=\"\" alt=\"\">- Config Settings</a></td>
</tr>
<tr>
<td align=\"left\"><a href=\"usr_list.php\" title=\"\" alt=\"\">- Userlist</a></td>
</tr>
<tr>
<td align=\"left\"><a href=\"showlog.php\" title=\"\" alt=\"\">- Show Log</a></td>
</tr>
<tr>
<td align=\"left\"><a href=\"stats.php\" title=\"\" alt=\"\">- Statistics</a></td>
</tr>
<tr>
<td align=\"left\"><a href=\"create_artist.php\" title=\"\" alt=\"\">- Create artist</a></td>
</tr>
<tr>
<td align=\"left\"><a href=\"del_artist.php\" title=\"\" alt=\"\">- Delete artist</a></td>
</tr>


</table>
</td>
</tr>
</table>
</div><br />
<div class="audio_head">Statistics</div>
<div class="audio_body">
<table align="center" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td valign="top" align="center">
<table align="center" cellspacing="0" cellpadding="0" width="90%">
<tr>
<td class="data3" align="left"><b>Albums </b>&nbsp</td>
<td class="data1" align="left">{vb:raw statal}</td></tr>
<tr><td class="data3" align="left"><b>Artists </b>&nbsp</td>
<td class="data1" align="left">{vb:raw statar}</td></tr>
<tr><td class="data3" align="left"><b>Songs </b>&nbsp</td>
<td class="data1" align="left">{vb:raw stats}</td></tr>
</table>
</td>
</tr>
</table>
</div>



<!-- Column 2 end -->
</div>
</div>
</div>
{vb:raw footer}
</body>
</html>

Pandemikk
04-09-2012, 04:55 AM
Is the HTML you're showing me from template: vbmusic_latest, or from template: vbmusic

If it's from template: vbmusic, then you need to add: $templater->register('musicals', $musicals); to the register in that template.

Preech
04-09-2012, 05:24 AM
Well god damn, I knew it was something simple that I was over looking. That was it.

I see that your site is music related as well. I'm currently deployed to Afghanistan right now. If it's not to much, can I continue to ask you for help from time to time. I get about six hours of free time each day. If possible how may I contact you.

Pandemikk
04-09-2012, 05:44 AM
Feel free to PM me.