View previous topic :: View next topic |
Author |
Message |
crxgames Smarty Rookie
Joined: 13 Nov 2004 Posts: 16
|
Posted: Sat Nov 13, 2004 11:13 pm Post subject: [solved]smarty->append |
|
|
I am pulling links from my database, and I am trying to append them to the smarty value to be outputted in the template with a {section}.
Here is the function:
[php:1:5ce802da64]
<?php
function outputNavs()
{
global $db, $template, $config;
$template->assign('links','');
$sql = "SELECT * FROM ". HEADER_NAV_TABLE ." ORDER BY nav_id";
if(!$result = $db->query($sql))
{
message_die(GENERAL_ERROR,"Couldn't SQL the header navigation links from the database in " .__FILE__.' on line '.__LINE__.' <br />'.$sql.'<br />'.mysql_error());
}
else
{
//pick up here
while( $row = $db->fetch_row($result))
{
$template->append('links',array('title' => $row['nav_name'], 'url' => $row['nav_link']));
}
}
}
?>[/php:1:5ce802da64]
and in my template I have:
Code: |
{section name=cycle loop=$links}
{strip}
<a href="{$links[cycle].url}">{$links[cycle].title}</a>
{/strip}
{/section}
{$ACP}
|
NOTE: $ACP is a link to my admin control panel.
Why is this not displaying, it should make a cell via CSS.
But the data is not being iterated through to be shown. What am I doing wrong? _________________ My site | My CMS Development site
Last edited by crxgames on Mon Nov 15, 2004 12:44 am; edited 1 time in total |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Nov 14, 2004 12:31 am Post subject: |
|
|
Try this instead:
[php:1:d5a1dee1b7]<?php
function outputNavs()
{
global $db, $template, $config;
$sql = "SELECT * FROM ". HEADER_NAV_TABLE ." ORDER BY nav_id";
$links = array();
if(!$result = $db->query($sql))
{
message_die(GENERAL_ERROR,"Couldn't SQL the header navigation links from the database in " .__FILE__.' on line '.__LINE__.' <br />'.$sql.'<br />'.mysql_error());
}
else
{
//pick up here
while( $row = $db->fetch_row($result))
{
$links[] = array('title' => $row['nav_name'], 'url' => $row['nav_link']);
}
}
$template->assign('links', $links);
}
?>[/php:1:d5a1dee1b7]
Building arrays using append() in a loop is less efficient than building the entire array and then passing it via a single assign. |
|
Back to top |
|
crxgames Smarty Rookie
Joined: 13 Nov 2004 Posts: 16
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Nov 14, 2004 1:18 am Post subject: |
|
|
Put a {debug} into your template. It seems like the $links isn't being populated. |
|
Back to top |
|
crxgames Smarty Rookie
Joined: 13 Nov 2004 Posts: 16
|
Posted: Sun Nov 14, 2004 1:22 am Post subject: |
|
|
It is being populated...
only thing I can guess is that I am not going through the data correctly in the template. _________________ My site | My CMS Development site |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Nov 14, 2004 3:01 am Post subject: |
|
|
Quote: | It is being populated... |
Okay--and you verified it is populated correctly, I imagine. Your template looks okay to me so a big hmmm. What is the loop actually producing--nothing at all? Ensure that error reporting is pumped up and turn template debugging on -- I wonder if you are getting notices or other errors. |
|
Back to top |
|
crxgames Smarty Rookie
Joined: 13 Nov 2004 Posts: 16
|
Posted: Sun Nov 14, 2004 3:07 am Post subject: |
|
|
It was getting the data to the debug. I'll set the debug on smarty on. _________________ My site | My CMS Development site |
|
Back to top |
|
crxgames Smarty Rookie
Joined: 13 Nov 2004 Posts: 16
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Nov 14, 2004 3:32 am Post subject: |
|
|
According to the debug window, there isn't a variable called $links. There does seem to be one that fits the bill that is called $nav_links. Could that be it? |
|
Back to top |
|
crxgames Smarty Rookie
Joined: 13 Nov 2004 Posts: 16
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Nov 14, 2004 4:17 am Post subject: |
|
|
...and you made your template read from $nav_links? Please, keep your sample code that you post here as close to the real thing as possible.
Anyhow, I'm getting the feeling that I will have to ask you for each piece of information required to solve this little puzzle. As I have no interest in doing that, I suggest you consider providing more details as I can't see what is wrong based on what you posted so far.
Good luck! |
|
Back to top |
|
crxgames Smarty Rookie
Joined: 13 Nov 2004 Posts: 16
|
Posted: Sun Nov 14, 2004 4:21 am Post subject: |
|
|
Problem is the array made in the outputNavs function is not being iterated through and displayed, but has data.
Current function:
[php:1:eea11795f6]<?php
//////////////////////////////////////
//Function: outputNavs()
//Ouputs the header navigation links
//////////////////////////////////////
function outputNavs()
{
global $db, $template, $config;
$sql = "SELECT * FROM ". HEADER_NAV_TABLE ." ORDER BY nav_id";
$links = array();
if(!$result = $db->query($sql))
{
message_die(GENERAL_ERROR,"Couldn't SQL the header navigation links from the database in " .__FILE__.' on line '.__LINE__.' <br />'.$sql.'<br />'.mysql_error());
}
else
{
//pick up here
while( $row = $db->fetch_row($result))
{
$links[] = array('title' => $row['nav_name'], 'url' => $row['nav_link']);
}
}
$template->assign('nav_links', $links);
}
?>[/php:1:eea11795f6]
and current tpl:
Code: |
{section name=cycle loop=$nav_links}
{strip}
<a href="{$nav_links[cycle].url}">{$nav_links[cycle].title}</a>
{/strip}
{/section}
{$ACP}
|
_________________ My site | My CMS Development site |
|
Back to top |
|
crxgames Smarty Rookie
Joined: 13 Nov 2004 Posts: 16
|
Posted: Sun Nov 14, 2004 4:13 pm Post subject: |
|
|
Here is the table structure if that helps.
Code: |
CREATE TABLE `portal_navs` (
`nav_id` int(8) NOT NULL auto_increment,
`nav_name` varchar(128) default NULL,
`nav_link` varchar(255) NOT NULL default '',
PRIMARY KEY (`nav_id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
#
# Dumping data for table `portal_navs`
#
INSERT INTO `portal_navs` (`nav_id`, `nav_name`, `nav_link`) VALUES (1, 'Test link to acp', '/admin/index.php');
|
Should I be using the foreach? _________________ My site | My CMS Development site |
|
Back to top |
|
crxgames Smarty Rookie
Joined: 13 Nov 2004 Posts: 16
|
Posted: Sun Nov 14, 2004 7:01 pm Post subject: |
|
|
I have tried the following foreach to just get it to output with no success.
Code: |
{foreach name=outer item=contact from=$contacts}
{foreach key=key item=item from=$contact}
{$key}: {$item}<br>
{/foreach}
{/foreach}
|
_________________ My site | My CMS Development site |
|
Back to top |
|
crxgames Smarty Rookie
Joined: 13 Nov 2004 Posts: 16
|
Posted: Sun Nov 14, 2004 8:50 pm Post subject: |
|
|
Someone has to know, I need to have this working by the end of the day. _________________ My site | My CMS Development site |
|
Back to top |
|
|