PDA

View Full Version : repeating an array from db in a template??


Jelmertjee
11-06-2007, 09:18 AM
I'm creating a simple news mod for my site but I'm having some problems.. I can access the database and get the threads I need, but don't know how to repeat them in a template (eg. using a similar technique as $threadbits, $adv_portal_newsbits). Here's the code:

// get main news page templates and data
if(!isset($_GET['do']))
{
$news_limit = 10;
$news_forumid = 15;
$news_ids = array();

$get_news_ids = $db->query_read("
SELECT threadid, firstpostid, dateline FROM " . TABLE_PREFIX . "thread
WHERE visible = 1
AND forumid = " . $news_forumid . "
ORDER BY dateline DESC
LIMIT 10
");

while ($db_ids = $db->fetch_array($get_news_ids))
{
$news_ids[] = $db_ids['threadid'];
}

$get_news = $db->query_read("SELECT threadid, title, firstpostid, forumid, replycount, postusername, dateline
FROM " . TABLE_PREFIX . "thread
WHERE threadid IN(". implode(',', $news_ids) .")
ORDER BY dateline
");

$newscount = 0;

while ($db_array = $db->fetch_array($get_news))
{
$newscount++;
$news['title'] = $db_array['title'];
$news['username'] = $db_array['postusername'];
$news['replycount'] = $db_array['replycount'];
eval('$news_bits = "' . fetch_template('news_bits') . '";');
}
eval('print_output("' . fetch_template('news_main') . '");');
}

in the main template I add the $news_bits variable, it does work but only shows the info for one thread.

the news_bits template contains:

title: $news[title]<br />
author: $news[username]<br />
replies: $news[replycount]<br />

I think this shouldn't be too hard to solve, I just don't know how to go about it, any help is appreciated.

--------------- Added 1194350102 at 1194350102 ---------------

I've done some basic testing, and if I use echo it does work, but of course that's not what i'm after, here's the "working" code:

while ($db_array = $db->fetch_array($get_news))
{
$newscount++;
$news['title'] = $db_array['title'];
$news['username'] = $db_array['postusername'];
$news['replycount'] = $db_array['replycount'];
echo $news['title'];
}

this just provides an empty page with the thread titles of course, but does show the mysql works properly.

--------------- Added 1194355336 at 1194355336 ---------------

I've managed to fix the problem by using some code from this article (https://vborg.vbsupport.ru/showthread.php?t=108725), instead of using:

eval('$news_bits = "' . fetch_template('news_bits') . '";');

I used:
eval('$news_bits .= "' . fetch_template('news_bits') . '";');

.= instead of = so the array is added each time instead of being replaced, duh!