473,409 Members | 2,022 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

[smarty] addressing associative array

Given this assoc array:

$dtl_array[0]['uom_array'][0][0]='key1';
$dtl_array[0]['uom_array'][0][1]='value1';
$dtl_array[0]['uom_array'][1][0]='key2';
$dtl_array[0]['uom_array'][1][1]='value2';
$dtl_array[1]['uom_array'][0][0]='key3';
$dtl_array[1]['uom_array'][0][1]='value3';
$dtl_array[1]['uom_array'][1][0]='key4';
$dtl_array[1]['uom_array'][1][1]='value4';
$sample->assign("dtl_array",$dtl_array);
$sample->display();

How could I create a combobox out of $dtl_array in the templates?
The following does not work:

<select name="aname">
{section name=mm start=0 loop=$dtl_array.0.uom_array step=1}
<option value="{$dtl_array.0.uom_array.mm.0}">
{$dtl_array.0.uom_array.mm.1}
{/section}
</select>

--
iTech Consulting Services Limited
Expert of ePOS solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
Apr 25 '07 #1
3 5457
Man-wai Chang wrote:
Given this assoc array:

$dtl_array[0]['uom_array'][0][0]='key1';
$dtl_array[0]['uom_array'][0][1]='value1';
$dtl_array[0]['uom_array'][1][0]='key2';
$dtl_array[0]['uom_array'][1][1]='value2';
$dtl_array[1]['uom_array'][0][0]='key3';
$dtl_array[1]['uom_array'][0][1]='value3';
$dtl_array[1]['uom_array'][1][0]='key4';
$dtl_array[1]['uom_array'][1][1]='value4';
$sample->assign("dtl_array",$dtl_array);
$sample->display();

How could I create a combobox out of $dtl_array in the templates?
The following does not work:

<select name="aname">
{section name=mm start=0 loop=$dtl_array.0.uom_array step=1}
<option value="{$dtl_array.0.uom_array.mm.0}">
{$dtl_array.0.uom_array.mm.1}
{/section}
</select>
Why do you have such a strange array? It's not as easy to iterate through its values if it's counted in base two, you should alter $dtl_array through PHP into something easier to deal with first:
<?php
$dtl_array = generate_bizarre_array();

$cool_array = array();

$SoFdtl_array = count( $dtl_array );
for( $i = 0; $i < $SoFdtl_array; ++$i )
{
$SoFdtl_array_I_uom_array = count( $dtl_array[$i]['uom_array'] );
for( $j = 0; $j < $SoFdtl_array_I_uom_array; ++$j )
$cool_array[] = array( 'key' =$dtl_array[$i]['uom_array'][$j][0],
'value' =$dtl_array[$i]['uom_array'][$j][1] );
}

$sample->assign( 'coolness', $cool_array );
$sample->display( 'it.tpl' );
?>

(it.tpl:)
<select name="JohnDoe">
{foreach from=$coolness item="opt"}
<option value="{$opt.key}">{$opt.value}</option>
{/foreach}
</select>

This assumes that however you get that binary-indexed array, it comes from a dynamic source. Otherwise, the PHP code would be much simpler. {foreach} is also easier to use (IMO). I suggest you also throw in a {foreachelse} if this really is a dynamic source.

-Mike PII
Apr 25 '07 #2
$SoFdtl_array = count( $dtl_array );
for( $i = 0; $i < $SoFdtl_array; ++$i )
{
$SoFdtl_array_I_uom_array = count( $dtl_array[$i]['uom_array'] );
for( $j = 0; $j < $SoFdtl_array_I_uom_array; ++$j )
$cool_array[] = array( 'key' =$dtl_array[$i]['uom_array'][$j][0],
'value' =$dtl_array[$i]['uom_array'][$j][1] );
}
Thanks for the suggestion.

--
iTech Consulting Services Limited
Expert of ePOS solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
Apr 26 '07 #3
<select name="aname">
{section name=mm start=0 loop=$dtl_array.0.uom_array step=1}
<option value="{$dtl_array.0.uom_array.mm.0}">
{$dtl_array.0.uom_array.mm.1}
{/section}
</select>
I found the solution after a bit of trial-and-error. it needs a
temporary variable as a bridge:

<select name="aname">
{section name=mm start=0 loop=$dtl_array.0.uom_array step=1}
{assign var=ha value=$smarty.section.mm.index}
<option value="{$dtl_array.0.uom_array.$ha.0}">
{$dtl_array.0.uom_array.$ha.1}
{/section}
</select>
--
iTech Consulting Services Limited
Expert of ePOS solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
Apr 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
Does Smarty support assigning multidimensional arrays? I've tried to get it working, but just can't get it. can someone help me? this is what I have in my php file: $side_bar = array('link'...
2
by: Brian | last post by:
I have an application that uses an array of values as a "lookup" table and smarty as a template engine. $lookup = "label"; $lookup = "label2"; $lookup = "label"; $lookup = "label2"; In my...
1
by: Richard | last post by:
I need help. I have a smarty based program that I am modifying. I have a smarty template file that consists of smarty and HTML. I need to integrate some PHP database calls into it. My problem...
1
by: Jim Red | last post by:
Hello I have a Smarty(.php.net) Question. I want to create a Navigation with a Smarty Template. How can I display an Array recursively? Do I have to write a Plugin?
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
7
by: Man-wai Chang | last post by:
1. How could concatenate two values into a string in the template? {section name=j start=0 loop=10 step=1} {section name=k start=0 loop=10 step=1} {assign var=xx...
2
dlite922
by: dlite922 | last post by:
difficult to explain, easier to show: Here's what my data looks like on the PHP side: Array ( => Array ( => 20003001 => ABC RESTAURANT
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.