Jump to content

using sql helpers and db connection in tpl files


leemason

Recommended Posts

im trying to populate a menu list with product groups from the db.

 

problem is its not working???

 

this is what i have in my header.tpl:

 

{php}
					$links = select_query('tblproductgroups', 'id,name,order', '');
					$linksarray = array();
					while($link = mysql_fetch_assoc($links)){
						$linksarray[$link['order']] = $link;
					}
					foreach($linksarray as $link){
						echo '<li><a href="cart.php?gid='.$link['id'].'">'.$link['name'].'</a>';	
					}
					{/php}

 

seems simple enough?

 

can anyone tell me why this doesnt work?

 

ive also tried a generic mysql query without the helper function with no luck.

 

i know i can port this into a hook.php file and assign a var, but would like to keep it in the template folder, its for a client so having files all over the place isnt ideal

Link to comment
Share on other sites

{php}
$data = select_querry('tblproductgroups', 'id, name, order', array());
$linksarray = array();
while ($res = mysql_fetch_assoc($data)) {
 $id = $res['id'];
 $linksarray[$id] = $res;
}

foreach ($linksarray as $gid => $link) {
 echo '<li><a href="cart.php?gid='.$gid.'">'.$link['name'].'</a>';
}
{/php}

Link to comment
Share on other sites

nope, still an empty array, nothing printed (and yes i did correct the spelling mistakes).

 

ive used the select query before and you can pass an empty string for the where and it functions the same as passing an empty array.

 

but still nothing your way.

 

does anybody think t may be because i am viewing the theme using the ?systpl= get var, and not using it as the active theme?

Link to comment
Share on other sites

try var_dump($res) within the while loop, see if its returning anything. you can also try using full_query instead and see if that helps. It could be the "order" field, being that "order" is a protected sql command. Though I would think the sql helpers would escape that properly.

 

Is there any particular reason you need the "order" field, or were you just using it as a key? If you're simply wanting to display them in the proper "order" based on the database, you should use the ORDER BY clause anyways and let the array keys assign themselves. Heres what I mean:

 

{php}

$linksarray = array();
$data = select_query('tblproductgroups', 'id, name', array(), 'order', 'ASC');
while ($res = mysql_fetch_assoc($data)) {
 $linksarray[] = $res;
}

foreach ($linksarray as $key => $link) {
 echo '<li><a href="cart.php?gid='.$link['id'].'">'.$link['name'].'</a></li>';
}

{/php}

Link to comment
Share on other sites

Just tested the code I last posted, it works fine for me in the homepage.tpl template (it shouldnt matter what template file you use). I cant imagine defining systpl to something different would have any affect so long as you're specifying the template where the code is at.

Link to comment
Share on other sites

yes mee too.

 

also for anyone who come across this thread in need of help, because of franks solution you dont need to assign to array to get in the order.

 

just echo within the while:

 

{php}
$linksarray = array();
$data = select_query('tblproductgroups', 'id, name', array(), 'order', 'ASC');
while ($res = mysql_fetch_assoc($data)) {
 echo '<li><a href="cart.php?gid='.$res['id'].'">'.$res['name'].'</a></li>';
}
{/php}

Link to comment
Share on other sites

Another thing I'd probably do if it was mine, instead of using echo, just pass the variable into smarty and use a smarty foreach loop.

 

At the top of the template:

{php}
$linksarray = array(); 
$data = select_query('tblproductgroups', 'id, name', array(), 'order', 'ASC');
while ($res = mysql_fetch_assoc($data)) {
 $linkarray[] = $res;
}
$this->_tpl_vars['grouplinks'] = $linkarray;
{/php}

 

Then where ever you want to display the links:

 

{foreach from=$grouplinks key=num item=link}
 <li><a href="cart.php?gid={$link.id}">{$link.name}</a></li>
{/foreach}

 

I tend to keep PHP as separate as possible, so this would be a much cleaner solution ib my eyes.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated