Allowed Values list in drupal CCK Fields - php

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.

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;
}

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));

Related

How to break up this json_decoding file in PHP by key/value?

This is my first time coding in PHP. I have this json file (screenshot). json file
It contains a name and sources array. I need to break up this "sources" array by the fields that are in the array (name, url, description, groupfields, and searchfields). I have used the json_decode function but I am stuck on how to break up things in this file. Here is my code so far. Any help/hints/tips would be appreciated. Mainly, I am confused about accessing the various elements and subelements in the sources array from the file.
More background info: There is an html form and I am trying to add all the name elements from the sources array as options in the select element of the form.
$json_data = file_get_contents(SOURCE_URL);
$result = json_decode($json_data, true);
?>
<form action="P4.php" method="get">
Source Data
<select name ="sourcedata"></select> <br> <br>
<?php
$sources = $result['sources'];
#var_dump($sources);
#doing this var_dump successfully dumps the sources array
?>
If I am getting it correctly, you can use foreach and nested foreach to get the values within each array.
$json_data = file_get_contents(SOURCE_URL);
$result = json_decode($json_data, true);
$names = '<select>';
foreach($result['sources'] as $data1) {
// This will add each name inside the array
$names = $names.'<option>'.$data1['name'].'</option>';
// Nest another foreach to go one step further to get values of 'groupfields' array within this name
foreach($data1['groupfields'] as $data2) {
$group_field = $data2;
}
}
$names = $names.'</select>';
For more information on foreach statement in php, check this SO question. How does PHP 'foreach' actually work?
You could also use array_column to pick out just the names, then implode it into a string.
echo '
<select name="sourcedata">
<option>'.implode('</option><option>', array_column($result['sources'], 'name')).'</option>
</select>';

Decode some value stored in mySQL and separate them for form

I have a database which contains some values in a field. It contains values which i'm trying to decode for them to be used in a select form.
The values are like this:
MEAL:YES::-::Yes::-*-::MEAL:NO::-::No
In this example, the select form should have two possibilities.
MEAL:YES is what is written, and Yes is the value
they are separated by ::-::
The first select option and 2nd one are separated by ::-*-::
like #Bermar said
use
$possible_values = explode('::-*-::', $mySQL-value-coming-from-db)
and then var_dump($possible_values) once you've got the values from db. You can change what needs to be displayed in what order.
foreach (explode('::-*-::', $raw_data) as $item) {
list($key, $value) = explode('::-::', $item);
$data[$key] = $value;
}
Result will be like:
Array
(
[MEAL:YES] => Yes
[MEAL:NO] => No
)

Reformat JSON arrays in MySQL database table using MySQL SELECT query (Wordpress)

I have a Wordpress build that uses CalderaWP forms. Unfortunately, the plugin's author has decided to store all checkbox entries as JSON arrays in the WP database and custom field values show up as "Array" instead of the actual text values.
I'd like to run a cron job that reformats these entries, but the first part is trying to figure out how to find and replace all values that are JSON objects.
I've written a function to get values from a specific table in the database, specifically the "wp_cf_form_entry_values" table. This function uses a regular expression in the SELECT query to find all values that begin with the "{" character. This is what I came up with to find JSON objects in the value field, but maybe there is a better way. Anyway, the function gets these values and reformats them from a JSON object to a list of individual items (i.e. various values that were checked in the submitted form).
Here is my current function:
function reformat_my_data() {
global $wpdb;
$data = $wpdb->get_results("SELECT value
FROM `wp_cf_form_entry_values`
WHERE `value` REGEXP '^[{].*$'
");
foreach($data as $key => $field) {
foreach($field as $val) {
if( is_json($val) ) {
$val = json_decode($val, true);
foreach($val as $checked) {
echo ' - ' . $checked . '<br/>';
}
}
}
}
}
reformat_my_data();
I also have another function to determine if the value is a JSON object:
function is_json($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
This works and outputs a list like this:
- checked #1
- checked #2
- checked #3
My question is: How do I get this newly formatted value back into the database? In other words, how do I do a mass replace of JSON type objects in the WP database?

Check field and rewrite to array Codeigniter

I need to truncate string and rewrite it back to array
I have got a function where I get data from data base
$data['about_text_list'] = $this->about_text_model->get_array();
I get these fields from data base : id, num, header, text, language
I need to strip_tags and truncate text with function word_limiter
foreach ($data['about_text_list'] as $items)
{
$data['about_text_list']['text'] = word_limiter($items['text'], 100);
$data['about_text_list']['text'] = strip_tags($items['text']);
}
in view I do foreach
<? foreach ($about_text_list as $line) : ?>
<td><?=$line['text']?></td>
<? endforeach; ?>
But I get error, please tell me how to do correct things like this...
In the loop in your controller, you're limiting the word count, then setting that to the value in the array. Then, you're overwriting that value with the strip_tags function. You're using both functions on the same value instead of using the altered values. (And I would strip the tags first, then limit the word count.)
You're also just overwriting the $data['about_text_list']['text'] value each iteration. I'm assuming this needs to be an array of 'text' values? I would create a new array with the updated content and merge your $data['about_text_list'] array with the new array.
Change that loop to this:
$newarray = array();
foreach ($data['about_text_list'] as $key => $value)
{
$item_text = $value['text'];
$altered = strip_tags($item_text);
$newarray[$key]['text'] = word_limiter($altered, 100);
}
$data['about_text_list'] = array_merge($data['about_text_list'], $newarray);
// here, you create a new empty array,
// then loop through the array getting key and value of each item
// then cache the 'text' value in a variable
// then strip the tags from the text key in that item
// then create a new array that mirrors the original array and set
// that to the limited word count
// then, after the loop is finished, merge the original and altered arrays
// the altered array values will override the original values
Also, I'm not sure what your error is (as you haven't told us), but make sure you're loading the text helper to give you access to the word_limiter function:
$this->load->helper('text');
Of course, this all depends on the structure of your array, which I'm guessing at right now.

Get post values when the key is unknown in CodeIgniter

CodeIgniter allows access to POSTed data via:
$this->input->post('input_name');
where 'input_name' is the name of a form field. This works well for a static form where each input name in known ahead of time.
In my case, I am loading a collection of key/value pairs from the database. The form contains a text input for each key/value pair.
I am wondering, is there a way to get an array of posted data via the CodeIgniter api?
Thanks!
According to the documentation, no. I would suggest just using array_keys($_POST) to get the keys.
foreach($this->input->post() as $key => $val) { echo "<p>Key: ".$key. " Value:" . $val . "</p>\n"; }
that can be used to
Surely if you have an array of keys from the database you can use that, like :
foreach ($arrayFromDb as $key => $value) {
$newValue = $this->input->post($key);
}
Then you have the advantage that people if people submit additional fields (e.g. by modifying the form and posting it themselves) those fields will be ignored
$array_db_columns = $this->db->query('SHOW COLUMNS FROM ci_props');
$array_db_columns = $array_db_columns->result_array();
$array_save_values = array();
foreach ( $array_db_columns as $value )
{
$array_save_values[$value['Field']] = $this->input->post($value['Field']);
}
insert :
$this->db->insert('props', $array_save_values);
update :
$this->db->where('id',$id); $this->db->update('props',$array_save_values);

Categories