I have 3 tables and I'm working with wordpress too: wp7s_g_bookings, wp7s_g_tests and wp7s_g_clients. This is a study project, not to sell.
I want to be able to save 1 booking with X clients and Y tests.
I read about LAST_INSERT_ID() but I don't think it will work in this case and I'm lost.
Possible scenarios:
You are a partner, then the data is beeing pulled, so we have the ID because it exist already;
In this case we are adding other users data inside repeatable fields like name="client_birth[]" ... // client_vat only show/exist if the partner don't exist.
You are a normal client, in this case you fill the same client data plus client_vat (so I'm sure when adding into database that only the guy that have client_vat is the booking_client)
if (!empty($_POST)) {
$wpdb->query('START TRANSACTION');
//All this fields are repeatable inputs, except client_vat that can only be the 1st record. I think it's missing a foreach
$default = array(
'client_name' => $_POST['client_name'],
'client_birth' => $_POST['client_birth'],
'client_sns' => $_POST['client_sns'],
'client_vat' => $_POST['client_vat'],
'client_city' => $_POST['client_city'],
'client_email' => $_POST['client_email'],
'client_phone' => $_POST['client_phone']
);
$item = shortcode_atts( $default, $_REQUEST );
$addClients = $wpdb->insert( 'wp7s_g_clients', $item );
$default = array(
'test_client' => ???, //ID of previous client that have client_vat filled [0]
);
$item = shortcode_atts( $default, $_REQUEST );
$addTests = $wpdb->insert( 'wp7s_g_tests', $item );
$collectDate = date('Y-m-d H:i:s', strtotime($_POST['booking_collect_date']));
$default = array(
'booking_status' => 1,
'booking_partner' => $_POST['booking_partner'], //ID of the partner, if not empty
'booking_client' => ???, //If partner don't exist -> ID of client that have client_vat filled
'booking_type' => $_POST['booking_type'],
'booking_clients' => ???, //Array of all IDs previously added in wp7s_g_clients table
'booking_city' => $_POST['booking_city'],
'booking_address' => $_POST['booking_address'],
'booking_collect_date' => $collectDate,
'booking_payment' => $_POST['booking_payment'],
'booking_amount' => $_POST['booking_amount'],
'booking_obs' => nl2br($_POST['booking_obs']),
);
$item = shortcode_atts( $default, $_REQUEST );
$addBookings = $wpdb->insert( 'wp7s_g_bookings', $item );
if($addClients && $addTests && $addBookings) {
$wpdb->query('COMMIT');
$msg = "Success";
wp_redirect( esc_url( get_page_link( 6 ) ) );
exit;
}
else {
$wpdb->query('ROLLBACK');
$msg = "Error";
}
}
My issues are adding this properly into database the repeatable fields and using a previous created ID(s).
I tried to explain everything and comment so it's better to understand.
After you perform a query with $wpdb you can access the $wpdb->insert_id to get the last inserted id. You can find more details on the documentation page of wpdb
If you need more ids because u have more $wpdb->insert all you need to do is store $wpdb->insert_id after each insert, like:
...
$item = shortcode_atts( $default, $_REQUEST );
$addClients = $wpdb->insert( 'wp7s_g_clients', $item );
$clientId = $wpdb->insert_id;
....
$default = array(
'test_client' => ???, //ID of previous client that have client_vat filled [0]
);
$item = shortcode_atts( $default, $_REQUEST );
$addTests = $wpdb->insert( 'wp7s_g_tests', $item );
$testId = $wpdb->insert_id;
...
rest of your code
Related
I am trying to prevent duplicate entries for 2 of my Gravity Forms. I need to prevent these entries only within a certain timeframe and based on certain fields like 'email' or 'address'. So if they have an entry with the same 'email' prior to the start_date, they can still submit the form without issue. If they have an entry with the same 'email' after the start_date and before the end_date then they won't be able to submit the form. The code below is what I currently have in place, but it isn't working. Any help is greatly appreciated.
add_filter( 'gform_is_duplicate', 'custom_duplicate_entry_rules', 10, 4 );
function custom_duplicate_entry_rules( $count, $form_id, $field, $value ) {
$form_id = array( 6,61 );
$search_criteria = array(
'start_date' => 2021-02-01, // Get entries from date after last event
'end_date' => 2022-01-20, // Up to right before next event
'field_filters' => array( // which fields to search
array(
'key' => 'email',
)
)
);
$search_past_entries = GFAPI::count_entries($form_id, $search_criteria);
if ( $search_past_entries >= '1' ) {
echo 'You have already signed up.';
}
}
I managed to get this figured out with the help of a fellow developer...
add_filter( 'gform_is_duplicate_6', 'custom_duplicate_entry_rules', 10, 4 );
add_filter( 'gform_is_duplicate_61', 'custom_duplicate_entry_rules', 10, 4 );
function custom_duplicate_entry_rules( $count, $form_id, $field, $value ) {
$search_criteria = array(
'start_date' => '2021-02-01', // Get entries from date after last event
'end_date' => '2022-01-20', // Up to right before next event
'field_filters' => array( // which fields to search
array(
'key' => 2,
'value' => $value,
)
)
);
$count = GFAPI::count_entries( $form_id, $search_criteria );
if( $count >= 1 ) {
return $count;
} else {
return 0;
}
}
I needed to zero in on the specific entry meta in field_filters which in this case ended up being 2, for the email address (this is the field ID in Gravity Forms). Lastly, I changed the if statement to return $count or 0. Gravity Forms does all the heavy lifting beyond that.
i am trying to code a calendar script within wordpress and custom post types. works fine so far. I use several custom fields (ACF) to call my desired parameters (event title, start_date, end_date, etc.)
I call the results by get_posts().
but I have a problem with naming my keys within the array. after the foreach operation I can get the desired values e.g. event_title by typing $posts[0] or start_date by $posts['1'].
how can I achieve that I can name the keys so that I can use $posts[title] or $posts['start_date'] in my output?
I have tried to build a second array an use array_combine, but this fails.
<?php
$args = array( 'post_type' => 'cpt_kalender', 'post_status' => 'publish', 'numberposts' => '-1', 'meta_key' => 'cf_termin_start', 'orderby' => 'meta_value_num', 'order' => 'ASC' );
$posts = get_posts( $args );
$group_posts = array();
foreach ( $posts as $post ) : setup_postdata( $post );
/*
Hole die Termine der einzelnen Startdaten
- Schreibe das Datumsformat yyyymmdd in yyyy-mm-dd um
*/
$titel = get_the_title();
$termin_datum_start = get_field('cf_termin_start');
$termin_datum_start = new DateTime($termin_datum_start);
$termin_datum_ende = get_field('cf_termin_ende');
$termin_datum_ende = new DateTime($termin_datum_ende);
$termin_jahr = $termin_datum_start->format('Y');
$termin_monat = $termin_datum_start->format('F');
$termin_tag = $termin_datum_start->format('d');
$post_id = get_the_ID();
$termin_start_uhrzeit_stunde = get_field('cf_termin_start_uhrzeit_stunde');
$termin_start_uhrzeit_minute = get_field('cf_termin_start_uhrzeit_minute');
$termin_ende_uhrzeit_stunde = get_field('cf_termin_ende_uhrzeit_stunde');
$termin_ende_uhrzeit_minute = get_field('cf_termin_ende_uhrzeit_minute');
$termin_schulfrei = get_field('cf_termin_schulfrei');
$termin_intern = get_field('cf_termin_intern');
/*
Fülle das Array mit den Werten
*/
$group_posts[$termin_jahr][$termin_monat][] = array(
$titel,
$termin_datum_start,
$termin_datum_ende,
$post_id,
$termin_start_uhrzeit_stunde,
$termin_start_uhrzeit_minute,
$termin_ende_uhrzeit_stunde,
$termin_ende_uhrzeit_minute,
$termin_schulfrei,
$termin_intern
);
endforeach; ?>
Is there any way to tell the array listed above the desired key names in the same foreach operation?
i am thankful for every hint. thx a lot. I am just learning php and this drives me nuts. :(
The part where you are adding the values to the $group_posts array. You can add keys to each item.
$group_posts[$termin_jahr][$termin_monat][] = array(
'title' => $titel,
'termin_datum_start' => $termin_datum_start,
'termin_datum_ende' => $termin_datum_ende,
'post_id' => $post_id,
'termin_start_uhrzeit_stunde' => $termin_start_uhrzeit_stunde,
'termin_start_uhrzeit_minute' => $termin_start_uhrzeit_minute,
'termin_ende_uhrzeit_stunde' => $termin_ende_uhrzeit_stunde,
'termin_ende_uhrzeit_minute' => $termin_ende_uhrzeit_minute,
'termin_schulfrei' => $termin_schulfrei,
'termin_intern' => $termin_intern
);
Then you can access a value like $group_posts['termin_jahr']['termin_monat']['title']
Since you are adding each item with termin_jahr and termin_monat You will need to provide those first to access the correct item's title.
Alternatively you can just add those variables inside of the array and access it later like below:
$group_posts = array(
'title' => $titel,
'termin_datum_start' => $termin_datum_start,
'termin_datum_ende' => $termin_datum_ende,
'post_id' => $post_id,
'termin_start_uhrzeit_stunde' => $termin_start_uhrzeit_stunde,
'termin_start_uhrzeit_minute' => $termin_start_uhrzeit_minute,
'termin_ende_uhrzeit_stunde' => $termin_ende_uhrzeit_stunde,
'termin_ende_uhrzeit_minute' => $termin_ende_uhrzeit_minute,
'termin_schulfrei' => $termin_schulfrei,
'termin_intern' => $termin_intern,
'termin_jahr' => $termin_jahr,
'termin_monat' => $termin_monat,
);
You can do a foreach loop on the $group_posts array and then access each item like below:
foreach($group_posts as $group_post) {
// Outputs $titel variable which you added to array above
echo $group_post['title'];
}
I'm trying to use Prestashop WebService via PHP to filter products by categories but it seems that it's impossible.
How to make it? Must be something like this
array(‘resource’ =>’products’, ‘display’ => ‘[name]’, ‘filter[category]’ => ‘[x]');
Which Prestashop version do you use ?
I managed to get products for a specific category for v1.5.6.1 as follows:
$webService = new PrestaShopWebservice( YOUR_SITE_URL, YOUR_API_KEY, false );
$opt = array(
'resource' => 'products',
'display' => 'full',
'filter[id_category_default]' => '[8]',
'limit' => '5'
);
$xml = $webService->get($opt);
$resources = $xml->products->children();
At this stage you get a products collection. You can reach properties using standard object notation ..
$xml->categories->category->associations->products->product
foreach ( $resources as $key => $value ) :
echo $value->id; // product's identifier
echo $value->price; // product's .. guess what !
endforeach;
You should be able to see elements exposed by reaching YOUR_SITE/api/products?schema=synopsis
That's fine but I've not been able to retrieve products urls yet in order to print anchors.. Anyone ? Any suggustion ?
Complete documentation (1.5) here
Hope it will help.
EDIT
Construct products URLS on the fly
Make a first API call to retrieve categor(ies/y) you want and their
datas (url slug, ids of products they own, ...)
Make a second API call to retrieve the actual datas corresponding to ids retrieved during the first step.
Slugs are available under the link_rewrite property of items collections (like categories and products). There will be as many slugs as the total of languages that have been configured from the back-end, so you may want to loop over the link_rewrite property to get them all and build all urls.
## Initialize Prestashop API
$webService = new PrestaShopWebservice( YOUR_SITE_URL, YOUR_API_KEY, false );
## Getting category I want
$opt = array(
'resource' => 'categories',
'display' => 'full',
'filter[id]' => '[70]', # we are interested only in one category
'limit' => '1'
);
$xml = $webService->get($opt);
$ws_cat = $xml->categories->category;
$products = $ws_cat->associations->products->product;
## Gathering products ids to feed the second API call filter parameter
$productsIds = array();
foreach ( $products as $p ) {
$productsIds[] = (int)$p->id;
}
## Getting products ..
$opt = array (
'resource' => 'products',
'display' => 'full',
'filter[id]' => '['.implode('|',$productsIds).']',
'limit' => '4'
);
$xml = $webService->get($opt);
$products = $xml->products->product;
if ( count($products) ) {
$products = array();
foreach ( $products as $value ) {
$products[] = array(
'id' => $value->id
,'catalogURL' => "{$prestashop['url']}/{$ws_cat->link_rewrite->language[0]}/{$value->id}-{$value->link_rewrite->language[0]}.html";
);
# There you go ..
}
}
Learning php and I am losing my mind trying to solve this for days now. Please help.
This is a code which goes thought a table COUPON, take data with a condition met, and download it afterwards. In this table COUPON I have USER_ID as number but I want to have a user name also, which is kept in another table USER.
How can I go to another table (USER) and take names (REALNAME) by this USER_ID which is the same in both tables?
if ( $_POST ) {
$team_id = abs(intval($_POST['team_id']));
$consume = $_POST['consume'];
if (!$team_id || !$consume) die('-ERR ERR_NO_DATA');
$condition = array(
'team_id' => $team_id,
'consume' => $consume,
);
$coupons = DB::LimitQuery('coupon', array(
'condition' => $condition,
));
if (!$coupons) die('-ERR ERR_NO_DATA');
$team = Table::Fetch('team', $team_id);
$name = 'coupon_'.date('Ymd');
$kn = array(
'id' => 'ID',
'secret' => 'Password',
'date' => 'Valid',
'consume' => 'Status',
);
$consume = array(
'Y' => 'Used',
'N' => 'Unused',
);
$ecoupons = array();
foreach( $coupons AS $one ) {
$one['id'] = "#{$one['id']}";
$one['consume'] = $consume[$one['consume']];
$one['date'] = date('Y-m-d', $one['expire_time']);
$ecoupons[] = $one;
}
down_xls($ecoupons, $kn, $name);
After this, I want to try to do the same thing using only SQL queries.
You would need to JOIN the tables in the SQL query
SELECT something FROM coupons as coupons JOIN user as user ON coupons.id=user.id
You should use join when you want to retrieve details from two tables.
Join table COUPON and table USER based on user_id . This should yield results you want.
Hello All,
I know how to fetch record with the help of podscms,
But I would like to update the record fetched by podscms.
like
$somePod = pods('somepod')->update($my_array);
Anybody have some suggestion,
This is available in pods()->save() - http://pods.io/docs/save/
<?php
// Get the book item with an ID of 5
$pod = pods( 'book', 5 );
// Set the author (a user relationship field)
// to a user with an ID of 2
$pod->save( 'author', 2 );
// Set a group of fields to specific values
$data = array(
'name' => 'New book name',
'author' => 2,
'description' => 'Awesome book, read worthy!'
);
// Save the data as set above
$pod->save( $data );
// Or the shorthand
$id = pods( 'yourpod', $id )->save( $data );
Also available is add() - http://pods.io/docs/add/