get id dynamic inside $_POST PHP - php

Is this the right way to get ID dynamic within $_POST?
if( isset( $_POST['episode_title '. $episode_ID .''] ) ){
$episode_title . $episode_ID = $_POST['episode_title '. $episode_ID .''];
}
using '. $episode_ID .' to get id dynamic.
If not, then does someone know how? thanks.

$posts = $_POST;
foreach( $posts as $key => $value)
if(preg_match('/episode_title (\d+)/', $post))
{
// rest...
}
}

Related

How to retrieve the value of the field in ACF?

<?php if( $fields )
{
foreach( $fields as $field)
{
$value = get_field( $field['name'] );
if ($value) {
echo '<dl>';
echo '<dt>' . $field['label'] . '</dt>';
echo '<dd>' . $field['value'] . '</dd>';
echo '</dl>';
}
}
}
?>
This is what I have. If I do a var_dump on acf_get_fields, it apparently sets the value to NULL. I could have known, as it's written here:
https://www.advancedcustomfields.com/resources/get_field_object/
The problem: I have to first get all fields in a specific field_group, hence I am using acf_get_fields.
I thought with using $field['value'] I could get it to work, but apparently this does not work.
Can someone help me to retrieve the values in the foreach per field? Surely there must be a way?
PS:
<?php
$fields = get_fields();
if( $fields ): ?>
<ul>
<?php foreach( $fields as $name => $value ): ?>
<li><b><?php echo $name; ?></b> <?php echo $value; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This gives me ALL fields. I just need a specific list of fields from a specific field group. That is the problem. Anybody knows how I can solve it?
<?php
$fields = get_field('fieldname');
foreach( $fields as $field)
{
echo '<dl>';
echo '<dd>' . $field . '</dd>';
echo '</dl>';
}
?>
Think the value is always NULL because you're getting it only the group fields, not the fields from a post. Have made a few examples maybe this will help you out.
$post_id = 123;
$group_id = 1234;
// Get post fields
$post_fields = get_fields($post_id);
foreach ($post_fields as $key => $value) {
if ($key == 'my_custom_field') {
echo $value;
}
}
// Get group fields
$group_fields = acf_get_fields($group_id);
foreach ($group_fields as $key => $group_field) {
if ($key == 'my_custom_field') {
echo $value;
}
}
// Get field objects
$field_objects = get_field_objects($post_id);
foreach ($field_objects as $key => $field_object) {
if ($key == 'my_custom_field') {
echo $field_object['value'];
}
if ($field_object['name'] == 'my_custom_field') {
echo $field_object['value'];
}
// only show fields from specific group
if ($field_object['parent'] == $group_id) {
echo $field_object['value'];
}
}

Checking query results against exploded array - Wordpress, php

I have some brands listed against a user in the db which are input as a comma seperated list in the users profile - i get the individual brands associated to the user back by as follows
$brand_name = get_user_meta($user_id, 'brand', true);
$brands = explode(', ', $brand_name);
it works ok.
I am now creating brand pages which list their products, this is easy but i want to prevent users seeing the pages of the brands that they arent subscribed to.
my code that isnt working is below:
foreach ( $results as $result ){
$brand_name = get_user_meta($user_id, 'brand', true);
$brands = explode(', ', $brand_name);
if($brands = $result->brand) {
echo '<h2>Title: ' . $result->title . '</h2><br/>';
} else {
echo 'You can\'t view that brand';
}
}
I think i need to do some sort of nested foreach/for loop but not sure - Thanks in advance.
foreach ( $results as $result )
{
$brand_name = get_user_meta($user_id, 'brand', true);
$brands = explode(', ', $brand_name);
if(in_array($result->brand,$brands))
{
echo '<h2>Title: ' . $result->title . '</h2><br/>';
}
else
{
echo 'You can\'t view that brand';
}
}
use in_array()

Which method is best for retrieving information from custom fields in a Wordpress post to be displayed on another page?

I'm a PHP beginner working with Wordpress, and I'm trying to retrieve data from custom fields (using Custom Field Suite) from a post to be displayed on a different page.
I have tried a few different ways, and found two methods that work. But as I'm a beginner, I wonder if these methods are 'proper'?
This is one solution I found, but it's not exactly elegant:
// Method 1
$current = CFS()->get( 'get_current' ); //retrieves the post ID from a custom field on the page
$custom_fields = get_post_custom($current);
$my_custom_field = $custom_fields['current_title'];
$my_custom_field2 = $custom_fields['current_artist'];
foreach ( $my_custom_field as $key) {
}
foreach ( $my_custom_field2 as $key2) {
}
echo '<h2>'.$key.'</h2>';
echo '<h1>'.$key2.'</h1>';
I tried to re-write it like this, but nothing is displayed - not sure what's wrong with this loop:
// Method 2
$current = CFS()->get( 'get_current' );
$custom_fields = get_post_custom($current);
foreach ( $custom_fields as $key) {
echo '<h2>'.$key['current_title'].'</h2>';
echo '<h1>'.$key['current_artist'].'</h1>';
}
As method 2 wasn't working, I tried something else and found that this also works (I added the loop here based on this answer: https://stackoverflow.com/a/19918170/5483154):
// Method 3
<?php while ( have_posts() ) : the_post();
$current = CFS()->get( 'get_current' );
$currentitle = get_post_meta($current, 'current_title', true);
$artistname = get_post_meta($current, 'current_artist', true);
echo '<h2>'.$currentitle.'</h2>';
echo '<h1>'.$artistname.'</h1>';
endwhile;
wp_reset_query();
?>
Method 3 seems best to me. Is this a good way of approaching this problem? And if anyone would be kind enough to explain what's wrong with method 2, I would also appreciate it. Thanks
Looking at the Custom Field Suite documentation here: http://customfieldsuite.com/api/get.html
It looks like you should be using the 'get' method:
CFS()->get( $field_name, $post_id, $options );
Eg.
$current = CFS()->get( 'get_current' );
echo '<h2>' . CFS()->get( 'current_title', $current ) . '</h2>';
echo '<h1>' . CFS()->get( 'current_artist', $current ) . '</h1>';
Ps. Don't forget to watch the order of your heading tags (<h1> should come before <h2>)
In the case of your "method 2": what if you try rewriting it something like this?
// Method 2
$current = CFS()->get('get_current');
$custom_fields = get_post_custom($current);
foreach ($custom_fields as $field) {
foreach ($field as $key => $value) {
echo $key . ': ' . $value;
}
}

Automate assignment of multiple values from the $_POST variable

I just wrote the following code:
<?php
$email = checkPost("email");
$username = checkPost("username");
$firstname = checkPost("firstname");
$lastname = checkPost("lastname");
$zipcode = checkPost("zipcode");
echo("EMAIL: ".$email." USERENAME: ".$username);
function checkPost($formData) {
if (isset($_POST[$formData])) {
return $_POST[$formData];
}
}
?>
What I'd like to do is eliminate all those calls to checkPost() at the top. This code below doesn't require any knowledge of the fields in the form that submits to it, it just loops through all of the fields and spits out their values.
<?php
// loop through every form field
while( list( $field, $value ) = each( $_POST )) {
// display values
if( is_array( $value )) {
// if checkbox (or other multiple value fields)
while( list( $arrayField, $arrayValue ) = each( $value ) {
echo "<p>" . $arrayValue . "</p>\n";
}
} else {
echo "<p>" . $value . "</p>\n";
}
}
?>
I want to modify this code such that variables like $email, etc would be created and assigned values on the fly. Like say you run this on a form that has "email" and "name". You won't have to give php a variable name $email or $name. It will just loop through and for "email" create and fill a variable named $email and so on and so forth.
Am I dreaming?
foreach ($_POST as $key=>$value) {
echo '<p>', htmlspecialchars($key), ' = ', htmlspecialchars($value), '</p>';
}
I think that's what you're looking for anyway. Basically, we just loop through all $_POST elements and display them. You can of course do whatever you need with them, within the loop.
You could use...
extract($_POST, EXTR_SKIP);
...but I don't recommend it. This will unpack the array's keys into the scope it is in. I've set it to not overwrite existing variables.
You should explicitly define your variables.

How do I get the key values from $_POST?

echo $_POST["name"]; //returns the value a user typed into the "name" field
I would like to be able to also return the text of the key. In this example, I want to return the text "name". Can I do this?
$_POST is just a normal associative array so you can also loop over the entire thing like this:
foreach($_POST as $key=>$value)
{
echo "$key=$value";
}
Check out the array_keys() function assuming this is PHP.
http://us2.php.net/array_keys
#Tim: there was a ) missing. so it should be:
while( list( $field, $value ) = each( $_POST )) {
echo "<p>" . $field . " = " . $value . "</p>\n";
}
while( list( $field, $value ) = each( $_POST )) {
echo "<p>" . $field . " = " . $value . "</p>\n";
}
array_keys($_POST)
Manual
foreach($_POST as $rvar)
{
$rvarkey=key($_POST)
$$rvarkey=mysql_real_escape_string($rvar);
}
it creates variables having the name of the request parameters which is pretty awesome.

Categories