Only want to show assigned wordpress category - arrays

I am building a education platform within wordpress, but i am having difficulties getting the categories only to show the user assigned categories in the filter.
I am using ACF Pro to assign my custom categories to the users.
And here i get the terms assigned to the user.
$uid = get_current_user_id();
$departments = get_terms( array('departments'), 'user_'.$uid );
In this line i am looping the categories:
foreach ($departments as $value) {
if(!in_array($value->term_id, $departments)){
echo '<input id="switch-demo'. $value->term_id .'" type="checkbox" data-filter="'. $value->term_id .'" class="filter-check '. $value->term_id .'" value="'. $value->term_id .'" name="departments[]" >';
echo '<label for="switch-demo'. $value->term_id .'" class="filter '.$value->slug.'">'. $value->name .'</label>';
// echo $value->name;
}
}
For some reason it is showing all categories that are currently in my post type "Courses".
But i want to make sure that y category field is only showing categories that are assigned to the user.
Please help.

get_terms doesn't take any user_.$uid parameter. Only thing you should pass to get_terms should be the arguments array.
For ACF, to get a field value you will need to use the_field or get_field functions.
$uid = get_current_user_id();
$departments = get_field( 'departments', 'user_'.$uid );
More details on how to get values from user using ACF

Related

Symfony form with several inputs text type to fill array property of entity object

I need to render a Symfony form to add entity object with his properties in the database. On of those properties is an array.
I want to display in the twig view several inputs text type field to fill this array. So i tried this following the doc but it doesn't display anything.
$builder
->add('tracklist', CollectionType::class, [
'entry_type' => TextType::class,
'entry_options' => [
'attr' => ['class' => 'fs-1 text-center text-uppercase']
]
])
I have looked for answers a lot everywhere but nothing worked. I do something wrong and can't figure out what.
I'm not arrived at this point but at the end my point is to use some javascript to display an additional input text type each time the one before is filled. All those inputs will fill the array in the database. This is to add a tracklist for discs so I never know how many inputs i'll need.
Thanks for the help guys !
// EDIT
OK after multiples tries and fails. I finally did it without using Symfony. It's much more simple for what I need.
Here is the template part
<p>TRACKLIST</p>
<div id="tracklist_container">
<label for="tracks">TRACK 1</label>
<input type="text" name="tracks[]" id="tracks">
</div>
<button id="tracklist-button">ADD A TRACK</button>
The JS part
let tracklistContainer = document.querySelector('#tracklist_container')
let label = tracklistContainer.querySelector('label')
let input = tracklistContainer.querySelector('input')
let addButton = document.querySelector('#tracklist-button')
let i = 1
addButton.addEventListener('click', (e) => {
e.preventDefault()
i++
labelClone = label.cloneNode(true)
inputClone = input.cloneNode(true)
labelClone.textContent = "TRACK" + i
inputClone.value = ""
tracklistContainer.appendChild(labelClone)
tracklistContainer.appendChild(inputClone)
})
And the PHP Controller part
$safeTracks = array_map('trim', array_map('strip_tags', $_POST['tracks']));
$disc->setTracklist($safeTracks);
I get what I needed that way !

How to get results in form of tables with column titles in wordpress?

<form action="" method="post" name="myForm">
Filter <input id="isbn" type="text" name="isbn" />
<input type="submit" name="submit" value="Submit" /> </form>
<?php
if(isset($_POST['submit']))
{
global $wpdb;
$table_name = "isbn"; // change this to your table name
$field = $_POST['isbn']; // change this to your isbn field $_POST['ISBN'];
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name where isbn = '".$field."'");
foreach ($retrieve_data as $retrieved_data) {
echo $retrieved_data->Title;
echo $retrieved_data->Image; // for image
echo $retrieved_data->Isbn;
}
}
// I have this php code for accessing data from my database table. but the result is not coming in desired form. i am sending some screenshots which are describing my situations.
This image contain my database table structure and record containing title, image and isbn column.
This is the output which i am getting after applying above query and image is also not displaying in browser.
This type of output i want in my browswer after someone enter isbn number of book in search form.
Can someone please provide me code for this?
Try adding ARRAY_A in the get_results method argument like below.
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name where isbn = '".$field."'", ARRAY_A);
and then for displaying image try
foreach ($retrieve_data as $retrieved_data) {
echo $retrieved_data['Title'];
echo '<img src="data:image/jpeg;base64,'.base64_encode( $retrieved_data['Image'] ).'"/>'; // for image
echo $retrieved_data['Isbn'];
}

Create links to tagged list from an array of tags

I just followed a tag tutorial (https://book.cakephp.org/3.0/en/tutorials-and-examples/cms/tags-and-users.html) on the Red Velvet Cookbook. I was successful in adding tagging to my website. Though I feel it needs a couple more things. The comma separated list of tags is just that, a list. I would prefer a list of links. The links would obviously link to a list of posts that are tagged with the same tag. This list already exists thanks to the tutorial. The url looks like http://localhost:8765/story/tagged/cats/
I have fiddled with the html helper to try and create links but I am unsure how to do this with an array. Any leads or help would be greatly appreciated.
This is the line in my template that shows the list of tags
<p><b>Tags:</b> <?= h($story->tag_string) ?></p>
This is the function that gets the tag_string
class Story extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* #var array
*/
protected $_accessible = [
'*' => false,
'tag_string' => true
];
protected function _getTagString()
{
if (isset($this->_properties['tag_string'])) {
return $this->_properties['tag_string'];
}
if (empty($this->tags)) {
return '';
}
$tags = new Collection($this->tags);
$str = $tags->reduce(function ($string, $tag) {
return $string . $tag->name . ', ';
}, '');
return trim($str, ', ');
}
}
In your $story entity you have tags property, which you already used in computed field tag_string. To get separated tags, you need to iterate over it in a template, eg:
<ul>
<?php foreach($story->tags as $tag): ?>
<li><?= $tag->title ?></li>
<?php endforeach; ?>
</ul>
Assuming you have a method in your StoriesController named tagged, which accepts one parameter: tag title, you can construct link using HtmlHelper for each tag:
<ul>
<?php foreach($story->tags as $tag): ?>
<li>
<?= $this->Html->link($tag->title, [
"controller" => "Stories",
"action" => "tagged",
$tag->title
], [
"class" => "your-tag-link-class" //second array is for properties, in case you would like to add eg class for these links to style them using css.
]) ?>
</li>
<?php endforeach; ?>
</ul>

Codeigniter foreach Select Menu 'Selected' Option

I am creating a theme select option for an App. I want the user to choose from a select option which theme they want, light or dark.
I am storing theme names in the database like so:
I am then calling all themes in the Model and returning the array with this code:
public function getThemes() {
$query = $this->db->query('SELECT * FROM theme');
return $query->result_array();
}
I am using my Constructor to grab the data and Render it to my settings view like so: (I am sending a few things at the same time in $this->data that's why it is in an array but I cut that code out)
$this->data = [
'themes' => $this->model_setting->getThemes()
];
$this->render('backend/standart/administrator/setting/setting_general', $this->data);
In my HTML View I have the following HTML which successfully displays the correct 2 themes available:
<div class="col-sm-12">
<label>Select Your Desired Theme</label><br>
<select class="form-control" name="site_color_theme" id="site_color_theme">
<?php foreach ($themes as $theme): ?>
<option value="<?= $theme['theme']; ?>" ><?= $theme['name']; ?></option>
<?php endforeach; ?>
</select>
</div>
When I save the settings form I am updating my options table in the database with the theme name. The Html Body Class calls on this options table and finds the chosen theme and uses this to render a specific CSS Stylesheet.
The problem I am having is when I save my settings, the Select Option does not display the saved theme name to show that this is the active theme chosen.
How can I get the Select Option to display the chosen theme when the user visits the settings page to select another theme?
You should have active_theme column in the theme table
active_theme set to 1 when form dropdown site_color_theme is submitted (the rest theme records set to 0)
You should use Codeigniter Form Helper to do that : https://www.codeigniter.com/user_guide/helpers/form_helper.html#form_dropdown
// $themes = Theme list array [id => theme-name, id => theme-name]
// $active_theme = Record of "active_theme" with value equal to "1"
// $extra = Custom HTML attributes e.g. class="something" onclick="function(js)"
echo form_dropdown('site_color_theme', $themes, $active_theme, $extra);

Laravel edit blade checkbox array issue

My Problem I can't get my dynamic checkboxes to save properly in from my edit.blade, they only work if the values are 1, if an unchecked checkbox is submitted via hidden field it will overwrite the next set of checkbox checked values.
My Code
I have a crud resource that takes orders, the form in the create.blade itself has a bunch of dynamic fields that add a new product to the order via 'add-new' button that clones the product fields.
Part of that form is a bunch of days checkboxes that work fine and are stored correcly.
Where I'm Getting Stuck
I've made an edit.blade to be used to correct any mistakes that would be made while creating an order.
To call back the section that refers to the dates checkboxes I've used the following blade syntax (I know its different from create, mainly due to me trying to fix the problem)
#foreach($orders as $orderkey => $order)
#foreach($days as $day)
{{ Form::hidden($day.'[]', 0, array('id'=> $day.'_hidden_'.$orderkey, 'class' => 'is-checkradio')) }}
{{ Form::checkbox($day.'[]', 1, $order->{$day}, array('id'=> $day.'_'.$orderkey, 'class' => 'is-checkradio')) }}
<label for="<?php echo $day.'_'.$orderkey; ?>"><?php echo $day; ?></label>
#endforeach
#endforeach
OrderController - Update
I've had to use the following in my controller to get the fields to update however whenever a checkbox is left unchecked it will overwrite the next checked value.
$customer = Customer::find($id);
foreach($customer->orders as $key => $order){
$Monday[] = $request->Monday[$key];
};
$updates = array(
'Monday' => $Monday,
);
foreach($updates['orders'] as $k => $update){
$update_order->Monday = $updates['Monday'][$k];
$update_order->save();
};
This part:
foreach($customer->orders as $key => $order){
$Monday[] = $request->Monday[$key];
};
Is creating an array where if the existing data is true and the new data is true, save it. Otherwise delete it. That means any checkboxes that have been deselected will be captured, but any newly checked checkboxes won't be. Is this your intention?
Why not just do:
$updates = array(
'Monday' => $request->Monday,
);
I may not have fully understood your question so please comment and/or amend the Q if you need to clarify further,
Found out how to fix it from Unchecked checkbox returning null value
I needed to add my $orderkey to the name []
So in the end this worked:
#foreach($days as $day)
{{ Form::hidden($day.'['.$orderkey.']', null, array('id'=> $day.'_hidden_'.$orderkey, 'class' => 'is-checkradio')) }}
{{ Form::checkbox($day.'['.$orderkey.']', 1, $order->{$day}, array('id'=> $day.'_'.$orderkey, 'class' => 'is-checkradio')) }}
<label for="<?php echo $day.'_'.$orderkey; ?>"><?php echo $day; ?></label>
#endforeach

Resources