3

I'm basically looking to simply print out each of the allowed values in a CCK field..

i know the allowed values are stored inside a text field within the table: 'content_node_field'.

the values are then stored within 'global_settings'

I'm looking to somehow print out each individual allowed value using a PHP loop.

however with all values being stored within one text field.. im finding it hard to print out each value individually.

2 Answers2

1

Something like this should do the trick.

// Get the global_settings like you described.
$serialized_data = db_result(db_query("..."));
// Unserialize the data.
$unserialized_data = unserialize($serialized_data)
// Foreach the allowed values.
$values = array();
foreach(explode("\n", $unserialized_data['allowed_values']) as $value) {
  $values[] = $value;
}
googletorp
  • 33,075
  • 15
  • 67
  • 82
  • sorry, having a few problems with the foreach loop. getting this error: warning: Invalid argument supplied for foreach() in C:\wamp\www\sites\all\modules\custom\marli\marli.admin.inc on line 11. where it says 'allowed_values' ive put the field 'global_settings' (which containts the data. –  Apr 20 '10 at 13:32
  • Sorry to be a complete pain.. how would i be able to store each individual value into a variable? for example: $value = array(); $a = $value['a']; $b = $value['b']; i feel like im close to what im trying to achieve! –  Apr 21 '10 at 09:05
0

If I am getting your question right, you can create PHP arrays by simply suffixing the [] to the names of the fields, so for example:

<input type="text" name="myname[]" />

Now you can get the values of the array like this:

foreach($myname as $value)
{
  echo $value . '<br />';
}

Update Based On Comment:

You can use the json_decode function to convert your data to array and then manipulate accordingly:

Example:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json, true));
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • Within the field global_settings contains the multiple values.. example: a:4:{s:15:"text_processing";s:1:"0";s:10:"max_length";s:0:"";s:14:"allowed_values";s:17:"A* A B C D E";s:18:"allowed_values_php";s:0:"";} im looking to output each grade (a, b, c, d, e) and strip the rest away now i know there is str_replace for this however im doing this on multiple fields where the contents do vary. –  Apr 20 '10 at 10:37