|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
bkatsma Smarty n00b
Joined: 30 Jun 2003 Posts: 3
|
Posted: Mon Jun 30, 2003 2:09 pm Post subject: Problems passing Varibles |
|
|
For some reason I am having problems getting a varible to pass correctly into my smarty template.
[php:1:23c02a6cda]<?php
php code:
//Create an array of main category ids
while(list($cid) = $xoopsDB->fetchRow($maincatids))
{
$tid = $myts->makeTboxData4Show($cid);
//Set max counter to count how many subcategories for array. This will tell us when we hit the last item
$max = 0;
//query DB for Main categories and ids
$maincat = $xoopsDB->query("SELECT cid, title FROM xlts_vnd_cat WHERE pid = 0 and cid = $tid");
//*********************MAIN CATEGORIES**********************
//create an array for Main categories and assign them to the template
while(list($cid, $title) = $xoopsDB->fetchRow($maincat))
{
$mcid = $myts->makeTboxData4Show($cid);
$mtitle = $myts->makeTboxData4Show($title);
$xoopsTpl->append('tid', array('tid' => $tid));
$xoopsTpl->append('maincats', array('cid' => $cid, 'title' => $title));
//*********************SUB CATEGORIES**********************
//create an array for Sub categories and assign them to the template
$subcat = $xoopsDB->query("SELECT cid, title FROM xlts_vnd_cat WHERE pid = $cid");
while(list($cid, $title) = $xoopsDB->fetchRow($subcat))
{
$cid = $myts->makeTboxData4Show($cid);
$title = $myts->makeTboxData4Show($title);
$xoopsTpl->append("$mtitle" , array('cid' => $cid, 'title' => $title));
$max ++;
}//End main Sub Categories
}//End main Categories
}//End main Category id's
?>[/php:1:23c02a6cda]
I am creating an array foreach mancategory.
There arrays are as follows:
Code: |
Arrays created:
{$CAD Systems} Array (4)
0 => Array (2)
cid => 12
title => CATIA
1 => Array (2)
cid => 13
title => SDRC
2 => Array (2)
cid => 14
title => Unigrapics
3 => Array (2)
cid => 16
title => PDGS
{$Data Formats} Array (4)
0 => Array (2)
cid => 15
title => IGES
1 => Array (2)
cid => 17
title => STEP
2 => Array (2)
cid => 18
title => PARTPP
3 => Array (2)
cid => 19
title => CATIA Export
{$Data Package/Compression} Array (4)
0 => Array (2)
cid => 25
title => PK ZIP
1 => Array (2)
cid => 26
title => UNIX Compress
2 => Array (2)
cid => 27
title => TAR
3 => Array (2)
cid => 28
title => GZIP
{$Media Type} Array (5)
0 => Array (2)
cid => 20
title => 4 mm Tape
1 => Array (2)
cid => 21
title => 8 mm Tape
2 => Array (2)
cid => 22
title => 3 1/2" Floppy
3 => Array (2)
cid => 23
title => CDROM
4 => Array (2)
cid => 24
title => FTP
{$maincats} Array (4)
0 => Array (2)
cid => 8
title => CAD Systems
1 => Array (2)
cid => 9
title => Data Formats
2 => Array (2)
cid => 10
title => Media Type
3 => Array (2)
cid => 11
title => Data Package/Compression
{$tid} Array (4)
0 => Array (1)
tid => 8
1 => Array (1)
tid => 9
2 => Array (1)
tid => 10
3 => Array (1)
tid => 11
|
Template is as follows:
Code: |
<{section name=j loop=$maincats}>
<!--Start new link loop -->
<tr class=head>
<td colspan = "6" width="100%"><b><{$maincats[j].title}></b></td>
</tr>
<tr>
<{section name=i loop=9}>
<{assign var=pre value=$maincats[j].title}>
<td class='even' ><input type="checkbox" name="<{$maincats[j].title}>[]" value = "<{$tid[i].tid}>" </td>
<{if $max == $count}>
<{if $count % 3 == 1}>
<td width = "33%" colspan = "5" class='even' ><{$pre.title}></td>
<{elseif $count % 3 == 2}>
<td width = "33%" colspan = "3" class='even' ><{$pre.title}></td>
<{else}>
<td width = "33%" class='even' ><{$pre.title}></td>
<{/if}>
<{else}>
<td width = "33%" class='even' ><{$pre.title}></td>
<{/if}>
<{if $smarty.section.i.rownum is div by 3}>
</tr><tr>
<{/if}>
<{/section}>
<{/section}>
<!--End new link loop -->
|
Which creates this HTML
Code: |
<!--Start new link loop -->
<tr class=head>
<td colspan = "6" width="100%"><b>CAD Systems</b></td>
</tr>
<tr>
<td class='even' ><input type="checkbox" name="CAD Systems[]" value = "8" </td>
<td width = "33%" class='even' >C</td>
<td class='even' ><input type="checkbox" name="CAD Systems[]" value = "9" </td>
<td width = "33%" class='even' >C</td>
<td class='even' ><input type="checkbox" name="CAD Systems[]" value = "10" </td>
<td width = "33%" class='even' >C</td>
</tr><tr>
<td class='even' ><input type="checkbox" name="CAD Systems[]" value = "11" </td>
<td width = "33%" class='even' >C</td>
<td class='even' ><input type="checkbox" name="CAD Systems[]" value = "" </td>
<td width = "33%" class='even' >C</td>
<td class='even' ><input type="checkbox" name="CAD Systems[]" value = "" </td>
<td width = "33%" class='even' >C</td>
</tr><tr>
<td class='even' ><input type="checkbox" name="CAD Systems[]" value = "" </td>
<td width = "33%" class='even' >C</td>
<td class='even' ><input type="checkbox" name="CAD Systems[]" value = "" </td>
<td width = "33%" class='even' >C</td>
<td class='even' ><input type="checkbox" name="CAD Systems[]" value = "" </td>
<td width = "33%" class='even' >C</td>
</tr><tr>
<!--Start new link loop -->
<tr class=head>
<td colspan = "6" width="100%"><b>Data Formats</b></td>
</tr>
<tr>
<td class='even' ><input type="checkbox" name="Data Formats[]" value = "8" </td>
<td width = "33%" class='even' >D</td>
<td class='even' ><input type="checkbox" name="Data Formats[]" value = "9" </td>
<td width = "33%" class='even' >D</td>
<td class='even' ><input type="checkbox" name="Data Formats[]" value = "10" </td>
<td width = "33%" class='even' >D</td>
</tr><tr>
<td class='even' ><input type="checkbox" name="Data Formats[]" value = "11" </td>
<td width = "33%" class='even' >D</td>
<td class='even' ><input type="checkbox" name="Data Formats[]" value = "" </td>
<td width = "33%" class='even' >D</td>
<td class='even' ><input type="checkbox" name="Data Formats[]" value = "" </td>
<td width = "33%" class='even' >D</td>
</tr><tr>
<td class='even' ><input type="checkbox" name="Data Formats[]" value = "" </td>
<td width = "33%" class='even' >D</td>
<td class='even' ><input type="checkbox" name="Data Formats[]" value = "" </td>
<td width = "33%" class='even' >D</td>
<td class='even' ><input type="checkbox" name="Data Formats[]" value = "" </td>
<td width = "33%" class='even' >D</td>
</tr><tr>
<!--Start new link loop -->
<tr class=head>
<td colspan = "6" width="100%"><b>Media Type</b></td>
</tr>
<tr>
<td class='even' ><input type="checkbox" name="Media Type[]" value = "8" </td>
<td width = "33%" class='even' >M</td>
<td class='even' ><input type="checkbox" name="Media Type[]" value = "9" </td>
<td width = "33%" class='even' >M</td>
<td class='even' ><input type="checkbox" name="Media Type[]" value = "10" </td>
<td width = "33%" class='even' >M</td>
</tr><tr>
<td class='even' ><input type="checkbox" name="Media Type[]" value = "11" </td>
<td width = "33%" class='even' >M</td>
<td class='even' ><input type="checkbox" name="Media Type[]" value = "" </td>
<td width = "33%" class='even' >M</td>
<td class='even' ><input type="checkbox" name="Media Type[]" value = "" </td>
<td width = "33%" class='even' >M</td>
</tr><tr>
<td class='even' ><input type="checkbox" name="Media Type[]" value = "" </td>
<td width = "33%" class='even' >M</td>
<td class='even' ><input type="checkbox" name="Media Type[]" value = "" </td>
<td width = "33%" class='even' >M</td>
<td class='even' ><input type="checkbox" name="Media Type[]" value = "" </td>
<td width = "33%" class='even' >M</td>
</tr><tr>
<!--Start new link loop -->
<tr class=head>
<td colspan = "6" width="100%"><b>Data Package/Compression</b></td>
</tr>
<tr>
<td class='even' ><input type="checkbox" name="Data Package/Compression[]" value = "8" </td>
<td width = "33%" class='even' >D</td>
<td class='even' ><input type="checkbox" name="Data Package/Compression[]" value = "9" </td>
<td width = "33%" class='even' >D</td>
<td class='even' ><input type="checkbox" name="Data Package/Compression[]" value = "10" </td>
<td width = "33%" class='even' >D</td>
</tr><tr>
<td class='even' ><input type="checkbox" name="Data Package/Compression[]" value = "11" </td>
<td width = "33%" class='even' >D</td>
<td class='even' ><input type="checkbox" name="Data Package/Compression[]" value = "" </td>
<td width = "33%" class='even' >D</td>
<td class='even' ><input type="checkbox" name="Data Package/Compression[]" value = "" </td>
<td width = "33%" class='even' >D</td>
</tr><tr>
<td class='even' ><input type="checkbox" name="Data Package/Compression[]" value = "" </td>
<td width = "33%" class='even' >D</td>
<td class='even' ><input type="checkbox" name="Data Package/Compression[]" value = "" </td>
<td width = "33%" class='even' >D</td>
<td class='even' ><input type="checkbox" name="Data Package/Compression[]" value = "" </td>
<td width = "33%" class='even' >D</td>
</tr><tr>
<!--End new link loop -->
|
For come reason the result of my Varible in smarty is producing the first character.
This line display's correctly
"<td colspan = "6" width="100%"><b><{$maincats[j].title}></b></td>"
But this one does not
<{assign var=pre value=$maincats[j].title}>
It appears it is only grabing the first character.
Note this code worked once!!
Thank you |
|
Back to top |
|
that_guy_gomer Smarty n00b
Joined: 08 Jan 2004 Posts: 2
|
Posted: Thu Jan 08, 2004 3:48 am Post subject: Me too |
|
|
I have the exact same thing happeneing to me, except I chose to use foreach loops, but problem is the same. I am getting the first character of each value, instead of the entire value.
Here is my php:
Code: | foreach ($rss->getItems() as $item) {
$smarty->append("feed",array("link"=>$item['link'],
"title"=>$item['title'],
"desc"=>$item['description']));
}
|
Here is the template:
Code: | {foreach name=outer item=content from=$feed }
{foreach item=item from=$content}
<tr bgcolor="{cycle values="#eeeeee,#d0d0d0"}">
<td><a href="{$item.link}">{$item.title}</a></td>
</tr>
<tr>
<td>{$item.desc}</td>
</tr>
{/foreach}
{foreachelse}
|
It's the strangest thing. I thought that I did everything correctly, yet I only get the first characters in each variable.
Any ideas? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Jan 08, 2004 8:06 am Post subject: |
|
|
that_guy_gomer: you want to use $content.title not $item.title. $item is a string in your example, not an array.
bkatsma: i'm sure in your example the problem is the same somewhere.
if you access a string like $string.foo you get the first character. actually you get the nth character, but n is "foo" which is zero when casted to an integer. |
|
Back to top |
|
that_guy_gomer Smarty n00b
Joined: 08 Jan 2004 Posts: 2
|
Posted: Fri Jan 09, 2004 12:32 am Post subject: |
|
|
Thank you so much. That was driving me nuts. I'm still not 100% clear why that's the case. I might have missed it in the documentation. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Jan 09, 2004 8:32 am Post subject: |
|
|
it's a side effect of php. smarty translates variables {$foo.bar} to php-code like "$foo['bar']" .
smarty does not (and can not check) check during translation if $foo is an array.
if $foo is an array everything is fine.
if $foo is a string php uses the value inside the [] as an index n and returns the nth character of the string. 'bar' is casted to zero, so it's $foo[0] for php, and that's the first character in the string $foo.
BTW don't use the $foo[$i] notation for strings in you php-code, yourself. it's disencouraged and $foo{$i} should be used to access a string char by char. a string is not an array in php! |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|