Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6597749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:14:54+00:00 2026-05-25T18:14:54+00:00

I have two arrays. In first array I have Type and Zone field names.

  • 0

I have two arrays. In first array I have Type and Zone field names. These two field names have multiple values. I am using smarty. I want the select name from the first array and options for each dropdown will come from the second array. This is what I have tried :

{foreach from=$field_names item=fld_name}
        <tr>
            <td width="3%" height="15" style="font-weight:bold;height:30px; padding-top:5px;">&nbsp;</td>
            <td width="32%" height="15" valign="middle" style="font-weight:bold;height:30px; padding-top:5px;">&nbsp;{$fld_name}:</td>
            <td width="65%" height="15" valign="middle" style=" padding-top:5px;"> 
                <select name="{$fld_name}" id="{$fld_name}" style="width:95px">
                    <option value="-1">Any</option>
                    {foreach from=$field_values item=fld_val key=key}
                        <option value="{$fld_val.$key.value}">{$fld_val.$key.value}</option>
                    {/foreach}
                </select> 
            </td>
        </tr>
        {/foreach}

This is the output of arrays :

This is the Output of first array:

Array
(
[2] => Type
[1] => Zone
)

This is second array:
Array
(
[0] => Array
(
[0] => Array
(
[productid] => 141
[fieldid] => 2
[value] => Laptop
)

        [1] => Array
            (
                [productid] => 191
                [fieldid] => 2
                [value] => Books
            )

        [2] => Array
            (
                [productid] => 177
                [fieldid] => 2
                [value] => Printer
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [productid] => 141
                [fieldid] => 1
                [value] => 3
            )

        [1] => Array
            (
                [productid] => 191
                [fieldid] => 1
                [value] => 4
            )

        [2] => Array
            (
                [productid] => 177
                [fieldid] => 1
                [value] => 2
            )

    )

)

I am getting the same options in both the dropdown. What Iam doing wrong please help..

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:14:54+00:00Added an answer on May 25, 2026 at 6:14 pm

    First off; the first array has id’s 2 and 1 mapped to respectively Type and Zone. Your first smarty foreach will loop through that array and product Type and Zone as values for the $fld_name variable.

    The second foreach, however, is being done without any relation to the first; so both iterations through that array will be the same.

    I believe I have a similar situation in one of my Smarty templates; but since I’m at work I can’t take a look at them :(.

    Anyways; I believe I made either an object or an array with names as indexes; so in your case, the options for Type would be in the second array under a ‘Type’ index, same for Zone.
    That way, you can foreach through the second array part that belongs to the given key:

    {foreach $field_names as $field_name}
        ...
        {foreach $field_values[$field_name] as $field_value}
            ...
        {/foreach}
    {/foreach}
    

    Your second array would change from having ‘0’ and ‘1’ as indexes for the value arrays, to ‘Type’ and ‘Zone’. Such a construction should be able to work.

    For a codepad example of the above, see http://codepad.org/kzfaYuSd. I believe that somewhat does the same as what you are trying to get done with the arrays. It still means changing the arrays; but otherwise you’ll probably be doing a lot of logic in the smarty template, which really makes them unreadable. Unfortunately codepad doesn’t have smarty; but I know smarty can do the same with the foreach statements :).

    A secondary approach is to create a object for a field, consisting of the field name and field values as properties:

    $items = new array();
    $item1 = new StdObject();
    $item1->field_name = "Type";
    $item1->field_values = new array("Value1", "Value2");
    $items[] = $item1;
    

    And then in smarty (assuming $items is mapped to $items):

    {foreach $items as $item}
        $item->field_name
        {foreach $item->field_values as $value}
             $value
        {/foreach}
    {/foreach}
    

    (ofcourse including your own code around those values :)).

    Codepad illustrating the latter: http://codepad.org/flIhRlmO

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using php with smarty. In php I have two arrays: $code = Array
I have two arrays in PHP. The first array ($author_array) is comprised of user_ids
I have two arrays: Array ( [0] => Array ( [id] => 1 [type]
I have 10 arrays, each consisting of similar type of values. I want to
I have two arrays built while parsing a text file. The first contains the
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two arrays: Array ( [2005] => 0 [2006] => 0 [2007] =>
I have two arrays with time values in them in this format. 00:00:00 which
I have two arrays of values like X,Y,Z and 1,2 There is a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.