$old = get_post_meta($post_id, 'figure_sugsubject_repeatable_fields', true);
$new = array();
$figuresugsubjectpositions = $_POST['figuresugsubjectposition'];
$figuresugsubjectworkplaces = $_POST['figuresugsubjectworkplace'];
$figuresugsubjectlocations = $_POST['figuresugsubjectlocation'];
$figuresugsubjectfroms = $_POST['figuresugsubjectfrom'];
$figuresugsubjectstatuss = $_POST['figuresugsubjectstatus'];
$count = count( $figuresugsubjectpositions );
for ( $i = 0; $i < $count; $i++ ) {
if ( $figuresugsubjectpositions[$i] != '' ) :
$new[$i]['figuresugsubjectposition'] = stripslashes( strip_tags( $figuresugsubjectpositions[$i] ) );
$new[$i]['figuresugsubjectworkplace'] = $figuresugsubjectworkplaces[$i];
$new[$i]['figuresugsubjectlocation'] = $figuresugsubjectlocations[$i];
$new[$i]['figuresugsubjectfrom'] = $figuresugsubjectfroms[$i];
$new[$i]['figuresugsubjectstatus'] = $figuresugsubjectstatuss[$i];
endif;
}
if ( !empty( $new ) && $new != $old )
update_post_meta( $post_id, 'figure_sugsubject_repeatable_fields', $new );
elseif ( empty($new) && $old )
delete_post_meta( $post_id, 'figure_sugsubject_repeatable_fields', $old );
I have a frontend form for post submission. I want the new entries in the form not to replace the old ones, instead I want it to be added. To replace update_post_meta with add_post_meta will not solve the problem as users will not be able to edit their old inputs.
Thank you.
Related
This is my code using PHPExcel and NinjaForms. Basically the code does go inside if( isset($sub[$field_key]) ){ so all data is OK. I have also echoed it but somehow the rows are empty.
public function export_file( $form_id ) {
// set xls always on
$use_xls = true;
// set save dir
$upload_dir = wp_upload_dir();
$tmp_file = $upload_dir['basedir'] . '/test/' . $form_id . '/form-submissions-'. $form_id .'.xls';;
$sub_results = $this->get_submissions( $form_id );
$num_submissions = $this->count_submissions($form_id );
$fields_meta = Ninja_Forms()->form($form_id)->get_fields();
$field_names=array();
$header_row=array();
$header_row['id'] = __('ID','ninja-forms-spreadsheet');
$header_row['date_submitted'] = __('Submission date','ninja-forms-spreadsheet');
foreach($fields_meta as $field_id => $field){
$field_settings = $field->get_settings();
// skip save fields
if( $field_settings['type'] == 'save' || $field_settings['type'] == 'submit' ) {
unset( $fields_meta[$field_id] );
continue;
}
$field_names[$field_id] = sanitize_title( $field_settings['label'].'-'.$field_id );
$field_types[$field_id] = $field_settings['type'];
$header_row[$field_id] = $field_settings['label'];
}
//die();
require_once( plugin_dir_path( __DIR__ ).'/ninja-forms-excel-export/includes/PHPExcel.php');
$objPHPExcel = new PHPExcel();
$row_number = 1;
// this should be the same wordpress uses and therefore be the saver choice
PHPExcel_Shared_File::setUseUploadTempDirectory( true );
// Table Headline
$col_index = 0;
foreach ($header_row as $headline) {
$col = PHPExcel_Cell::stringFromColumnIndex( $col_index );
$objPHPExcel->getActiveSheet()->getCell($col.$row_number)->setValue( $headline );
$objPHPExcel->getActiveSheet()->getStyle($col.$row_number)->getFont()->setBold(true);
$col_index++;
}
$objPHPExcel->getActiveSheet()->freezePane( "A2" );
$row_number++;
/*
* Loop over each sub
*/
foreach($sub_results as $sub){
$col = 'A';
$objPHPExcel->getActiveSheet()->getCell($col++.$row_number)->setValueExplicit( $sub['_seq_num'], PHPExcel_Cell_DataType::TYPE_NUMERIC );
$objPHPExcel->getActiveSheet()->getCell($col++.$row_number)->setValueExplicit( $sub['date_submitted'] );
$col_index = 0;
foreach ($fields_meta as $field_id => $field) {
$field_key = '_field_'.$field_id;
if( isset($sub[$field_key]) ){
$field_value = $sub[$field_key];
$col = PHPExcel_Cell::stringFromColumnIndex( 2+$col_index );
$field_value = maybe_unserialize( $field_value );
echo $field_types[$field_id] . ' ';
if( in_array($field_types[$field_id], array( 'shipping', 'total', 'number' ) ) ){ // float values
$objPHPExcel->getActiveSheet()->getCell($col.$row_number) ->setValueExplicit( $field_value, PHPExcel_Cell_DataType::TYPE_NUMERIC );
$objPHPExcel->getActiveSheet()->getStyle($col.$row_number)
->getNumberFormat()
->setFormatCode('#,##');
}elseif( in_array($field_types[$field_id], array( 'starrating', 'quantity' ) ) ){ // integer values
$objPHPExcel->getActiveSheet()->getCell($col.$row_number) ->setValueExplicit( $field_value, PHPExcel_Cell_DataType::TYPE_NUMERIC );
$objPHPExcel->getActiveSheet()->getStyle($col.$row_number)
->getNumberFormat()
->setFormatCode('#');
}elseif( in_array($field_types[$field_id], array( 'checkbox' ) ) || strpos($field_types[$field_id], '-optin') ){
$objPHPExcel->getActiveSheet()->getCell($col.$row_number)->setValueExplicit( $field_value, PHPExcel_Cell_DataType::TYPE_STRING );
}elseif( in_array($field_types[$field_id], array( 'listcheckbox' ) ) ){
$field_output = $field_value;
if( is_array($field_value) ){
$field_output = '';
foreach ($field_value as $key => $value) {
if( $field_output == '' )
$field_output = $value;
else
$field_output .= ', ' . $value;
}
}
$objPHPExcel->getActiveSheet()->getCell($col.$row_number)->setValueExplicit( htmlspecialchars_decode ( $field_output,ENT_QUOTES ), PHPExcel_Cell_DataType::TYPE_STRING );
}elseif( $field_types[$field_id] == 'file_upload' ){
if( is_array($field_value)){
$field_value = implode("\n", $field_value);
}
$objPHPExcel->getActiveSheet()->getCell($col.$row_number)->setValueExplicit( htmlspecialchars_decode ( $field_value,ENT_QUOTES ), PHPExcel_Cell_DataType::TYPE_STRING );
$objPHPExcel->getActiveSheet()->getStyle($col.$row_number)
->getAlignment()
->setWrapText(true);
}elseif( $field_types[$field_id] == 'textarea' ){
$objPHPExcel->getActiveSheet()->getCell($col.$row_number)->setValueExplicit( htmlspecialchars_decode ( $field_value,ENT_QUOTES ), PHPExcel_Cell_DataType::TYPE_STRING );
$objPHPExcel->getActiveSheet()->getStyle($col.$row_number)
->getAlignment()
->setWrapText(true);
}else{
if( is_array($field_value) ){
$field_value = implode('; ', $field_value);
}
$objPHPExcel->getActiveSheet()->getCell($col.$row_number)->setValueExplicit( htmlspecialchars_decode ( $field_value,ENT_QUOTES ), PHPExcel_Cell_DataType::TYPE_STRING );
}
}
$col_index++;
}
$row_number++;
}
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
ob_end_clean();
$objWriter->save($tmp_file);
}
The excel modified time is updated correctly but all rows are empty except the first header row in bold. I am not sure what is the issue.
Any help is appreciated.
I have created dynamic add / remove file field to insert image in database. But somehow it is not working properly. Following is my code...
$member_image_names = array_map( 'sanitize_file_name', $_FILES['member_image']['name'] );
foreach ( $member_image_names as $member_image_name ) {
if ( empty( $member_image_name ) ) {
global $wpdb;
$project_id = $_SESSION['project_id'];
$project_member_details = $wpdb->get_var( $wpdb->prepare( "SELECT project_members FROM wpxa_orocox_project_members WHERE project_id = %d", $project_id ) );
$project_member_detail = json_decode( $project_member_details, true );
$member_image_newname = $project_member_detail['member_image'];
} else {
$member_image_ext = strtolower( end( explode( '.', $member_image_name ) ) );
$member_image_newname[] = get_current_user_id() . $_SESSION['project_id'] . "_" . time() . "_" . mt_rand() . "." . $member_image_ext;
}
$member_details->member_image = $member_image_newname;
$member_details_encode = wp_json_encode( $member_details );
global $wpdb;
$members_result_update = $wpdb->update( 'wpxa_project_members',
array( 'project_members' => $member_details_encode ),
array( 'project_id' => $_SESSION['project_id'] ),
array( '%s' ),
array( '%d' )
);
if ( ! empty( $members_result_update ) ) {
$member_details_decode = json_decode($member_details_encode, true);
$count_member_decode = count( $member_details_decode['member_image'] );
for ( $i = 0; $i < $count_member_decode; $i++ ) {
$m_image_name = $member_details_decode['member_image'][$i];
$profile_image_folder = "profile-images/";
$profile_image_path = trim( $profile_image_folder . basename( $m_image_name ) );
$profile_image_temp = $_FILES['member_image']['tmp_name'][$i];
$profile_image_ext = strtolower( end( explode( '.', $m_image_name ) ) );
if ( $profile_image_ext == "jpg" || $profile_image_ext == "png" || $profile_image_ext == "jpeg" || $profile_image_ext == "gif" ) {
move_uploaded_file( $profile_image_temp, $profile_image_path );
}
}
}
}
Every thing works well but when I select a new image in first field and submit the value to the database, it removes all other images of other fields from database...
And if I select image for other fields and submit the value to database it then creates new field with that value...
In simple terms, When I want to update any particular file field in the dynamic add / remove file field then that particular field should only be get updated and other fields should remain intact.
Pl Help...
I had this problem and i did it :(Example)
if ( empty( $options['index1_cardimg'] ) ) {$options['index1_cardimg'] = get_myoption_value('index1_cardimg');}
without this check (if (empty){}) all other fields will be reset .
just you need to fill them by last data (if they dont send) .
with a code like this:
if ( empty( $members_result_update ) ) {
// fill Other options with last value
}
I have a problem with the following code when I make the price variable the function doesn't work but when I hardcode a price it works. (note I am in a testing face and haven't worried about security)
this, down't work:
function add_custom_price( $cart_obj ) {
global $product, $woocommerce ,$wpdb;
if ( isset( $_POST['fra'] ) ){
$GLOBALS['$_fra'] = urldecode( $_POST["fra"] );
} else {
$GLOBALS['$_fra'] = "";
}
if ( isset( $_POST['til'] ) ){
$GLOBALS['$_til'] = urldecode($_POST["til"]);
} else {
$GLOBALS['$_til'] = "";
}
if ( $GLOBALS['$_til'] !="" && $GLOBALS['$_fra'] !="" ){
$t1 = urldecode( $GLOBALS['$_til'] );
$t2 = urldecode( $GLOBALS['$_fra']);
$objArray = $wpdb->get_results("SELECT $t1 FROM test_priser WHERE city = '$t2'");
if ( isset($objArray[0]->$t1) ){
$priser = explode("/",$objArray[0]->$t1);
if ( !isset($priser[1]) ){
$priser[1] = intval($priser[0]);
}
}
echo "priser[1] = ".$priser[1]; //this outputs the expected value
}
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
return;
}
foreach ( $cart_obj->get_cart() as $key => $value ) {
$value['data']->set_price( $priser[1] );
}
}
When I add this line, and override the variable, it works:
$priser[1] = 1000;
I am quite confused as to what the problem might be.
for anyone else who might stumble upon this. Seems I need to pass the variable price in the session then it works.
you can find the answer here: custom price on woocommerce product
$authors = $newsitem->get_item_authors();
if( !empty( $authors ) ){
$facebook_author .= ',' .get_user_meta( $authors[0]->ID, 'eco_author_social_profile', true );
}
Trying to concat multiple authors in a fb share
You can try with implode
<?php
$authors = $newsitem->get_item_authors();
$facebook_authors = array()
if( !empty( $authors ) ){
$facebook_authors[] = get_user_meta( $authors[0]->ID, 'eco_author_social_profile', true );
}
$facebook_author = implode(",", $facebook_authors);
?>
You can also do this with your code
$authors = $newsitem->get_item_authors();
if( !empty( $authors ) ){
$facebook_author .= ',' .get_user_meta( $authors[0]->ID, 'eco_author_social_profile', true );
}
$facebook_author = ltrim($facebook_author, ",");
I'm trying to get more advanced with php and I pick up the book PHP 5 Social Networking by Michael Peacock. While the book seemed to be interesting it didn't however get to involved in the details of the code. The function I'm trying to figure out is,
public function getURLData()
{
$urldata = ( isset( $_GET['page'] ) ) ? $_GET['page'] : '' ;
$this->urlPath = $urldata;
if( $urldata == '' )
{
$this->urlBits[] = '';
$this->urlPath = '';
}
else
{
$data = explode( '/', $urldata );
while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
{
//NOTES: php array_shift — Shift an element off the beginning of array
array_shift( $data );
}
while ( !empty( $data ) && strlen( end( $data ) ) === 0)
{
array_pop($data);
}
$this->urlBits = $this->array_trim( $data );
}
}
This a part of a larger class and the $_GET['page'] is something like this: relationships/mutual/3. My main question is what is happening in the else section. I think what is happening that it's removing any empty array indexes but I also question that.
Any help would be appreciated.
EDIT: added array_trim function that is also part of the class
private function array_trim( $array )
{
while ( ! empty( $array ) && strlen( reset( $array ) ) === 0)
{
array_shift( $array );
}
while ( !empty( $array ) && strlen( end( $array ) ) === 0)
{
array_pop( $array );
}
return $array;
}
public function getURLData()
{
Gets the 'page', this data can be obtained by $_GET from the url: for instance: http://mysite.com/?page=contact
If 'page' has been set, is assigned to $urldata, else $urldata=''
$urldata = ( isset( $_GET['page'] ) ) ? $_GET['page'] : '' ;
$this->urlPath = $urldata;
if( $urldata == '' )
{
$this->urlBits[] = '';
$this->urlPath = '';
}
else
{
Now is creating an array with all the substrings from $urldata splited by '/'
$data = explode( '/', $urldata );
If the array $data is not empty (otherwise accessing a non-existent element would raise an exception) or the lenght of the first element is equal to 0, then removes the first element from the array.
while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
{
//NOTES: php array_shift — Shift an element off the beginning of array
array_shift( $data );
}
If the array $data is not empty (otherwise accessing a non-existent element would raise an exception) or the lenght of the last element is equal to 0, then removes the last element from the array.
while ( !empty( $data ) && strlen( end( $data ) ) === 0)
{
array_pop($data);
}
array_trim is a custom function, not sure what does but probably will do some kind of trimming too
$this->urlBits = $this->array_trim( $data );
}
}