How to query a post's custom fields of radio type - advanced-custom-fields

I am working with radio button for a list of colors. I am using ACF Advanced Custom Fields for creating the custom fields. I have no problem with other field types. I cannot get this to work because it's an array type.
I have 5 choices for colors - red, blue, green, yellow, white.
<?php
$colors = get_post_meta( get_the_ID(), 'fav_colors', true );
echo $colors;
?>
The above prints array instead of the actual result. Also, get_field doesn't return any value so I resorted to using get_post_meta
How do I write and output the values of the radio fields.

Check-in your ACF field where you created a radio button.
Select the value in Return Value and check once more. If the return value is value then you get the value like this.
$color = get_field('fav_colors');
echo $color;
If your site using caching plugins then purge the cache and try once more.
And if you have selected a Return value as Array then you can get that field like this.
$color = get_field('fav_colors');
echo $color['value'];

Related

Two values from a single HTML checkbox

is it possible to have two values assigned to one checkbox? I require the checkbox to have the value yes or no and I'm simply wondering if this is possible to do with HTML. For example when the checkbox is checked it has the value of 'Yes' and unchecked it has the value of 'No' Thanks
EDIT:
Used a workaround that works fine in the PHP that handles the form if anyone else has this issue.
$var1 = e($_POST['checkbox']);
if ($var1 == null){
$var = 'No';
}else{
$var = 'Yes';
}
A checkbox has an attribute 'checked' which you can use to compute the value you need:
const checkbox = document.getElementById('my-checkbox');
const value = checkbox.checked ? 'yes' : 'no';
No, your checkbox will have a value and if it's checked - that value will be submitted with the form, if it's not checked it won't be.
The only way you could have two values is if you had an "onChange" listener in javascript and toggle the value based on if it's checked or not.

How to maintain checkbox value through pagination?

I have a list of products which are shown with pagination and I can filter the display of the products using check boxes. The problem is it can only displays the value from check box at first page, and if i go to the next page, i will lose the check box checked's value. Please help me how to solve this. I don't know where should I put input hidden and how to write Java Script. Here's my code:
<input id="checkbox_brand" type="checkbox" name="checkbox_brand[<? echo $data_brand[brand_name]; ?>]" value="<? echo $data_brand[brand_name]; ?>"/>
if (isset($_POST["checkbox_brand"])){
foreach($_POST["checkbox_brand"] as $status_a) {
$status_sql[] = '\''.$status_a.'\'';
}
$status = implode(',',$status_sql);
session_start();
$_SESSION["selected"]=$status;
}
if (session_is_registered("selected")){
-->my query
}
Each time you press a check box you'll have to use dictionary array in JavaScript
For example:
if I use the example page you gave, then when pressing a check box inside "Categories"
you'll have to put a value inside correct variable.
// Initialize Objects
var userChoice = {};
userChoice.Catagories = {};
userChoice.Brands = {};
userChoice.ScreenSize = {};
.
.
.
// Pressing check box will trigger the bellow
userChoice.Categories["LG"] = 0;
userChoice.Categories["Sharp"] = 1;
userChoice.Categories["Sony"] = 1;
userChoice.Brands["LcdTV"] = 1;
userChoice.Brands["LedTV"] = 0;
Each checkbox press will also trigger the following JavaScript
document.getElementById("userChoiceHiddenField").value = JSON.sringify("userChoice");
When submitting the page or going to the next page the hidden value will contain the JSON string, so you can parse it as JSON again.
Server Side: (.NET)
string userChoiceHiddenField = request["userChoiceHiddenField"].ToString();
and then take the value you got and place it back in the hidden field and the JavaScript value as follows:
userChoice = JSON.parse(document.getElementById("userChoiceHiddenField").value);
Hope that answers your question.

How to add form_dropdown id value to database? - codeigniter

I have a form with a dropdown for categories.
it looks like this:
<tr>
<td><?= form_label('Categorieen'); ?></td>
<td><?= form_dropdown('categorieen', $opties); ?></td>
</tr>
for the $opties i use this code:
$dbres = $this->db->get('categorieen');
$ddmenu = array();
foreach ($dbres->result_array() as $tablerow) {
$ddmenu[] = $tablerow['idcategorieen'];
}
$data['opties'] = $ddmenu;
But when i use this:
$this->input->post('categorieen');
it stores the value of the selected dropdown as an int.
so like this
select:
option1 (gives value 1, because of first option)
option2 (gives value 2, because it's the second option in de dropdown)
etc
How do i save the categoryid to the database instead of the number of the selected value?
You need to assign keys to the items you're adding to the $ddmenu;
Eg. $ddmenu[$tablerow['idcategorieen']] = $tablerow['idcategorieen']; will make the values be the idcategorieen.
Edit for clarification:
If $ddmenu looks like this:
array(
1 => 'Books',
2 => 'Cats',
3 => 'Foo Bars'
)
The dropdown options will look like
<option value="1">Books</option>
<option value="2">Cats</option>
<option value="3">Foo Bars</option>
Have some solutions to this.
When you call form_dropdown, the codeigniter do your dropdown based on key=>value array.
So key gonna be your option value, and value gonna be your option label.
You can do 2 things, format your "$opties" to the expected format, or take the submit before it happens with javascript, and post the option label instead the value.

MediaWiki: changing the label of a category at the bottom of the page

In mediawiki, is it possible to change the label of a 'Category' at the bottom of an article.
For example for the following article:
=Paris=
blablablablablabla
[[Category:place_id]]
I'd like to see something more verbose like (the example below doesn't work):
=Paris=
blablablablablabla
[[Category:place_id|France]]
Note: I don't want to use a 'redirect' and I want to keep my strange ids because they are linked to an external database.
I do not think mediawiki is supporting this feature.
However, how about using:
[[Category:France]]
in your page, and set it into the category named with your id? France would just be a subcategory of "place_id", and you could use more terms all linked to the parent category. For this, you just need to edit the category page for "France", inserting:
[[Category:place_id]]
An alternative would be to put your page in both categories, but in this case, the id would still be displayed:
[[Category:place_id]]
[[Category:France]]
You could do this with an OutputPageMakeCategoryLinks hook. Alas, the interface for that hook seems to be a bit inconvenient — as far as I can tell, it's pretty much only good for replacing the standard category link generation code entirely. Still, you could do that is you want:
function myOutputPageMakeCategoryLinks( &$out, $categories, &$links ) {
foreach ( $categories as $category => $type ) {
$title = Title::makeTitleSafe( NS_CATEGORY, $category );
$text = $title->getText();
if ( $text == 'Place id' ) {
// set $text to something else
}
$links[$type][] = Linker::link( $title, htmlspecialchars( $text ) );
}
return false; // skip default link generation
}
$wgHooks['OutputPageMakeCategoryLinks'][] = 'myOutputPageMakeCategoryLinks';
(The code above is based on the default category link generation code in OutputPage.php, somewhat simplified; I assume you're not using language variant conversion on your wiki, so I removed the parts that deal with that. Note that this code is untested! Use at your own risk.)

html listbox to show additonal txt in extra field outside of listbox

I have the following db-retrieve which results in the Listbox I like to have.
But I need $desc2 to be shown under the listbox in a separate field and it must change its content when the user clicks on an other item in the list:
Here is the retrieve which works:
echo "<form action=\"admin.php\" method=\"post\">";
echo "Industry:<SELECT name=\"industry\" size=\"10\">";
$result=sql_query("SELECT cat, title, desc, parentid
FROM industries
WHERE language='english'");
while(list($cid2, $ctitle2, $desc2, $parentid2) = sql_fetch_row($result)) {
if ($cid2==$userindustry) {
$sel = "selected";
} else {
$sel = "";
}
if ($parentid2!=0) $ctitle2=getparentindustry($parentid2,$ctitle2);
echo "<option value=\"$cid2\" $sel>$ctitle2</option>";
}
echo "</SELECT>
<br>$desc2<br> # place to show description of list item
<input type=\"hidden\" name=\"op\" value=\"save\">
<input type=\"submit\" value=\"Go\"></form><br>";
As I'm now searchin for some time, but didn't found something, hopefully someone here could help me.
The code for the side is in php.
Thanks in advance.
You would have to store $desc2 to a temporary variable in the loop, and use the temporary variable after the select to show the temporary variable.
I would however, point out that in general, this is probably the wrong way of going about this code, and that your problem is deeper in your implementation :)