store array in database Wordpress - arrays

I have this form with a select.
<form>[...]
<select name="quantity[]">
<?php
for ($i=1; $i <20; $i++) {
?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php } ?>
</select>[...]
</form>
I'm trying to store the quantity data in the database using $wpdb.
global $wpdb;
$table_name=$wpdb->prefix.'prenotazione_eventi';
$wpdb_quantity = maybe_serialize( $_POST[ 'quantity'] );
$query = "INSERT INTO $table_name (quantity) VALUES (%s)";
$prepare_query = $wpdb->prepare( $query, $wpdb_quantity);
$result = $wpdb->query( $prepare_query );
When I submit the form the data is saved in the database, but in this sintax a:1:{i:0;s:2:"10";}. What I want is that the data have only the number that I choose I the form, "10". How do that?

Try this code.
use json_encode instead of maybe_serialize.
global $wpdb;
$table_name=$wpdb->prefix.'prenotazione_eventi';
$wpdb_quantity = json_encode( $_POST[ 'quantity'] );
$query = "INSERT INTO $table_name (quantity) VALUES (%s)";
$prepare_query = $wpdb->prepare( $query, $wpdb_quantity);
$result = $wpdb->query( $prepare_query );

Related

Custom Fields - Repeater dropdown - list only unique & not blank values

Here's the thing that I'm using the ACF repeater subfield (firma) to store some data. Usually the user is typing here his parent company name like: google, alibaba, sodexo etc. So it might happen that in multiple posts, the value of this field will be the same. At the moment I have following code:
$args = array(
'post_type' => 'opencourses',
'meta_key' => 'terminy_warsztatow'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
echo '<select type="text" class="form-control" name="filtr_lokalizacja">';
while ($the_query->have_posts()) : $the_query->the_post();
if(have_rows('terminy_warsztatow')):
while (have_rows('terminy_warsztatow')) : the_row();
// display your sub fields
$filtr_var = get_sub_field('firma');
echo '<option value="'. $filtr_var .'">';
echo $filtr_var;
echo '</option>';
endwhile;
else :
// no rows found
endif;
endwhile;
echo '</select>';
endif;
And it works - meaning: it shows all typed values. But instead of showing only UNIQUE values it generates a list similar to this:
Google
Alibaba
Google
Sodexo
Sodexo
Tesla
Tesla
Sodexo
How to avoid showing same values and hide empty as well? I know that there is php function array_unique but I was unable to implement that. I've done sth like:
$filtr_var = get_sub_field('firma');
$result = array_unique($filtr_var);
echo $result;
but then it shows no values at all.
I assume that "firma" is simple text input type in repeater. if so then arrya_unique function won't work for string output.
you need to save each value in array and then use in_array function to make it unique.
see below code.
$args = array(
'post_type' => 'opencourses',
'meta_key' => 'terminy_warsztatow'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
echo '<select type="text" class="form-control" name="filtr_lokalizacja">';
while ($the_query->have_posts()) : $the_query->the_post();
if(have_rows('terminy_warsztatow')):
// $PreValue = array();
while (have_rows('terminy_warsztatow')) : the_row();
// display your sub fields
$filtr_var = get_sub_field('firma');
// compare current value in saved array
if( !in_array( $filtr_var, $PreValue ) )
{
echo '<option value="'. $filtr_var .'">';
echo $filtr_var;
echo '</option>';
}
// save value in array
$PreValue[] = $filtr_var;
endwhile;
else :
// no rows found
endif;
endwhile;
echo '</select>';
endif;
Hope this will help you!
Enjoy

multi select not work in cakephp.how can add all select?

i create two multi select list in my cakephp view .
in one of select box get all users and in anouther get all car list .
i want select users and car name and insert in user profile.but just select one and last item .
my table=>
user -->
-id
-username
-user_family
car-->
-id
-name
-model
my controller{
$users = $this->User->find('all', array(
'fields' => array('User.id', 'User.username', 'User.user_family')));
$this->set('users', $users);
$cars = $this->Car->find('all', array());
$this->set('cars', $cars);
}
and my view file {
foreach($users as $user){
?>
<option
value="<?php echo $user['User']['id'] ?>"><?php echo $user['User']['username'].' (' .$user['User']['user_family']. ' )' ?></option>
<?php
}
?></select>
<label>نام درس</label> </label>
<select name="car_id" id="car_id" multiple="true" style="width: 150px;height: 200px;">
<?php
foreach($cars as $car) {
?>
<option value="<?php echo $car['Car']['id'] ?>"><?php echo $course['Car']['name'] ?></option>
<?php
}
?>
</select></fieldset>
}
and my pr() result:
Array
(
[user_id] => 16
[car_id] => 8
)
but i select 10 user and 2 car .how can fix it?
Try in controller
$users = $this->User->find('list');
$cars = $this->Car->find('list');
$this->set(compact('users','cars'));
in your view use Form helper
echo $this->Form->input('User');
echo $this->Form->input('Car');

Save array from drop-down menu in Wordpress

I have successfully pulled a custom post type through into a drop-down that is in a custom meta box. However, when displaying it on the front end I would like to also provide a link to the actual post, not just the name of the post. So I am guessing I need to save this as an array? Is this possible through a drop-down? Confused on how I should approach this. Any help is greatly appreciated.
Here is what I have so far:
// Add Meta Box To Select Overseeing Pastor
add_action('admin_init', 'ministry_select_add_meta');
function ministry_select_add_meta(){
add_meta_box('ministry_select_post', __('Overseeing Pastor'), 'ministry_select_meta', 'ministry', 'side');
}
function ministry_select_meta( $post ) {
$values = get_post_custom( $post->ID );
$selected = isset( $values['pastor_select'] ) ? esc_attr( $values['pastor_select'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<select name="pastor_select">
<?php
$args = array(
'post_type' => 'employee',
'position' => 'pastor'
);
$pastorList = new WP_Query($args); while ($pastorList->have_posts()) : $pastorList->the_post();
$is_selected = (get_the_title() == $selected) ? 'selected="selected"' : '';
echo '<option value="'.get_the_title().'" '.$is_selected.'>'.get_the_title().'</option>';
endwhile; wp_reset_postdata();
?>
</select>
<?php
}
add_action( 'save_post', 'ministry_select_save' );
function ministry_select_save( $post_id )
{
// Stop If Autosaving
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// Stop If Nonce Can't Be Verified
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// Stop If Unauthorized User
if( !current_user_can( 'edit_post' ) ) return;
// Make Sure Data Is Set Then Save
if( isset( $_POST['pastor_select'] ) )
update_post_meta( $post_id, 'pastor_select', esc_attr( $_POST['pastor_select'] ) );
}
To get the link of a Post you can use the get_permalink function
<?php $permalink = get_permalink( ); ?>
or like this if you are outside the Loop
<?php $permalink = get_permalink( $post->ID ); ?>
You can use that to print it in any place on your HTML code.
If what you want is go to the Post URL when the Post Title is selected in the Drop Down you can use JavaScript code to do that, doing something like:
<select name="pastor_select" onchange='location=this.options[this.selectedIndex].value;'>
<?php
$args = array(
'post_type' => 'employee',
'position' => 'pastor'
);
$pastorList = new WP_Query($args); while ($pastorList->have_posts()) : $pastorList->the_post();
$is_selected = (get_the_title() == $selected) ? 'selected="selected"' : '';
echo '<option value="'.get_permalink( ).'" '.$is_selected.'>'.get_the_title().'</option>';
endwhile; wp_reset_postdata();
?>
</select>
If what you want is save some POST information, is recommended save the ID of the POST, so later you can retrieve any data for that POST, what if you want to store permalink and title you can combine the functions get_permalink( ); and get_the_title(); in the select "value" attribute.
So I came up with a different solution. Instead of trying to save an array, I just saved the post ID which would allow me access to the title of the post as well as the permalink.
This is my modified code
<select name="pastor_select">
<?php
$args = array(
'post_type' => 'employee',
'position' => 'pastor'
);
$pastorList = new WP_Query($args); while ($pastorList->have_posts()) : $pastorList->the_post();
$employeeID = get_the_ID(); // THIS FIXED THE PROBLEM
$is_selected = ($employeeID == $selected) ? 'selected="selected"' : '';
echo '<option value="'.$employeeID.'" '.$is_selected.'>'.get_the_title().'</option>';
endwhile; wp_reset_postdata();
?>
</select>
And this is how I am calling it on the front end
<?php
$id = $post_meta_data['pastor_select'][0];
echo '<a href="'.get_permalink($id).'">';
echo get_the_title($id);
echo '</a>';
?>

Issues with results from joomla database to screen

I seem to be stuck. I have checked this document JDatabase 3.0
And i cant figure out why my rusults from the column wont display on the screen.
Its possible its an issue with the WHERE statement but it does echo out the correct username so I'm not sure. Here is the code I have been manipulating.
{source 0}
<?php
$user =& JFactory::getUser();
if (!$user->guest)
$name = $user->username;
$db =& JFactory::getDbo();
$query = $db->getQuery(true);
$query = "SELECT image_url FROM #__image_data WHERE user_name = $name";
$db->setQuery($query);
$results = $db->image_url(); //also tried $results = $db->loadObjectList
and loadAssocList
echo $results
?>
{/source}
You need
$list = $db->loadObjectList();
foreach ($list as $item){
echo $item->image_url.'<br />';
}

Create a custom loop with post IDs taken from Magic Fields 2 duplicate text fields

I'm trying to create a custom loop with content related to specific post IDs whose numbers I'm getting from a Magic Fields duplicate text field called "reference_posts".
When I echo $testvalue; it outputs the correct listing of posts "20432,43242,34253," but when I try to output it inside the array I only get the first value repeated over and over "20432,20432,20432,".
I'm guessing the problem is that I have to envelope the second foreach within the first but I'm not managing to do that.
Can anyone help me out?
<?php
$value = get_field ('reference_posts') ;
foreach ( $value as $my_val ) {
$testvalue = $my_val . ",";
echo $testvalue;
$post_idarray = array( 'post__in' => array( $testvalue ) );
$postspecial = get_posts($post_idarray);
}
foreach( $postspecial as $post ) :
setup_postdata($post);
?>
<div>my content</div>
<?php endforeach; ?>
Thanks in advance!
Got it with:
<?php
$value = get_field ('reference_posts') ;
foreach ( $value as $my_val );
$args = array( 'include' => $value );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<div>my content</div>
<?php endforeach; ?>

Resources