Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to write an array for keyed list of items in a select list

Posted on 2009-07-13
5
Medium Priority
?
214 Views
Last Modified: 2012-08-14
Hello, I am trying to write a list of allowed items for a form's select list. The CMS system I am on says it wants "a keyed array of items" using the following format:
-----------------------------------------------
The default value PHP code returned an incorrect value.
Expected format:

return array(
  0 => array('value' => value for value),
  // You'll usually want to stop here. Provide more values
  // if you want your 'default value' to be multi-valued:
  1 => array('value' => value for value),
  2 => ...
);

Returned value:

----------------------

Here's what I'm trying to do.

For each item, the key (aka "label") is a name like "kilim" or "gabbeh" and the value is itself a php snippet the looks up data from a database. It is

$term = (array) taxonomy_get_term(64); print "<strong>About" . " ". $term[name] . ":</strong> " . $term[description];

The only thing that changes for each item is the number (64).

Here is the list of keys and corresponding values:

Khorjuns   64
Kilims         4
Kilim pillows 63
Jajims      6
Suzani    99
Sofreh    119
Qashqai     78
Bakhtiari     108
Senneh      102
Shahsavan   80
Afshar       79
Qum      109
Azarbaijan    100
Turkmen     89
Ghoochan    110
Pile       666

I've written the following code and it works when I put it in a text area.

$des [Khorjuns] = $term = (array) taxonomy_get_term(64); print "<strong>About" . " ". $term[name] . ":</strong> " . $term[description];
$des [Kilims] = $term = (array) taxonomy_get_term(4); print "<strong>About" . " ". $term[name] . ":</strong> " . $term[description];
foreach ($des as $value) {

echo $value;

But when I put it in to create a select list, it won't take it, referring me to the aforementioned format it wants. Plus I think my format is needlessly repeating a whole bunch things. The bottom line is I've never really understood array  in php.

Can someone help me how to do this. Please write it out for me or at least write out enough for me to get started. Just saying "you need to use the ... function" will go over my head. Thank you for your patience.
0
Comment
Question by:polaatx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24844242
Not sure I really understand what the CMS wants, but I can provide you a code sample that might be useful.  Back in a moment... ~Ray
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 24844377
See if this helps understand the use of associative arrays in PHP.  Best regards, ~Ray
<?php // RAY_array_sample_17.php
error_reporting(E_ALL);
echo "<pre>\n";
 
// DEFINE AN EMPTY ARRAY
$arr = array();
 
// ADD SOME KEY => VALUE PAIRS TO THE ARRAY
$arr["Monday"]    = "Lamb Stew";
$arr["Tuesday"]   = "Chicken";
$arr["Wednesday"] = "Soup";
$arr["Thursday"]  = "Roast Beef";
$arr["Friday"]    = "Fresh Fish";
 
// VISUALIZE THE ARRAY
var_dump($arr);
 
// SELECT AN ELEMENT FROM THE ARRAY
echo "\n";
echo "ON THURSDAY WE ARE GOING TO HAVE ";
echo $arr["Thursday"];
echo "\n";
 
// ITERATE OVER THE ARRAY
foreach ($arr as $key => $val)
{
   echo "\n ON $key WE EAT $val";
   if ($key == "Thursday") echo " YUM!";
}
echo "\n\n";
 
// CREATE SELECT-LIST ELEMENTS FROM THE ARRAY
$select_list = "<select>\n";
foreach ($arr as $key => $val)
{
    $select_list .= "<option value=\"$key\">$val</option>\n";
}
$select_list .= "</select>\n";
 
// SHOW THE SELECT LIST
echo htmlentities($select_list);

Open in new window

0
 

Author Comment

by:polaatx
ID: 24845686
Thank you for your help. I will respond as soon as I get a chance to work on it.
0
 

Author Closing Comment

by:polaatx
ID: 31602997
Very good example. Thank you.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24959488
Thanks for the points - it's a great question, ~Ray
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question