'; print_r($form_data_array); echo ''; // Table name for field selector settings $table_name_settings = $wpdb->prefix . 'ai_app_onsite_field_selector_settings'; $max_field_option = $wpdb->get_var("SELECT MAX(field_position) FROM $table_name_settings"); // Iterate through form data and insert/update settings foreach ($form_data_array as $index => $formData) { // Check if settings already exist for this field $existing_settings = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name_settings WHERE field_uniqueid = %s", $formData['field_uniqueid'])); // echo 'existing_settings = {{'.$formData['field_label'].'}}'; $field_label = $formData['field_label']; // Replace underscores with spaces $field_label = str_replace(' ', '_', $field_label); // Convert the first letter to lowercase $field_label = strtolower($field_label); // Enclose the key in curly braces $field_tag = '{{' . $field_label . '}}'; $field_id = $formData['field_id']; $replaceField_id = str_replace(' ', '-', $field_id); // Convert the first letter to lowercase $formField_id = strtolower($replaceField_id); if (isset($formData['field_dropdown_options'])) { // Split the string into an array using '|' as a delimiter $field_dropdown_options = explode('|', $formData['field_dropdown_options']); } else { $field_dropdown_options = []; } // Serialize the array to store in the database $field_dropdown_options = serialize($field_dropdown_options); $new_field_position = $existing_settings->field_position; // If settings already exist, update the row; otherwise, insert a new row if ($existing_settings) { // Prepare data for update $update_data = array( 'app_id' => 1, // Adjust as necessary 'field_uniqueid' => $formData['field_uniqueid'], 'field_id' => $formField_id, 'app_width' => $formData['app_width_figure'], 'field_type' => $formData['field_creator'], 'field_label' => $formData['field_label'], 'field_maxchar' => $formData['field_maxchar'], 'field_dropdown_options' => $field_dropdown_options, 'field_sample_text' => $formData['field_sample_text'], 'field_require_field_status' => $formData['section_required'] ? 1 : 0, 'field_include_in_export_email' => $formData['include_in_email'] ? 1 : 0, 'field_position' => $new_field_position, 'field_tag' => $field_tag, // 'created_at' => current_time('mysql'), 'updated_at' => gmdate("Y-m-d H:i:s") ); // Update existing row $wpdb->update( $table_name_settings, $update_data, array('id' => $existing_settings->id), array('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s'), array('%d') // where format ); } else { $field_position = $max_field_option + 1; // Prepare data for insertion $create_data = array( 'app_id' => 1, // Adjust as necessary 'field_uniqueid' => $formData['field_uniqueid'], 'field_id' => $formData['field_id'], 'app_width' => $formData['app_width_figure'], 'field_type' => $formData['field_creator'], 'field_label' => $formData['field_label'], 'field_maxchar' => $formData['field_maxchar'], 'field_dropdown_options' => $field_dropdown_options, 'field_sample_text' => $formData['field_sample_text'], 'field_require_field_status' => $formData['section_required'] ? 1 : 0, 'field_include_in_export_email' => $formData['include_in_email'] ? 1 : 0, 'field_position' => $field_position, 'field_tag' => $field_tag, 'created_at' => gmdate("Y-m-d H:i:s"), 'updated_at' => gmdate("Y-m-d H:i:s") ); // Insert new row $wpdb->insert( $table_name_settings, $create_data, array('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%s') ); } // Check if the database operation was successful if ($wpdb->last_error) { wp_send_json_error('Error savingdf data: ' . $wpdb->last_error); } } // If reached here, data saved successfully wp_send_json_success('Settings saved successfully.'); } public function ai_app_onsite_read_field_selector_callback() { global $wpdb; $table_name = $wpdb->prefix . 'ai_app_onsite_field_selector_settings'; // Fetch data from the database $results = $wpdb->get_results("SELECT * FROM $table_name ORDER BY field_position ASC", ARRAY_A); // HTML content string to store the generated HTML $html_content = ''; // Loop through each record and generate HTML content $ctr = 0; $app_width = $results[0]['app_width']; foreach ($results as $record) { $html_content .= '