Add class to ACF multi select - php

I have been struggling for two hours with the following situation and I still didn't find the solution. Hopefully you guys can help me out. It's about the following:
I use ACF to make a selection from a multi select menu. I want to show all of the choices and I want to add a class to the selected values.
Below you will find my code so far. This outputs all of the choices. But I don't know how to check whether the choice has been selected or not.
$features = get_field_object('features');
$choices = $features['choices'];
$values = $features['value'];
if ( $features ):
echo '<ul class="checks">';
foreach ( $choices as $choice) {
echo '<li>'. $choice .'</li>';
}
echo '</ul>';
endif;
Thanks a lot for helping!

You can check if the key of the current $choice is as a value in the $values array.
if you var_dump the $features array you will see that $values array contains the keys of the values and the $choices array contains both of them.
var_dump example:
$features['value'] => Array
(
[0] => color1
)
$features['choices'] => Array
(
[color1] => red
[color2] => yellow
[color3] => green
)
Code:
$features = get_field_object('features');
$choices = $features['choices'];
$values = $features['value'];
if ( $features ):
echo '<ul class="checks">';
foreach ( $choices as $key => $choice) {
$class = in_array($key, $values) ? 'class="selected"' : '';
echo '<li ' . $class . '>'. $choice .'</li>';
}
echo '</ul>';
endif;

Related

php get the text into an array list

I have an array in the database. When I used print_r($variable_name), I got the array like this
Array
(
[0] => Array
(
[attribute_name] => Disk space,Color,Processor
)
)
So to get the value of attribute_name I tried this
foreach($attributes_name as $key=>$val) {
echo $val['attribute_name'];
}
Here I got the result like Disk space,Color,Processor. But I want the result should come like a list
<li>Disk Space</li>
<li>Color</li>
<li>Processor</li>
So can someone tell me how to do this?
Try this :
<?php
$arr = array(array("attribute_name" => "Disk space,Color,Processor"));
foreach($arr as $val) {
$resultArr = explode(",",$val['attribute_name']);
foreach($resultArr as $value){
echo "<li>".$value."</li>";
// Add id in li
echo "<li id='".str_replace(" ","_", strtolower($value))."'>".$value."</li>";
}
}
?>

Wordpress and custom fields in a loop

Say I have custom fields with the keys p_title_1 p_value_1 p_title_2 p_value_2
Each of these has values within them and I would like to loop through p_title_[i] and p_value_[i] and show them on the page so that title and value would be grouped together in their own div.
I can't seem to figure out how to write it as a loop to show the 1's together and 2's together.
Reason it needs to be in a loop is incase more custom fields get added in the future. At current I have the following but it only echos the key and value
<?php
$custom_fields = get_post_custom( get_the_ID() );
$my_custom_field = $custom_fields['p_title_1'];
foreach ( $my_custom_field as $key => $value )
echo $key . " => " . $value . "<br />";
?>
Help is appreciated
Use the field name to build an array.
Where the array looks like:
array
1 =>
array
'title' => 'title 1'
'value' => 'value 1'
Loop through your fields and add to the array:
You can get the array index using explode function
$parts = explode ( '_' , $fieldname );
$name = $parts[0];
$index = $parts[1];
And add the value to the array:
$array[$index][$name] = $value;

how to echo multidimension array using php

I have the following array, I need to display the names on a form.
How can I do this via foreach() loop?
What I am trying is:
Array
(
[0] => Array
(
[to_user_name] => John
)
[1] => Array
(
[to_user_name] => Mike
)
)
foreach( $myArray as $subArray ) {
echo $subArray["to_user_name"];
}
It's not clear how you want to use those values in your form, but just echo the values wherever you'd need them, e.g.,
foreach( $myArray as $subArray ) {
echo "<input type=\"text\" name=\"user_name\" value=\"" . $subArray["to_user_name"] . "\">";
}
I was treating it like a single array and then i realized it is a multidimensino array and the following worked, i hope this helps someone else too
foreach ($messages->getMessage('recipient_names') as $section => $items ){
foreach ($items as $key => $value){
echo "$value, ";
}
}
to see the content you can use
print_r($your_array);
For developing purpose you need to use for/foreach loop
foreach($your_array as $array_temp)
{
foreach($array_temp as $item)
{
echo $item;
}
}

Store multidimensional array from JQuery Dropdown checklist in database in string form using loop

I want to store an array of checklist into a string in a database. Below this is the code in the interface which display a list of room with a dropdown checklist for each room.
while($rows = mysql_fetch_array($query)){
<!-- Apply dropdown check list to the selected items -->
<script type="text/javascript">
$(document).ready(function(){$("#<?php echo $bnum; ?>").dropdownchecklist( { width: 300 } );});
</script>
<!-- This is the JQuery Dropdown checklist !-->
<select id="<?php print $bnum;?>" multiple="multiple" name="dietdata[]" >
<option value="0"></option>
<?php
//query drop-down list
$sqlakh="select diet_id,diet_name from bmsdb.diettypes";
$resultakh=mysql_query($sqlakh);
while($rowsakh= mysql_fetch_array($resultakh)) { ?>
<option value='<?php echo $rowsakh['diet_id'].'|'.$bnum; ?>'><?php echo $rowsakh['diet_name']; ?>
</option>
<?php }
}//end while ?>
</select>`
When I submit this form in the server side, this is what I do to extract the data from the array of the dropdown checklist:
$data2 = $_POST['data2']; //total no of data
$dietdata= $_POST['dietdata']; //diet data
$roomlist= $_POST['data3']; //patient room number
for($k=0; $k<=sizeof($data3); $k++){
$dietarray= $dietdata[$k];
$separatediet= (explode('|',$dietarray));
$output_array[]=array("diet"=>$separatediet['0'],"room"=>$separatediet['1']);
for($j=0; $j<=sizeof($data3); $j++){
if($output_array[$k][room]== $roomlist[$j]){
$rekod[$output_array[$k][room]]= $output_array[$k][diet];
}
}
}print_r($rekod);
The print_r output when I submitting the form is like this:
Array ( [501] => 3 [502] => 4 [] => )
Then I extract the array using implode to separate the room and the diet type.
Array ( [0] => 2|501 [1] => 3|501
[2] => 3|502 [3] => 4|502 )
What I want is for the array to be like this. Is there any way for me to make the array to be like this, or a better structure to use?
Array ( [501] => 2,3) //2,3 is the diet type group together after being extracted from the array above
[502] => 3,4 )
Assuming you're keeping all the code up to the point where you're imploding to generate your final array:
Array ( [0] => 2|501 [1] => 3|501
[2] => 3|502 [3] => 4|502 )
You could create the array you're asking for like this:
$out = array(); // The final output array
foreach ($your_array as $item) {
$vals = explode('|', $item);
if (!isset($out[$item[1]])) $out[$item[1]] = array();
$out[$item[1]][] = $item[0];
}
foreach ($out as &$item) {
$item = implode(',', $item);
}
This is my final code. Which generata output like this
Array ( [501] => 2,3,4 [502] => 3,4 [503] => 5,6 )
Thanks to Hibiscus for your help.
$out = array(); // The final output array
foreach ($status as $item) {
$vals = explode('|', $item);
if (!isset($out[$vals[1]])) $out[$vals[1]] = array();
$out[$vals[1]][] = $item[0];
}
$item=array();
foreach ($out as &$item)
{
$item = implode(',', $item);
}
$diet = array();
$data3 = $_POST['data3'];
for($h=0; $h<=$data2; $h++)
{
$data3 = $_POST['data3']; //patient bnum
$data3 = $data3[$h];//bnum
if(!empty($out[$data3]))
{
$diet[$data3] = $out[$data3];
}
} print_r($diet);

Foreach loop returning null values in PHP?

I have a pretty simple problem.
Basically I have an array called $list that is a list of titles. If I do a print_r($list) I get these results:
Array ( [0] => Another New Title [1] => Awesome Movies and stuff [2] => Jascha's Title )
Now, I'm running a foreach loop to retrieve their values and format them in an <ul> like so...
function get_film_list(){
global $categories;
$list = $categories->get_film_list();
if(count($list)==0){
echo 'No films are in this category';
}else{
echo '<ul>';
foreach($list as $title){
echo '<li>' . $title . '<li>';
}
echo '</ul>';
}
}
The problem I'm having is my loop is returning two values per value (is it the key value?)
The result of the preceding function looks like this:
Another New Title
Awesome Movies and stuff
Jascha's Title
I even tried:
foreach($list as $key => $title){
echo '<li>' . $title . '<li>';
}
With the same results:
Another New Title
Awesome Movies and stuff
Jascha's Title
What am I missing here?
Thanks in advance.
You’re using <li> instead of </li> as closing tag. Use the proper closing tag and it should work:
echo '<li>' . $title . '</li>';

Categories